From 17befd9f050421035566735bfafd5f18347bdad9 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Oct 2022 22:24:20 +0200 Subject: [PATCH] Added auto-role rule remove command #54 --- src/bot/translation/de.json | 2 +- .../auto_role/command/auto_role_group.py | 42 ++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index 6fa2fc2d..927c6b57 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -80,7 +80,7 @@ "list": { "title": "auto-role Regeln:", "description": "Von auto-role angewendete Regeln:", - "auto_role_id": "auto-role Id", + "auto_role_rule_id": "auto-role Regel Id", "emoji": "Emoji", "role": "Rolle" }, diff --git a/src/modules/auto_role/command/auto_role_group.py b/src/modules/auto_role/command/auto_role_group.py index 18bc5242..c26210db 100644 --- a/src/modules/auto_role/command/auto_role_group.py +++ b/src/modules/auto_role/command/auto_role_group.py @@ -108,7 +108,7 @@ class AutoRoleGroup(DiscordCommandABC): return if self._auto_roles.find_auto_roles_by_message_id(int(message_id)) is not None: - self._logger.debug(__name__, f'AutoRole for message {message_id} already exists') + self._logger.debug(__name__, f'auto-role for message {message_id} already exists') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.already_exists').format(message_id)) self._logger.trace(__name__, f'Finished command auto-role add') return @@ -116,7 +116,7 @@ class AutoRoleGroup(DiscordCommandABC): server_id = self._servers.get_server_by_discord_id(ctx.guild.id).server_id self._auto_roles.add_auto_role(AutoRole(server_id, int(message_id))) self._db_context.save_changes() - self._logger.info(__name__, f'Saved AutoRole for message {message_id} at server {server_id}') + self._logger.info(__name__, f'Saved auto-role for message {message_id} at server {server_id}') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.success').format(message_id)) self._logger.trace(__name__, f'Finished command auto-role add') @@ -140,18 +140,18 @@ class AutoRoleGroup(DiscordCommandABC): auto_role_from_db = self._auto_roles.find_auto_role_by_id(auto_role) if auto_role_from_db is None: - self._logger.debug(__name__, f'AutoRole {auto_role} not found') + self._logger.debug(__name__, f'auto-role {auto_role} not found') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.error.not_found').format(auto_role)) self._logger.trace(__name__, f'Finished command auto-role remove') return for rule in self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_from_db.auto_role_id): self._auto_roles.delete_auto_role_rule(rule) - self._logger.info(__name__, f'Removed AutoRole rule {rule.role_id}') + self._logger.info(__name__, f'Removed auto-role rule {rule.role_id}') self._auto_roles.delete_auto_role(auto_role_from_db) self._db_context.save_changes() - self._logger.info(__name__, f'Removed AutoRole {auto_role}') + self._logger.info(__name__, f'Removed auto-role {auto_role}') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.success').format(auto_role)) self._logger.trace(__name__, f'Finished command auto-role remove') @@ -196,7 +196,7 @@ class AutoRoleGroup(DiscordCommandABC): emoji += f'\n{rule.emoji_name}' role += f'\n{ctx.guild.get_role(rule.role_id)}' - embed.add_field(name=self._t.transform('modules.auto_role.rule.list.auto_role_id'), value=auto_role_rule_id, inline=True) + embed.add_field(name=self._t.transform('modules.auto_role.rule.list.auto_role_rule_id'), value=auto_role_rule_id, inline=True) embed.add_field(name=self._t.transform('modules.auto_role.rule.list.emoji'), value=emoji, inline=True) embed.add_field(name=self._t.transform('modules.auto_role.rule.list.role'), value=role, inline=True) await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) @@ -263,3 +263,33 @@ class AutoRoleGroup(DiscordCommandABC): async def rule_add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: roles = interaction.guild.roles return [app_commands.Choice(name=role.name, value=str(role.id)) for role in roles] + + @rule.command() + @commands.guild_only() + async def remove(self, ctx: Context, auto_role_rule: int): + self._logger.debug(__name__, f'Received command auto-role remove {ctx} {auto_role_rule}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + + if not self._permissions.is_member_moderator(ctx.author): + await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) + self._logger.trace(__name__, f'Finished command auto-role remove') + return + + auto_role_from_db = self._auto_roles.get_auto_role_rule_by_id(auto_role_rule) + if auto_role_from_db is None: + self._logger.debug(__name__, f'auto-role rule {auto_role_rule} not found') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.error.not_found').format(auto_role_rule)) + self._logger.trace(__name__, f'Finished command auto-role rule remove') + return + + self._auto_roles.delete_auto_role_rule(auto_role_from_db) + self._db_context.save_changes() + self._logger.info(__name__, f'Removed auto-role rule {auto_role_rule}') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.remove.success').format(auto_role_rule)) + self._logger.trace(__name__, f'Finished command auto-role remove') + + @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]