From 12492cf596eaa4363e615b9555097510a0658346 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 16 Nov 2021 21:01:16 +0100 Subject: [PATCH] Added on_message event --- src/gismo_core/service/bot_service.py | 24 ++++++++++++++-------- src/modules_core/abc/module_service_abc.py | 6 +++++- src/modules_core/service/module_service.py | 9 +++++++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/gismo_core/service/bot_service.py b/src/gismo_core/service/bot_service.py index 98f7045..2f26e86 100644 --- a/src/gismo_core/service/bot_service.py +++ b/src/gismo_core/service/bot_service.py @@ -1,6 +1,8 @@ +import discord +from discord.ext import commands + 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 @@ -27,6 +29,14 @@ class BotService(BotServiceABC, commands.Bot): self._logger.trace(__name__, 'Try to connect to discord') await self.start(self._discord_settings.token) # continue at on_ready + + async def stop_async(self): + self._logger.debug(__name__, f'Try to stop {BotService}') + try: + pass + # save data + except Exception as e: + self._logger.error(__name__, 'Stop failed', e) async def on_ready(self): self._logger.info(__name__, 'Connected to discord') @@ -40,11 +50,7 @@ class BotService(BotServiceABC, commands.Bot): await self._modules.on_ready() - - async def stop_async(self): - self._logger.debug(__name__, f'Try to stop {BotService}') - try: - pass - # save data - except Exception as e: - self._logger.error(__name__, 'Stop failed', e) + + 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) diff --git a/src/modules_core/abc/module_service_abc.py b/src/modules_core/abc/module_service_abc.py index 543f2b9..51bc8a0 100644 --- a/src/modules_core/abc/module_service_abc.py +++ b/src/modules_core/abc/module_service_abc.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod -from codecs import register + +import discord class ModuleServiceABC(ABC): @@ -9,3 +10,6 @@ class ModuleServiceABC(ABC): @abstractmethod async def on_ready(self): pass + + @abstractmethod + async def on_message(self, message: discord.Message): pass diff --git a/src/modules_core/service/module_service.py b/src/modules_core/service/module_service.py index 63c1bf7..94a4e17 100644 --- a/src/modules_core/service/module_service.py +++ b/src/modules_core/service/module_service.py @@ -1,9 +1,13 @@ +import discord +from discord.ext import commands + from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.environment import ApplicationEnvironmentABC from cpl_core.logging import LoggerABC from cpl_query.extension import List from modules_core.abc.module_abc import ModuleABC from modules_core.abc.module_service_abc import ModuleServiceABC +from modules_core.events_enum import EventsEnum class ModuleService(ModuleServiceABC): @@ -17,7 +21,10 @@ class ModuleService(ModuleServiceABC): async def on_ready(self): self._logger.debug(__name__, 'Start on_ready modules') - modules = self._modules.where(lambda m: hasattr(m, 'on_ready') and callable(m.on_ready)) + modules = self._modules.where(lambda m: hasattr(m, EventsEnum.on_ready.value) and callable(m.on_ready)) for module_type in modules: module = self._services.get_service(module_type) await module.on_ready() + + async def on_message(self, message: discord.Message): + pass \ No newline at end of file