sh_discord_bot/src/bot/application.py

84 lines
4.3 KiB
Python
Raw Normal View History

2022-10-02 12:14:44 +02:00
from typing import Optional, Type, Callable
from cpl_core.configuration import ConfigurationABC, ConfigurationModelABC
2022-07-14 16:32:46 +02:00
from cpl_core.console import Console
from cpl_core.dependency_injection import ServiceProviderABC
2022-07-16 19:47:04 +02:00
from cpl_core.logging import LoggerABC
from cpl_discord.application import DiscordBotApplicationABC
from cpl_discord.configuration import DiscordBotSettings
from cpl_discord.service import DiscordBotServiceABC, DiscordBotService
from cpl_translation import TranslatePipe, TranslationServiceABC, TranslationSettings
2022-07-14 16:32:46 +02:00
2022-10-02 12:14:44 +02:00
from bot_core.configuration.bot_logging_settings import BotLoggingSettings
from bot_core.configuration.bot_settings import BotSettings
from bot_core.configuration.server_settings import ServerSettings
2022-10-02 12:14:44 +02:00
from bot_core.logging.command_logger import CommandLogger
from bot_core.logging.event_logger import EventLogger
from modules.base.configuration.base_server_settings import BaseServerSettings
from modules.base.configuration.base_settings import BaseSettings
from modules.boot_log.configuration.boot_log_server_settings import BootLogServerSettings
from modules.boot_log.configuration.boot_log_settings import BootLogSettings
from modules.permission.configuration.permission_server_settings import PermissionServerSettings
from modules.permission.configuration.permission_settings import PermissionSettings
2022-07-14 16:32:46 +02:00
2022-07-16 19:47:04 +02:00
class Application(DiscordBotApplicationABC):
2022-07-14 16:32:46 +02:00
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
2022-07-16 19:47:04 +02:00
DiscordBotApplicationABC.__init__(self, config, services)
2022-10-02 12:14:44 +02:00
self._services = services
self._cmd_logger: Optional[LoggerABC] = None
self._event_logger: Optional[LoggerABC] = None
2022-07-16 19:47:04 +02:00
# cpl-core
self._logger: LoggerABC = services.get_service(LoggerABC)
# cpl-discord
self._bot: DiscordBotServiceABC = services.get_service(DiscordBotServiceABC)
self._bot_settings: DiscordBotSettings = config.get_configuration(DiscordBotSettings)
# cpl-translation
self._translation: TranslationServiceABC = services.get_service(TranslationServiceABC)
self._translate: TranslatePipe = services.get_service(TranslatePipe)
2022-07-14 16:32:46 +02:00
2022-10-02 12:14:44 +02:00
def _configure_settings_with_sub_settings(self, settings: Type, list_atr: Callable, atr: Callable):
settings: Optional[settings] = self._configuration.get_configuration(settings)
if settings is None:
return
2022-10-02 12:14:44 +02:00
for sub_settings in list_atr(settings):
self._logger.trace(__name__, f'Saved config: {type(sub_settings).__name__}_{atr(sub_settings)}')
self._configuration.add_configuration(f'{type(sub_settings).__name__}_{atr(sub_settings)}', sub_settings)
2022-07-14 16:32:46 +02:00
async def configure(self):
2022-07-16 19:47:04 +02:00
self._translation.load_by_settings(self._configuration.get_configuration(TranslationSettings))
2022-10-02 12:14:44 +02:00
self._configure_settings_with_sub_settings(BotSettings, lambda x: x.servers, lambda x: x.id)
self._configure_settings_with_sub_settings(BaseSettings, lambda x: x.servers, lambda x: x.id)
self._configure_settings_with_sub_settings(BootLogSettings, lambda x: x.servers, lambda x: x.id)
self._configure_settings_with_sub_settings(PermissionSettings, lambda x: x.servers, lambda x: x.id)
self._configure_settings_with_sub_settings(BotLoggingSettings, lambda x: x.files, lambda x: x.key)
# custom loggers need to be loaded AFTER config is loaded correctly
self._cmd_logger: LoggerABC = self._services.get_service(CommandLogger)
self._event_logger: LoggerABC = self._services.get_service(EventLogger)
2022-07-14 16:32:46 +02:00
async def main(self):
2022-07-16 19:47:04 +02:00
try:
self._logger.debug(__name__, f'Starting...\n')
self._logger.trace(__name__, f'Try to start {DiscordBotService.__name__}')
await self._bot.start_async()
2022-10-02 12:14:44 +02:00
await self._bot.stop_async()
2022-07-16 19:47:04 +02:00
except Exception as e:
self._logger.error(__name__, 'Start failed', e)
async def stop_async(self):
try:
self._logger.trace(__name__, f'Try to stop {DiscordBotService.__name__}')
await self._bot.close()
self._logger.trace(__name__, f'Stopped {DiscordBotService.__name__}')
except Exception as e:
self._logger.error(__name__, 'stop failed', e)
Console.write_line()
2022-07-19 11:58:33 +02:00
def is_restart(self):
return True if self._configuration.get_configuration('IS_RESTART') == 'true' else False