Improved bot startup logic

This commit is contained in:
2021-11-16 18:34:41 +01:00
parent 2176037d08
commit d2c233a855
12 changed files with 80 additions and 24 deletions

View File

@@ -1,16 +1,18 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from discord.ext import commands
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.configuration.bot_settings import BotSettings
from gismo_core.configuration.discord_settings import DiscordSettings
from gismo_core.configuration.server_settings import ServerSettings
from modules_core.abc.module_service_abc import ModuleServiceABC
class BotService(BotServiceABC, commands.Bot):
def __init__(self, logger: LoggerABC, modules: ModuleServiceABC, discord_settings: DiscordSettings, bot_settings: BotSettings):
def __init__(self, config: ConfigurationABC, logger: LoggerABC, modules: ModuleServiceABC, discord_settings: DiscordSettings, bot_settings: BotSettings):
# services
self._config = config
self._logger = logger
self._modules = modules
@@ -23,12 +25,21 @@ class BotService(BotServiceABC, commands.Bot):
async def start_async(self):
self._logger.trace(__name__, 'Try to connect to discord')
self.run(self._discord_settings.token)
await self.start(self._discord_settings.token)
# continue at on_ready
async def on_ready(self):
self._logger.info(__name__, 'Connected to discord')
await self._modules.start_modules()
self._logger.debug(__name__, 'Try to load discord server configs')
for server in self._bot_settings.servers:
server: ServerSettings = server
self._logger.trace(__name__, f'Try to load config for server: {server.id}')
self._config.add_configuration(f'DSERVER_{server.id}', server)
self._logger.trace(__name__, f'Loaded config for server: {server.id}')
await self._modules.on_ready()
async def stop_async(self):
self._logger.debug(__name__, f'Try to stop {BotService}')
@@ -37,4 +48,3 @@ class BotService(BotServiceABC, commands.Bot):
# save data
except Exception as e:
self._logger.error(__name__, 'Stop failed', e)