Update Votekick module! following the same structure as other modules

This commit is contained in:
adator85
2025-08-16 02:11:21 +02:00
parent e075b7b8d5
commit 3fc49e9069
5 changed files with 121 additions and 4 deletions

View File

@@ -127,7 +127,7 @@ class Irc:
# Define the IrcSocket object # Define the IrcSocket object
self.IrcSocket:Union[socket.socket, SSLSocket] = None self.IrcSocket: Union[socket.socket, SSLSocket] = None
self.__create_table() self.__create_table()
self.Base.create_thread(func=self.heartbeat, func_args=(self.beat, )) self.Base.create_thread(func=self.heartbeat, func_args=(self.beat, ))
@@ -145,6 +145,7 @@ class Irc:
self.init_service_user() self.init_service_user()
self.__create_socket() self.__create_socket()
self.__connect_to_irc(ircInstance) self.__connect_to_irc(ircInstance)
except AssertionError as ae: except AssertionError as ae:
self.Logs.critical(f'Assertion error: {ae}') self.Logs.critical(f'Assertion error: {ae}')
@@ -197,6 +198,7 @@ class Irc:
self.Logs.critical(f"AttributeError: {ae} - {soc.fileno()}") self.Logs.critical(f"AttributeError: {ae} - {soc.fileno()}")
def __ssl_context(self) -> ssl.SSLContext: def __ssl_context(self) -> ssl.SSLContext:
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.check_hostname = False ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE ctx.verify_mode = ssl.CERT_NONE

View File

@@ -1,3 +1,6 @@
'''
Main utils library.
'''
from pathlib import Path from pathlib import Path
from typing import Literal, Optional, Any from typing import Literal, Optional, Any
from datetime import datetime from datetime import datetime

View File

@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Any
import re import re
from dataclasses import dataclass, field from dataclasses import dataclass, field
@@ -55,6 +55,9 @@ class Votekick():
# Add Channel object to the module # Add Channel object to the module
self.Channel = ircInstance.Channel self.Channel = ircInstance.Channel
# Add Utils.
self.Utils = ircInstance.Utils
# Créer les nouvelles commandes du module # Créer les nouvelles commandes du module
self.Irc.build_command(1, self.module_name, 'vote', 'The kick vote module') self.Irc.build_command(1, self.module_name, 'vote', 'The kick vote module')
@@ -150,7 +153,7 @@ class Votekick():
Args: Args:
channel (str): le salon à enregistrer. channel (str): le salon à enregistrer.
""" """
current_datetime = self.Base.get_datetime() current_datetime = self.Utils.get_sdatetime()
mes_donnees = {'channel': channel} mes_donnees = {'channel': channel}
response = self.Base.db_execute_query("SELECT id FROM votekick_channel WHERE channel = :channel", mes_donnees) response = self.Base.db_execute_query("SELECT id FROM votekick_channel WHERE channel = :channel", mes_donnees)
@@ -256,7 +259,7 @@ class Votekick():
except Exception as err: except Exception as err:
self.Logs.error(f"General Error: {err}") self.Logs.error(f"General Error: {err}")
def hcmds(self, user:str, channel: any, cmd: list, fullcmd: list = []) -> None: def hcmds(self, user:str, channel: Any, cmd: list, fullcmd: list = []) -> None:
# cmd is the command starting from the user command # cmd is the command starting from the user command
# full cmd is sending the entire server response # full cmd is sending the entire server response

11
mods/votekick/schemas.py Normal file
View File

@@ -0,0 +1,11 @@
from typing import Optional
from core.definition import MainModel, dataclass, field
@dataclass
class VoteChannelModel(MainModel):
channel_name: Optional[str] = None
target_user: Optional[str] = None
voter_users: list = field(default_factory=list)
vote_for: int = 0
vote_against: int = 0

View File

@@ -0,0 +1,98 @@
from typing import TYPE_CHECKING, Optional
from mods.votekick.schemas import VoteChannelModel
if TYPE_CHECKING:
from mods.votekick.mod_votekick import Votekick
class VotekickManager:
VOTE_CHANNEL_DB:list[VoteChannelModel] = []
def __init__(self, uplink: 'Votekick'):
self.uplink = uplink
self.Logs = uplink.Logs
self.Utils = uplink.Utils
def activate_new_channel(self, channel_name: str) -> bool:
"""Activate a new channel in the votekick systeme
Args:
channel_name (str): The channel name you want to activate
Returns:
bool: True if it was activated
"""
votec = self.get_vote_channel_model(channel_name)
if votec is None:
self.VOTE_CHANNEL_DB.append(
VoteChannelModel(
channel_name=channel_name,
target_user='',
voter_users=[],
vote_for=0,
vote_against=0
)
)
self.Logs.debug(f"[VOTEKICK MANAGER] {channel_name} has been activated.")
return True
return False
def get_vote_channel_model(self, channel_name: str) -> Optional[VoteChannelModel]:
"""Get Vote Channel Object model
Args:
channel_name (str): The channel name you want to activate
Returns:
(VoteChannelModel | None): The VoteChannelModel if exist
"""
for vote in self.VOTE_CHANNEL_DB:
if vote.channel_name.lower() == channel_name.lower():
self.Logs.debug(f"[VOTEKICK MANAGER] {channel_name} has been found in the VOTE_CHANNEL_DB")
return vote
return None
def drop_vote_channel_model(self, channel_name: str) -> bool:
"""Drop a channel from the votekick system.
Args:
channel_name (str): The channel name you want to drop
Returns:
bool: True if the channel has been droped.
"""
votec = self.get_vote_channel_model(channel_name)
if votec:
self.VOTE_CHANNEL_DB.remove(votec)
self.Logs.debug(f"[VOTEKICK MANAGER] {channel_name} has been removed from the VOTE_CHANNEL_DB")
return True
return False
def is_vote_ongoing(self, channel_name: str) -> bool:
"""Check if there is an angoing vote on the channel provided
Args:
channel_name (str): The channel name to check
Returns:
bool: True if there is an ongoing vote on the channel provided.
"""
votec = self.get_vote_channel_model(channel_name)
if votec is None:
self.Logs.debug(f"[VOTEKICK MANAGER] {channel_name} is not activated!")
return False
if votec.target_user:
self.Logs.debug(f'[VOTEKICK MANAGER] A vote is ongoing on {channel_name}')
return True
self.Logs.debug(f'[VOTEKICK MANAGER] {channel_name} is activated but there is no ongoing vote!')
return False