Fixed module loading & added config

This commit is contained in:
Cora Cordes 2022-01-13 20:18:21 +01:00
parent ca7798987c
commit 9eef94baf2
7 changed files with 23 additions and 21 deletions

@ -1 +1 @@
Subproject commit 370e96c5b19290d8d229fd71febbaf5ca51544ae Subproject commit 3d4e93d4d658e1c6c571756f99a3297efb8a7d64

View File

@ -1,23 +1,22 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Optional from typing import Optional
from cpl_core.configuration import ConfigurationModelABC
class ModuleABC(ABC): class ModuleABC(ABC):
@abstractmethod @abstractmethod
def __init__(self, priorities: dict[type, int], settings_type: Optional[type]): def __init__(self, priorities: dict[type, int], settings_types: list[Optional[type]]):
self._priorities = priorities self._priorities = priorities
self._success = True self._success = True
self._settings_type = settings_type self._settings_types = settings_types
@property @property
def success(self) -> bool: def success(self) -> bool:
return self._success return self._success
@property @property
def settings_type(self) -> type: def settings_types(self) -> list[type]:
return self._settings_type return self._settings_types
def get_priority(self, t: type) -> int: def get_priority(self, t: type) -> int:
if t not in self._priorities: if t not in self._priorities:

View File

@ -81,11 +81,14 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=CommandsMeta):
self._logger.warn(__name__, f'Module {module_type} not found in services!') self._logger.warn(__name__, f'Module {module_type} not found in services!')
break break
if module.settings_type is not None: if len(module.settings_types) > 0:
with open(f'config/{String.convert_to_snake_case(type(module).__name__).lower()}.json', encoding='utf-8') as cfg: for settings_type in module.settings_types:
settings_name = settings_type.__name__.split('Settings')[0]
with open(f'config/{String.convert_to_snake_case(settings_name).lower()}.json', encoding='utf-8') as cfg:
json_cfg = json.load(cfg) json_cfg = json.load(cfg)
for index in json_cfg: for index in json_cfg:
settings: ConfigurationModelABC = module.settings_type() settings: ConfigurationModelABC = settings_type()
settings.from_dict(json_cfg[index]) settings.from_dict(json_cfg[index])
self._config.add_configuration(f'{type(module).__name__}_{index}', settings) self._config.add_configuration(f'{type(module).__name__}_{index}', settings)
self._logger.debug(__name__, f'Added config: {type(module).__name__}_{index}') self._logger.debug(__name__, f'Added config: {type(module).__name__}_{index}')

View File

@ -70,7 +70,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
OnMessageABC: 30, OnMessageABC: 30,
OnVoiceStateUpdateABC: 10 OnVoiceStateUpdateABC: 10
}, },
BaseSettings [BaseSettings]
) )
self._bot.add_cog(base_command_service) self._bot.add_cog(base_command_service)
self._logger.info(__name__, f'Module {type(self)} loaded') self._logger.info(__name__, f'Module {type(self)} loaded')

View File

@ -33,7 +33,7 @@ class BootLog(ModuleABC, OnReadyABC):
{ {
OnReadyABC: 10 OnReadyABC: 10
}, },
BootLogSettings [BootLogSettings]
) )
self._logger.info(__name__, f'Module {type(self)} loaded') self._logger.info(__name__, f'Module {type(self)} loaded')

View File

@ -3,11 +3,11 @@ from datetime import datetime
import discord import discord
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC
from cpl_core.database import DatabaseSettings
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.configuration.server_settings import ServerSettings
from gismo_data.abc.client_repository_abc import ClientRepositoryABC from gismo_data.abc.client_repository_abc import ClientRepositoryABC
from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC
from gismo_data.abc.user_joined_server_repository_abc import \ from gismo_data.abc.user_joined_server_repository_abc import \
@ -56,7 +56,7 @@ class Database(ModuleABC, OnReadyABC):
ModuleABC.__init__( ModuleABC.__init__(
self, self,
{OnReadyABC: 0}, {OnReadyABC: 0},
None [DatabaseSettings, BaseSettings]
) )
self._logger.info(__name__, f'Module {type(self)} loaded') self._logger.info(__name__, f'Module {type(self)} loaded')
@ -274,7 +274,7 @@ class Database(ModuleABC, OnReadyABC):
self._logger.warn(__name__, f'Active UserJoinedVoiceChannel found in database: {guild.id}:{member.id}@{join.joined_on}') self._logger.warn(__name__, f'Active UserJoinedVoiceChannel found in database: {guild.id}:{member.id}@{join.joined_on}')
join.leaved_on = datetime.now() join.leaved_on = datetime.now()
settings: BaseSettings = self._config.get_configuration(f'DSERVER_{guild.id}') settings: BaseSettings = self._config.get_configuration(f'base_{guild.id}')
if ((join.leaved_on - join.joined_on).total_seconds()/60/60) > settings.max_voice_state_hours: if ((join.leaved_on - join.joined_on).total_seconds()/60/60) > settings.max_voice_state_hours:
join.leaved_on = join.joined_on + datetime.timedelta(hours=settings.max_voice_state_hours) join.leaved_on = join.joined_on + datetime.timedelta(hours=settings.max_voice_state_hours)

View File

@ -21,7 +21,7 @@ class Permission(ModuleABC, OnReadyABC, OnMemberUpdateABC):
ModuleABC.__init__( ModuleABC.__init__(
self, self,
{OnReadyABC: 1, OnMemberUpdateABC: 0}, {OnReadyABC: 1, OnMemberUpdateABC: 0},
PermissionSettings [PermissionSettings]
) )
self._logger.info(__name__, f'Module {type(self)} loaded') self._logger.info(__name__, f'Module {type(self)} loaded')