Improved event handling with commands.Cog.listener

This commit is contained in:
2021-11-22 20:08:46 +01:00
parent 9ebbd7ccf7
commit be810a6533
4 changed files with 37 additions and 58 deletions

View File

@@ -14,6 +14,3 @@ class BotServiceABC(ABC, commands.Bot):
@abstractmethod
async def stop_async(self): pass
@abstractmethod
async def on_message(self, message: discord.Message): pass

View File

@@ -8,11 +8,12 @@ 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
from modules_core.service.module_service import ModuleService
class BotService(BotServiceABC, commands.Bot):
def __init__(self, config: ConfigurationABC, logger: LoggerABC, modules: ModuleServiceABC, discord_settings: DiscordSettings, bot_settings: BotSettings):
def __init__(self, config: ConfigurationABC, logger: LoggerABC, modules: ModuleService, discord_settings: DiscordSettings, bot_settings: BotSettings):
# services
self._config = config
self._logger = logger
@@ -27,6 +28,9 @@ class BotService(BotServiceABC, commands.Bot):
async def start_async(self):
self._logger.trace(__name__, 'Try to connect to discord')
self.add_cog(self._modules)
await self.start(self._discord_settings.token)
# continue at on_ready
@@ -50,7 +54,3 @@ class BotService(BotServiceABC, commands.Bot):
await self._modules.on_ready()
async def on_message(self, message: discord.Message):
self._logger.debug(__name__, f'Received message:\n{message}:\n{message.content}')
await self._modules.on_message(message)