Improved event handling
This commit is contained in:
parent
a5f710177e
commit
3806c36571
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@ -19,5 +19,6 @@
|
|||||||
"titleBar.inactiveForeground": "#15202b99"
|
"titleBar.inactiveForeground": "#15202b99"
|
||||||
},
|
},
|
||||||
"peacock.color": "#61dafb",
|
"peacock.color": "#61dafb",
|
||||||
|
"python.pythonPath": "${workspaceRoot}/venv/bin/python",
|
||||||
"python.defaultInterpreterPath": "${workspaceRoot}/venv/bin/python",
|
"python.defaultInterpreterPath": "${workspaceRoot}/venv/bin/python",
|
||||||
}
|
}
|
@ -1,8 +1,10 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
import discord
|
||||||
from cpl_core.configuration import ConfigurationABC
|
from cpl_core.configuration import ConfigurationABC
|
||||||
from cpl_core.logging import LoggerABC
|
from cpl_core.logging import LoggerABC
|
||||||
from discord import guild
|
from discord import guild
|
||||||
|
|
||||||
from gismo_core.abc.bot_service_abc import BotServiceABC
|
from gismo_core.abc.bot_service_abc import BotServiceABC
|
||||||
from gismo_core.abc.message_service_abc import MessageServiceABC
|
from gismo_core.abc.message_service_abc import MessageServiceABC
|
||||||
from gismo_core.configuration.server_settings import ServerSettings
|
from gismo_core.configuration.server_settings import ServerSettings
|
||||||
@ -72,4 +74,4 @@ class BootLog(ModuleABC, OnReadyABC):
|
|||||||
server_settings.login_message.format(init_time)
|
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')
|
@ -6,6 +6,7 @@ import discord
|
|||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
from cpl_core.environment import ApplicationEnvironmentABC
|
from cpl_core.environment import ApplicationEnvironmentABC
|
||||||
from cpl_core.logging import LoggerABC
|
from cpl_core.logging import LoggerABC
|
||||||
|
from cpl_core.utils import String
|
||||||
from cpl_query.extension import List
|
from cpl_query.extension import List
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from modules_core.abc.events.on_connect_abc import OnConnectABC
|
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))
|
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()
|
@commands.Cog.listener()
|
||||||
async def on_connect(self):
|
async def on_connect(self):
|
||||||
self._logger.debug(__name__, f'Start on_connect modules')
|
await self._handle_event(OnConnectABC)
|
||||||
for module in self._get_modules(OnConnectABC):
|
|
||||||
await module.on_connect()
|
|
||||||
|
|
||||||
self._logger.debug(__name__, 'Stopped on_connect modules')
|
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_disconnect(self):
|
async def on_disconnect(self):
|
||||||
self._logger.debug(__name__, f'Start on_disconnect modules')
|
await self._handle_event(OnDisconnectABC)
|
||||||
for module in self._get_modules(OnDisconnectABC):
|
|
||||||
await module.on_disconnect()
|
|
||||||
|
|
||||||
self._logger.debug(__name__, 'Stopped on_disconnect modules')
|
|
||||||
|
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
self._logger.debug(__name__, f'Start on_ready modules')
|
await self._handle_event(OnReadyABC)
|
||||||
for module in self._get_modules(OnReadyABC):
|
|
||||||
await module.on_ready()
|
|
||||||
|
|
||||||
self._logger.debug(__name__, 'Stopped on_ready modules')
|
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_resume(self): pass
|
async def on_resume(self): pass
|
||||||
@ -71,12 +69,7 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC):
|
|||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_message(self, message: discord.Message):
|
async def on_message(self, message: discord.Message):
|
||||||
self._logger.debug(__name__, f'Received message:\n{message}:\n{message.content}')
|
self._logger.debug(__name__, f'Received message:\n{message}:\n{message.content}')
|
||||||
self._logger.debug(__name__, 'Start on_message modules')
|
await self._handle_event(OnMessageABC, message)
|
||||||
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')
|
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_message_delete(self, message: discord.Message): pass
|
async def on_message_delete(self, message: discord.Message): pass
|
||||||
|
Reference in New Issue
Block a user