Added auto role remove #54

This commit is contained in:
Sven Heidemann 2022-10-03 18:27:07 +02:00
parent fa98f08161
commit d0ed413150
3 changed files with 46 additions and 3 deletions

View File

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

View File

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

View File

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