From 3806c36571bcd4fb47a12d56ff287b86bdb1d2af Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 23 Nov 2021 16:46:20 +0100 Subject: [PATCH] Improved event handling --- .vscode/settings.json | 1 + src/modules/boot_log/boot_log.py | 4 ++- src/modules_core/service/module_service.py | 35 +++++++++------------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index e8a8578..4e78d7c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,5 +19,6 @@ "titleBar.inactiveForeground": "#15202b99" }, "peacock.color": "#61dafb", + "python.pythonPath": "${workspaceRoot}/venv/bin/python", "python.defaultInterpreterPath": "${workspaceRoot}/venv/bin/python", } \ No newline at end of file diff --git a/src/modules/boot_log/boot_log.py b/src/modules/boot_log/boot_log.py index 587c743..4d72b69 100644 --- a/src/modules/boot_log/boot_log.py +++ b/src/modules/boot_log/boot_log.py @@ -1,8 +1,10 @@ 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 @@ -72,4 +74,4 @@ class BootLog(ModuleABC, OnReadyABC): server_settings.login_message.format(init_time) ) - self._logger.trace(__name__, f'Module {type(self)} stopped') + self._logger.trace(__name__, f'Module {type(self)} stopped') \ No newline at end of file diff --git a/src/modules_core/service/module_service.py b/src/modules_core/service/module_service.py index ab4d816..5fa4b74 100644 --- a/src/modules_core/service/module_service.py +++ b/src/modules_core/service/module_service.py @@ -6,6 +6,7 @@ import discord from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.environment import ApplicationEnvironmentABC from cpl_core.logging import LoggerABC +from cpl_core.utils import String from cpl_query.extension import List from discord.ext import commands from modules_core.abc.events.on_connect_abc import OnConnectABC @@ -36,28 +37,25 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC): return modules.order_by(lambda m: m.get_priority(t)) + async def _handle_event(self, event: type, *args): + self._logger.debug(__name__, f'Start {event} modules') + func_name = String.convert_to_snake_case(event.__name__.split('ABC')[0]) + for module in self._get_modules(event): + func = getattr(module, func_name) + await func(*args) + + self._logger.debug(__name__, f'Stopped {event} modules') + @commands.Cog.listener() async def on_connect(self): - self._logger.debug(__name__, f'Start on_connect modules') - for module in self._get_modules(OnConnectABC): - await module.on_connect() - - self._logger.debug(__name__, 'Stopped on_connect modules') + await self._handle_event(OnConnectABC) @commands.Cog.listener() async def on_disconnect(self): - self._logger.debug(__name__, f'Start on_disconnect modules') - for module in self._get_modules(OnDisconnectABC): - await module.on_disconnect() - - self._logger.debug(__name__, 'Stopped on_disconnect modules') + await self._handle_event(OnDisconnectABC) async def on_ready(self): - self._logger.debug(__name__, f'Start on_ready modules') - for module in self._get_modules(OnReadyABC): - await module.on_ready() - - self._logger.debug(__name__, 'Stopped on_ready modules') + await self._handle_event(OnReadyABC) @commands.Cog.listener() async def on_resume(self): pass @@ -71,12 +69,7 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC): @commands.Cog.listener() async def on_message(self, message: discord.Message): self._logger.debug(__name__, f'Received message:\n{message}:\n{message.content}') - self._logger.debug(__name__, 'Start on_message modules') - modules = self._modules.where(lambda m: issubclass(m, OnMessageABC)) - for module_type in modules: - module: OnMessageABC = self._services.get_service(module_type) - await module.on_message(message) - self._logger.debug(__name__, f'Stopped on_message modules') + await self._handle_event(OnMessageABC, message) @commands.Cog.listener() async def on_message_delete(self, message: discord.Message): pass