This repository has been archived on 2022-07-14. You can view files and clone it, but cannot push or open issues or pull requests.
sh_gismo/src/modules/boot_log/boot_log.py

77 lines
2.7 KiB
Python

from datetime import datetime
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
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.events.on_ready_abc import OnReadyABC
from modules_core.abc.module_abc import ModuleABC
class BootLog(ModuleABC, OnReadyABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
bot: BotServiceABC,
message_service: MessageServiceABC
):
self._config = config
self._logger = logger
self._bot = bot
self._message_service = message_service
self._priorities = {
OnReadyABC: 10
}
self._logger.trace(__name__, f'Module {type(self)} loaded')
def get_priority(self, t: type) -> int:
return self._priorities[t]
async def on_ready(self):
self._logger.debug(__name__, f'Module {type(self)} started')
try:
start_time = self._config.get_configuration('Bot_StartTime')
init_time = round((datetime.now() - start_time).total_seconds(), 2)
self._config.add_configuration('InitTime', init_time)
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!')
# 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!!!')
except Exception as e:
self._logger.error(__name__, 'Init time calculation failed', e)
return
for g in self._bot.guilds:
g: guild = g
self._logger.debug(__name__, f'Server detected: {g.id}')
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')