Improved bot startup logic
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import traceback
|
||||
|
||||
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl_core.configuration import ConfigurationModelABC
|
||||
from cpl_core.console import Console
|
||||
from cpl_query.extension import List
|
||||
from gismo_core.configuration.server_settings import ServerSettings
|
||||
|
||||
|
||||
class BotSettings(ConfigurationModelABC):
|
||||
@@ -10,14 +12,25 @@ class BotSettings(ConfigurationModelABC):
|
||||
ConfigurationModelABC.__init__(self)
|
||||
|
||||
self._prefix: str = ''
|
||||
self._servers: List[ServerSettings] = List()
|
||||
|
||||
@property
|
||||
def prefix(self) -> str:
|
||||
return self._prefix
|
||||
|
||||
@property
|
||||
def servers(self) -> List[ServerSettings]:
|
||||
return self._servers
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
self._prefix = settings['Prefix']
|
||||
servers = List(ServerSettings)
|
||||
for s in settings['Servers']:
|
||||
st = ServerSettings()
|
||||
st.from_dict(s)
|
||||
servers.append(st)
|
||||
self._servers = servers
|
||||
except Exception as e:
|
||||
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
|
||||
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in settings')
|
||||
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import traceback
|
||||
|
||||
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl_core.configuration import ConfigurationModelABC
|
||||
from cpl_core.console import Console
|
||||
|
||||
|
||||
|
23
src/gismo_core/configuration/server_settings.py
Normal file
23
src/gismo_core/configuration/server_settings.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import traceback
|
||||
|
||||
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl_core.console import Console
|
||||
|
||||
|
||||
class ServerSettings(ConfigurationModelABC):
|
||||
|
||||
def __init__(self):
|
||||
ConfigurationModelABC.__init__(self)
|
||||
|
||||
self._id: int = ''
|
||||
|
||||
@property
|
||||
def id(self) -> str:
|
||||
return self._id
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
self._id = settings['Id']
|
||||
except Exception as e:
|
||||
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in settings')
|
||||
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
@@ -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)
|
||||
|
||||
|
Reference in New Issue
Block a user