Improved event handling

This commit is contained in:
Sven Heidemann 2021-11-23 16:46:20 +01:00
parent a5f710177e
commit 3806c36571
3 changed files with 18 additions and 22 deletions

View File

@ -19,5 +19,6 @@
"titleBar.inactiveForeground": "#15202b99"
},
"peacock.color": "#61dafb",
"python.pythonPath": "${workspaceRoot}/venv/bin/python",
"python.defaultInterpreterPath": "${workspaceRoot}/venv/bin/python",
}

View File

@ -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')

View File

@ -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