From d0ed413150af09f5c6722bd1035f052061a71caa Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 18:27:07 +0200 Subject: [PATCH] Added auto role remove #54 --- src/modules/autorole/auto_role_module.py | 2 ++ .../auto_role_on_raw_reaction_remove.py | 35 +++++++++++++++++++ .../autorole/helper/reaction_handler.py | 12 +++++-- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/modules/autorole/events/auto_role_on_raw_reaction_remove.py diff --git a/src/modules/autorole/auto_role_module.py b/src/modules/autorole/auto_role_module.py index c8f83990c1..3206b7e8f9 100644 --- a/src/modules/autorole/auto_role_module.py +++ b/src/modules/autorole/auto_role_module.py @@ -7,6 +7,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from modules.autorole.events.auto_role_on_raw_reaction_add import AutoRoleOnRawReactionAddEvent +from modules.autorole.events.auto_role_on_raw_reaction_remove import AutoRoleOnRawReactionRemoveEvent from modules.autorole.helper.reaction_handler import ReactionHandler @@ -23,3 +24,4 @@ class AutoRoleModule(ModuleABC): # commands # events self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, AutoRoleOnRawReactionAddEvent) + self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_remove.value, AutoRoleOnRawReactionRemoveEvent) diff --git a/src/modules/autorole/events/auto_role_on_raw_reaction_remove.py b/src/modules/autorole/events/auto_role_on_raw_reaction_remove.py new file mode 100644 index 0000000000..fbd8682243 --- /dev/null +++ b/src/modules/autorole/events/auto_role_on_raw_reaction_remove.py @@ -0,0 +1,35 @@ +from cpl_core.logging import LoggerABC +from cpl_discord.events.on_raw_reaction_add_abc import OnRawReactionAddABC +from cpl_discord.events.on_raw_reaction_remove_abc import OnRawReactionRemoveABC +from cpl_discord.service import DiscordBotServiceABC +from discord import RawReactionActionEvent + +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from modules.autorole.helper.reaction_handler import ReactionHandler + + +class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC): + + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC, + reaction_handler: ReactionHandler + ): + OnRawReactionRemoveABC.__init__(self) + + self._logger = logger + self._bot = bot + self._servers = servers + self._auto_roles = auto_roles + self._reaction_handler = reaction_handler + + async def on_raw_reaction_remove(self, payload: RawReactionActionEvent): + self._logger.debug(__name__, f'Module {type(self)} started') + + await self._reaction_handler.handle(payload, 'remove') + + self._logger.debug(__name__, f'Module {type(self)} stopped') diff --git a/src/modules/autorole/helper/reaction_handler.py b/src/modules/autorole/helper/reaction_handler.py index 528a8968a1..383044cbb4 100644 --- a/src/modules/autorole/helper/reaction_handler.py +++ b/src/modules/autorole/helper/reaction_handler.py @@ -47,6 +47,12 @@ class ReactionHandler: if emoji != rule.emoji_name: continue - role = guild.get_role(rule.role_id) - self._logger.debug(__name__, f'Assign role {role.name} to {user.name}') - await user.add_roles(role) + if r_type == 'add': + role = guild.get_role(rule.role_id) + self._logger.debug(__name__, f'Assign role {role.name} to {user.name}') + await user.add_roles(role) + + if r_type == 'remove': + role = guild.get_role(rule.role_id) + self._logger.debug(__name__, f'Remove role {role.name} to {user.name}') + await user.remove_roles(role)