From 5a91e31964682ff531947a162fc1337029a3fa16 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 22 Nov 2021 21:16:09 +0100 Subject: [PATCH] Improved event listeners --- src/modules_core/service/module_service.py | 75 +++++++++++++++++++--- 1 file changed, 65 insertions(+), 10 deletions(-) diff --git a/src/modules_core/service/module_service.py b/src/modules_core/service/module_service.py index 57bd415..ab4d816 100644 --- a/src/modules_core/service/module_service.py +++ b/src/modules_core/service/module_service.py @@ -1,17 +1,19 @@ from abc import ABC, ABCMeta from datetime import datetime from typing import Optional, Sequence, Union -import discord -from discord.ext import commands +import discord 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 discord.ext import commands +from modules_core.abc.events.on_connect_abc import OnConnectABC +from modules_core.abc.events.on_disconnect_abc import OnDisconnectABC from modules_core.abc.events.on_message_abc import OnMessageABC from modules_core.abc.events.on_ready_abc import OnReadyABC +from modules_core.abc.module_abc import ModuleABC +from modules_core.abc.module_service_abc import ModuleServiceABC class _MetaCogABC(ABCMeta, commands.CogMeta): pass @@ -34,9 +36,21 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC): return modules.order_by(lambda m: m.get_priority(t)) - async def on_connect(self): pass + @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') - async def on_disconnect(self): pass + @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') async def on_ready(self): self._logger.debug(__name__, f'Start on_ready modules') @@ -45,10 +59,13 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC): self._logger.debug(__name__, 'Stopped on_ready modules') + @commands.Cog.listener() async def on_resume(self): pass + @commands.Cog.listener() async def on_error(self, event: str, *args, **kwargs): pass + @commands.Cog.listener() async def on_typing(self, channel: discord.abc.Messageable, user: Union[discord.User, discord.Member], when: datetime): pass @commands.Cog.listener() @@ -61,84 +78,122 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC): await module.on_message(message) self._logger.debug(__name__, f'Stopped on_message modules') + @commands.Cog.listener() async def on_message_delete(self, message: discord.Message): pass + @commands.Cog.listener() async def on_bulk_message_delete(self, message: discord.Message): pass + @commands.Cog.listener() async def on_message_edit(self, before: discord.Message, after: discord.Message): pass + @commands.Cog.listener() async def on_reaction_add(self, reaction: discord.Reaction, user: discord.User): pass + @commands.Cog.listener() async def on_reaction_remove(self, reaction: discord.Reaction, user: discord.User): pass + @commands.Cog.listener() async def on_reaction_clear(self, message: discord.Message, reactions: list[discord.Reaction]): pass + @commands.Cog.listener() async def on_reaction_clear_emoji(self, reaction: discord.Reaction): pass + @commands.Cog.listener() async def on_private_channel_delete(self, channel: discord.abc.PrivateChannel): pass + @commands.Cog.listener() async def on_private_channel_create(self, channel: discord.abc.PrivateChannel): pass + @commands.Cog.listener() async def on_private_channel_update(self, before: discord.GroupChannel, after: discord.GroupChannel): pass + @commands.Cog.listener() async def on_private_channel_pins_update(self, channel: discord.abc.PrivateChannel, list_pin: Optional[datetime]): pass + @commands.Cog.listener() async def on_guild_channel_delete(self, channel: discord.abc.GuildChannel): pass + @commands.Cog.listener() async def on_guild_channel_create(self, channel: discord.abc.GuildChannel): pass + @commands.Cog.listener() async def on_guild_channel_update(self, before: discord.abc.GuildChannel, after: discord.abc.GuildChannel): pass + @commands.Cog.listener() async def on_guild_channel_pins_update(self, channel: discord.abc.GuildChannel, list_pin: Optional[datetime]): pass + @commands.Cog.listener() async def on_guild_integrations_update(self, guild: discord.Guild): pass + @commands.Cog.listener() async def on_webhooks_update(self, channel: discord.abc.GuildChannel): pass + @commands.Cog.listener() async def on_member_join(self, member: discord.Member): pass + @commands.Cog.listener() async def on_member_remove(self, member: discord.Member): pass + @commands.Cog.listener() async def on_member_update(self, before: discord.Member, after: discord.Member): pass + @commands.Cog.listener() async def on_user_update(self, before: discord.User, after: discord.User): pass + @commands.Cog.listener() async def on_guild_join(self, guild: discord.Guild): pass + @commands.Cog.listener() async def on_guild_remove(self, guild: discord.Guild): pass + @commands.Cog.listener() async def on_guild_update(self, before: discord.Guild, after: discord.Guild): pass + @commands.Cog.listener() async def on_guild_role_create(self, role: discord.Role): pass + @commands.Cog.listener() async def on_guild_role_delete(self, role: discord.Role): pass + @commands.Cog.listener() async def on_guild_role_update(self, before: discord.Role, after: discord.Role): pass + @commands.Cog.listener() async def on_guild_emojis_update(self, guild: discord.Guild, before: Sequence[discord.Emoji], after: Sequence[discord.Emoji]): pass + @commands.Cog.listener() async def on_guild_available(self, guild: discord.Guild): pass + @commands.Cog.listener() async def on_guild_unavailable(self, guild: discord.Guild): pass + @commands.Cog.listener() async def on_voice_state_update(self, member: discord.Member, before: discord.VoiceState, after: discord.VoiceState): pass + @commands.Cog.listener() async def on_member_ban(self, guild: discord.Guild, user: discord.User): pass + @commands.Cog.listener() async def on_member_unban(self, guild: discord.Guild, user: discord.User): pass + @commands.Cog.listener() async def on_invite_create(self, invite: discord.Invite): pass + @commands.Cog.listener() async def on_invite_delete(self, invite: discord.Invite): pass - async def on_group_join( - self, chhanel: discord.GroupChannel, user: discord.User): pass + @commands.Cog.listener() + async def on_group_join(self, chhanel: discord.GroupChannel, user: discord.User): pass + @commands.Cog.listener() async def on_group_remove(self, chhanel: discord.GroupChannel, user: discord.User): pass - async def on_relationship_add( - self, relationship: discord.Relationship): pass + @commands.Cog.listener() + async def on_relationship_add(self, relationship: discord.Relationship): pass + @commands.Cog.listener() async def on_relationship_remove(self, relationship: discord.Relationship): pass + @commands.Cog.listener() async def on_relationship_update(self, before: discord.Relationship, after: discord.Relationship): pass