Improved message service & added logic to send login message

This commit is contained in:
2021-11-16 20:37:51 +01:00
parent c254114987
commit 5e1caf713c
4 changed files with 57 additions and 17 deletions

View File

@@ -4,17 +4,30 @@ from cpl_core.configuration import ConfigurationABC
from cpl_core.console import Console
from cpl_core.logging import LoggerABC, LoggingLevelEnum, LoggingSettings
from discord import guild
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings
from modules_core.abc.module_abc import ModuleABC
class BootLog(ModuleABC):
def __init__(self, config: ConfigurationABC, logging_st: LoggingSettings, logger: LoggerABC, bot: BotServiceABC):
def __init__(
self,
config: ConfigurationABC,
logging_st: LoggingSettings,
logger: LoggerABC,
bot: BotServiceABC,
message_service: MessageServiceABC
):
self._config = config
self._logging_st = logging_st
self._logger = logger
self._bot = bot
self._message_service = message_service
ModuleABC.__init__(self)
self._logger.trace(__name__, f'Module {type(self)} loaded')
@@ -27,26 +40,37 @@ class BootLog(ModuleABC):
self._logger.debug(__name__, f'Bot Init time: {init_time}s')
# print warning if initialisation took too long
if init_time >= 30:
self._logger.warn(__name__, 'It takes long time to start the bot!')
self._logger.warn(
__name__, 'It takes long time to start the bot!')
# print error if initialisation took way too long
elif init_time >= 90:
self._logger.error(__name__, 'It takes very long time to start the bot!!!')
self._logger.error(
__name__, 'It takes very long time to start the bot!!!')
except Exception as e:
self._logger.error(__name__, 'Init time calculation failed', e)
return
self._logger.header(f'{self._bot.user.name}:')
if self._logging_st.console.value >= LoggingLevelEnum.INFO.value:
Console.banner(self._bot.user.name)
for g in self._bot.guilds:
g: guild = g
self._logger.debug(__name__, f'Server detected: {g.id}')
server_config = self._config.get_configuration(f'DSERVER_{g.id}')
if server_config is None:
self._logger.error(__name__, f'Config for server {g.id} not found!')
server_settings: ServerSettings = self._config.get_configuration(
f'DSERVER_{g.id}')
if server_settings is None:
self._logger.error(
__name__, f'Config for server {g.id} not found!')
await self._bot.close()
return
await self._message_service.send_channel_message(
self._bot.get_channel(
server_settings.login_message_channel_id),
server_settings.login_message.format(init_time)
)
self._logger.trace(__name__, f'Module {type(self)} stopped')