forked from sh-edraft.de/sh_discord_bot
		
	Added channel param to auto-role add & fixed on reaction add event stuff #54
This commit is contained in:
		| @@ -9,19 +9,19 @@ | ||||
|     "Command": { | ||||
|       "Path": "logs/", | ||||
|       "Filename": "commands.log", | ||||
|       "ConsoleLogLevel": "DEBUG", | ||||
|       "ConsoleLogLevel": "TRACE", | ||||
|       "FileLogLevel": "TRACE" | ||||
|     }, | ||||
|     "Database": { | ||||
|       "Path": "logs/", | ||||
|       "Filename": "database.log", | ||||
|       "ConsoleLogLevel": "DEBUG", | ||||
|       "ConsoleLogLevel": "TRACE", | ||||
|       "FileLogLevel": "TRACE" | ||||
|     }, | ||||
|     "Message": { | ||||
|       "Path": "logs/", | ||||
|       "Filename": "message.log", | ||||
|       "ConsoleLogLevel": "DEBUG", | ||||
|       "ConsoleLogLevel": "TRACE", | ||||
|       "FileLogLevel": "TRACE" | ||||
|     } | ||||
|   }, | ||||
|   | ||||
| @@ -63,7 +63,7 @@ | ||||
|       "add": { | ||||
|         "success": "auto-role für die Nachricht {} wurde hinzugefügt :D", | ||||
|         "error": { | ||||
|           "not_found": "auto-role für die Nachricht {} nicht gefunden!", | ||||
|           "not_found": "Nachricht {} nicht in {} nicht gefunden!", | ||||
|           "already_exists": "auto-role für die Nachricht {} existiert bereits!" | ||||
|         } | ||||
|       }, | ||||
| @@ -88,7 +88,7 @@ | ||||
|           "success": "Regel {} -> {} für auto-role {} wurde hinzugefügt :D", | ||||
|           "error": { | ||||
|             "not_found": "Regel für auto-role {} nicht gefunden!", | ||||
|             "emoji_not_found": "Eomji {} für auto-role Regel {} nicht gefunden!", | ||||
|             "emoji_not_found": "Emoji {} für auto-role Regel {} nicht gefunden!", | ||||
|             "rule_not_found": "Rolle {} für auto-role Regel {} nicht gefunden!", | ||||
|             "already_exists": "Regel für auto-role {} existiert bereits!" | ||||
|           } | ||||
|   | ||||
| @@ -90,7 +90,7 @@ class AutoRoleGroup(DiscordCommandABC): | ||||
|  | ||||
|     @auto_role.command() | ||||
|     @commands.guild_only() | ||||
|     async def add(self, ctx: Context, message_id: str): | ||||
|     async def add(self, ctx: Context, channel: discord.TextChannel, message_id: str): | ||||
|         self._logger.debug(__name__, f'Received command auto-role add {ctx} {message_id}') | ||||
|         if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): | ||||
|             return | ||||
| @@ -100,10 +100,10 @@ class AutoRoleGroup(DiscordCommandABC): | ||||
|             self._logger.trace(__name__, f'Finished command auto-role add') | ||||
|             return | ||||
|  | ||||
|         message = List(discord.Message, [message async for message in ctx.channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default() | ||||
|         message = List(discord.Message, [message async for message in channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default() | ||||
|         if message is None: | ||||
|             self._logger.debug(__name__, f'Message with id  {message_id} not found in  {ctx.channel.name}') | ||||
|             await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.not_found').format(message_id)) | ||||
|             self._logger.debug(__name__, f'Message with id {message_id} not found in {channel.name}') | ||||
|             await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.not_found').format(message_id, channel.name)) | ||||
|             self._logger.trace(__name__, f'Finished command auto-role add') | ||||
|             return | ||||
|  | ||||
| @@ -123,6 +123,14 @@ class AutoRoleGroup(DiscordCommandABC): | ||||
|     @add.autocomplete('message_id') | ||||
|     async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: | ||||
|         channel = discord.utils.get(interaction.guild.text_channels, id=interaction.channel_id) | ||||
|         try: | ||||
|             channel_from_data = interaction.data['options'][0]['options'][0]['value'] | ||||
|             found_channel = discord.utils.get(interaction.guild.text_channels, id=int(channel_from_data)) | ||||
|             if found_channel is not None: | ||||
|                 channel = found_channel | ||||
|         finally: | ||||
|             pass | ||||
|  | ||||
|         messages = [message async for message in channel.history(limit=10)] | ||||
|         return [app_commands.Choice(name=f'{message.author}@{message.created_at}', value=str(message.id)) for message in messages if current in str(message.id)] | ||||
|  | ||||
| @@ -292,4 +300,5 @@ class AutoRoleGroup(DiscordCommandABC): | ||||
|     @remove.autocomplete('auto_role_rule') | ||||
|     async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: | ||||
|         rules = self._auto_roles.get_auto_role_rules() | ||||
|         return [app_commands.Choice(name=f'{rule.auto_role_rule_id} {rule.emoji_name} {interaction.guild.get_role(int(rule.role_id))}', value=rule.auto_role_rule_id) for rule in rules] | ||||
|         return [app_commands.Choice(name=f'{rule.auto_role_rule_id} {rule.emoji_name} {interaction.guild.get_role(int(rule.role_id))}', value=rule.auto_role_rule_id) for rule in | ||||
|                 rules] | ||||
|   | ||||
| @@ -28,20 +28,27 @@ class ReactionHandler: | ||||
|         self._roles = self._auto_roles.get_auto_roles() | ||||
|  | ||||
|     async def handle(self, payload: RawReactionActionEvent, r_type=None) -> None: | ||||
|         self._logger.trace(__name__, f'Handle reaction {payload} {r_type}') | ||||
|         if payload.message_id not in self._message_ids: | ||||
|             self._logger.debug(__name__, f'Message not in auto-roles - skipping') | ||||
|             return | ||||
|  | ||||
|         guild = self._bot.get_guild(payload.guild_id) | ||||
|         user = await guild.fetch_member(payload.user_id) | ||||
|         if user.bot: | ||||
|             self._logger.debug(__name__, f'User is bot - skipping') | ||||
|             return | ||||
|  | ||||
|         emoji = payload.emoji.name | ||||
|         auto_role: AutoRole = self._roles.where(lambda x: x.discord_message_id).first_or_default() | ||||
|         auto_role: AutoRole = self._roles.where(lambda x: x.discord_message_id == payload.message_id).single_or_default() | ||||
|         if auto_role is None: | ||||
|             self._logger.debug(__name__, f'auto-role for message not found - skipping') | ||||
|             return | ||||
|  | ||||
|         rules: List[AutoRoleRule] = self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role.auto_role_id) | ||||
|         if rules.count() == 0: | ||||
|             self._logger.debug(__name__, f'auto-role rules not found - skipping') | ||||
|             return | ||||
|  | ||||
|         for rule in rules: | ||||
|             if emoji != rule.emoji_name: | ||||
| @@ -51,8 +58,9 @@ class ReactionHandler: | ||||
|                 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': | ||||
|             elif 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) | ||||
|             else: | ||||
|                 self._logger.warn(__name__, f'Invalid reaction type {r_type}') | ||||
|   | ||||
		Reference in New Issue
	
	Block a user