From b75777a339d26c69966a6947ce1789dd7f5f7e40 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Oct 2022 21:04:02 +0200 Subject: [PATCH] Added auto-role remove command & some refactoring #54 --- src/bot/translation/de.json | 6 ++ src/bot_data/abc/auto_role_repository_abc.py | 16 ++--- src/bot_data/model/auto_role_rule.py | 12 +++- .../service/auto_role_repository_service.py | 16 +++++ .../auto_role/command/auto_role_group.py | 59 ++++++++++++++++++- .../base/events/base_on_command_event.py | 6 +- 6 files changed, 100 insertions(+), 15 deletions(-) diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index ee37a939..c7b0ce63 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -67,6 +67,12 @@ "already_exists": "auto-role für die Nachricht {} existiert bereits!" } }, + "remove": { + "success": "auto-role {} wurde entfernt :D", + "error": { + "not_found": "auto-role {} nicht gefunden!" + } + }, "rule": { "list": { "title": "auto-role Regeln:", diff --git a/src/bot_data/abc/auto_role_repository_abc.py b/src/bot_data/abc/auto_role_repository_abc.py index 7b9c0ed1..14422fc5 100644 --- a/src/bot_data/abc/auto_role_repository_abc.py +++ b/src/bot_data/abc/auto_role_repository_abc.py @@ -5,7 +5,6 @@ from cpl_query.extension import List from bot_data.model.auto_role import AutoRole from bot_data.model.auto_role_rule import AutoRoleRule -from bot_data.model.server import Server class AutoRoleRepositoryABC(ABC): @@ -19,6 +18,9 @@ class AutoRoleRepositoryABC(ABC): @abstractmethod def get_auto_role_by_id(self, id: int) -> AutoRole: pass + @abstractmethod + def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]: pass + @abstractmethod def get_auto_roles_by_server_id(self, id: int) -> AutoRole: pass @@ -32,13 +34,13 @@ class AutoRoleRepositoryABC(ABC): def find_auto_roles_by_message_id(self, id: int) -> Optional[AutoRole]: pass @abstractmethod - def add_auto_role(self, server: AutoRole): pass + def add_auto_role(self, auto_role: AutoRole): pass @abstractmethod - def update_auto_role(self, server: AutoRole): pass + def update_auto_role(self, auto_role: AutoRole): pass @abstractmethod - def delete_auto_role(self, server: AutoRole): pass + def delete_auto_role(self, auto_role: AutoRole): pass @abstractmethod def get_auto_role_rules(self) -> List[AutoRoleRule]: pass @@ -50,10 +52,10 @@ class AutoRoleRepositoryABC(ABC): def get_auto_role_rules_by_auto_role_id(self, id: int) -> List[AutoRoleRule]: pass @abstractmethod - def add_auto_role_rule(self, server: AutoRoleRule): pass + def add_auto_role_rule(self, auto_role: AutoRoleRule): pass @abstractmethod - def update_auto_role_rule(self, server: AutoRoleRule): pass + def update_auto_role_rule(self, auto_role: AutoRoleRule): pass @abstractmethod - def delete_auto_role_rule(self, server: AutoRoleRule): pass + def delete_auto_role_rule(self, auto_role: AutoRoleRule): pass diff --git a/src/bot_data/model/auto_role_rule.py b/src/bot_data/model/auto_role_rule.py index ca9fce32..5fe3114e 100644 --- a/src/bot_data/model/auto_role_rule.py +++ b/src/bot_data/model/auto_role_rule.py @@ -17,11 +17,19 @@ class AutoRoleRule(TableABC): self._modified_at = modified_at if modified_at is not None else self._modified_at @property - def emoji_name(self): + def auto_role_rule_id(self) -> int: + return self._auto_role_rule_id + + @property + def auto_role_id(self) -> int: + return self._auto_role_id + + @property + def emoji_name(self) -> str: return self._discord_emoji_name @property - def role_id(self): + def role_id(self) -> int: return self._discord_role_id @staticmethod diff --git a/src/bot_data/service/auto_role_repository_service.py b/src/bot_data/service/auto_role_repository_service.py index 7b02bc72..0810f858 100644 --- a/src/bot_data/service/auto_role_repository_service.py +++ b/src/bot_data/service/auto_role_repository_service.py @@ -43,6 +43,22 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): id=result[0] ) + def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]: + self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_id_string(id)}') + result = self._context.select(AutoRole.get_select_by_id_string(id)) + if result is None or len(result) == 0: + return None + + result = result[0] + + return AutoRole( + result[1], + result[2], + result[3], + result[4], + id=result[0] + ) + def get_auto_roles_by_server_id(self, id: int) -> AutoRole: self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_server_id_string(id)}') result = self._context.select(AutoRole.get_select_by_server_id_string(id))[0] diff --git a/src/modules/auto_role/command/auto_role_group.py b/src/modules/auto_role/command/auto_role_group.py index 9f5e86c4..549d9030 100644 --- a/src/modules/auto_role/command/auto_role_group.py +++ b/src/modules/auto_role/command/auto_role_group.py @@ -17,6 +17,7 @@ from bot_core.logging.command_logger import CommandLogger from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.auto_role import AutoRole +from modules.permission.abc.permission_service_abc import PermissionServiceABC class AutoRoleGroup(DiscordCommandABC): @@ -30,7 +31,8 @@ class AutoRoleGroup(DiscordCommandABC): translate: TranslatePipe, servers: ServerRepositoryABC, auto_roles: AutoRoleRepositoryABC, - db_context: DatabaseContextABC + db_context: DatabaseContextABC, + permission_service: PermissionServiceABC, ): DiscordCommandABC.__init__(self) @@ -42,6 +44,7 @@ class AutoRoleGroup(DiscordCommandABC): self._servers = servers self._auto_roles = auto_roles self._db_context = db_context + self._permissions = permission_service self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') @@ -56,7 +59,11 @@ class AutoRoleGroup(DiscordCommandABC): self._logger.debug(__name__, f'Received command auto-role list {ctx}') if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): return - self._client_utils.received_command(ctx.guild.id) + + 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 purge command') + return embed = discord.Embed( title=self._t.transform('modules.auto_role.list.title'), @@ -84,6 +91,14 @@ class AutoRoleGroup(DiscordCommandABC): @commands.guild_only() async def add(self, ctx: Context, 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 + + 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 purge command') + 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() if message is None: self._logger.debug(__name__, f'Message with id {message_id} not found in {ctx.channel.name}') @@ -110,6 +125,40 @@ class AutoRoleGroup(DiscordCommandABC): 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)] + @auto_role.command() + @commands.guild_only() + async def remove(self, ctx: Context, auto_role: int): + self._logger.debug(__name__, f'Received command auto-role remove {ctx} {auto_role}') + 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 purge command') + return + + 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') + 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._auto_roles.delete_auto_role(auto_role_from_db) + self._db_context.save_changes() + self._logger.info(__name__, f'Removed AutoRole {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') + + @remove.autocomplete('auto_role') + async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id) + return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles] + @auto_role.group() @commands.guild_only() async def rule(self, ctx: Context): @@ -121,7 +170,11 @@ class AutoRoleGroup(DiscordCommandABC): self._logger.debug(__name__, f'Received command auto-role-rule list {ctx}') if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): return - self._client_utils.received_command(ctx.guild.id) + + 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 purge command') + return embed = discord.Embed( title=self._t.transform('modules.auto_role.list.title'), diff --git a/src/modules/base/events/base_on_command_event.py b/src/modules/base/events/base_on_command_event.py index e1c46a20..348e9e4c 100644 --- a/src/modules/base/events/base_on_command_event.py +++ b/src/modules/base/events/base_on_command_event.py @@ -48,9 +48,9 @@ class BaseOnCommandEvent(OnCommandABC): self._clients = clients self._servers = servers - def _append_received_message_count(self, g_id: int): + def _append_received_command_count(self, g_id: int): try: - self._clients.append_received_message_count(self._bot.user.id, g_id, 1) + self._clients.append_received_command_count(self._bot.user.id, g_id, 1) self._db.save_changes() except Exception as e: self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}', e) @@ -87,4 +87,4 @@ class BaseOnCommandEvent(OnCommandABC): self._logger.info(__name__, f'Received command: {ctx.command} from {ctx.channel}') if ctx is None or ctx.guild is None: return - self._append_received_message_count(ctx.guild.id) + self._append_received_command_count(ctx.guild.id)