Files
DEFENDER/core/classes/interfaces/imodule.py
2025-11-18 16:27:42 +01:00

87 lines
2.5 KiB
Python

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Optional, Union
from dataclasses import dataclass
if TYPE_CHECKING:
from core.loader import Loader
class IModule(ABC):
@abstractmethod
@dataclass
class ModConfModel:
"""The Model containing the module parameters
"""
def __init__(self, uplink: 'Loader') -> None:
# import the context
self.ctx = uplink
# Module name (Mandatory)
self.module_name = 'mod_' + str(self.__class__.__name__).lower()
# Log the module
self.ctx.Logs.debug(f'Loading Module {self.module_name} ...')
def init(self) -> None:
self.load()
self.create_tables()
# Sync the configuration with core configuration (Mandatory)
self.ctx.Base.db_sync_core_config(self.module_name, self.mod_config)
return None
def update_configuration(self, param_key: str, param_value: Union[str, int]) -> None:
"""Update the local and core configuration
Args:
param_key (str): The parameter key
param_value (str): The parameter value
"""
self.ctx.Base.db_update_core_config(self.module_name, self.mod_config, param_key, param_value)
@property
@abstractmethod
def mod_config(self) -> ModConfModel:
"""
The module configuration model
"""
@abstractmethod
def create_tables(self) -> None:
"""Method that will create the database if it does not exist.
A single Session for this class will be created, which will be used within this class/module.
Returns:
None: No return is expected
"""
@abstractmethod
def load(self) -> None:
"""This method is executed when the module is loaded or reloaded.
"""
@abstractmethod
def unload(self) -> None:
"""This method is executed when the module is unloaded or reloaded.
"""
@abstractmethod
def cmd(self, data: list) -> None:
"""When recieving server messages.
Args:
data (list): The recieved message
"""
@abstractmethod
def hcmds(self, user: str, channel: Optional[str], cmd: list[str], fullcmd: Optional[list[str]] = None) -> None:
"""These are the commands recieved from a client
Args:
user (str): The client
channel (str|None): The channel if available
cmd (list): The user command sent
fullcmd (list, optional): The full server message. Defaults to [].
"""