From 6f3877700e5bbcffd4fc76a3eec0d8022431a493 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 9 Nov 2022 21:40:04 +0100 Subject: [PATCH] Fixed auto-role output by server #93 --- .../bot_data/abc/auto_role_repository_abc.py | 2 +- .../auto_role/command/auto_role_group.py | 22 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/kdb-bot/src/bot_data/abc/auto_role_repository_abc.py b/kdb-bot/src/bot_data/abc/auto_role_repository_abc.py index 899cd4868f..4283824fde 100644 --- a/kdb-bot/src/bot_data/abc/auto_role_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/auto_role_repository_abc.py @@ -22,7 +22,7 @@ class AutoRoleRepositoryABC(ABC): 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 + def get_auto_roles_by_server_id(self, id: int) -> List[AutoRole]: pass @abstractmethod def get_auto_role_by_message_id(self, id: int) -> AutoRole: pass diff --git a/kdb-bot/src/modules/auto_role/command/auto_role_group.py b/kdb-bot/src/modules/auto_role/command/auto_role_group.py index 515d2fe4db..25ba703695 100644 --- a/kdb-bot/src/modules/auto_role/command/auto_role_group.py +++ b/kdb-bot/src/modules/auto_role/command/auto_role_group.py @@ -66,12 +66,16 @@ class AutoRoleGroup(DiscordCommandABC): self._logger.trace(__name__, f'Finished command auto-role list') return + if ctx.guild is None: + return + embed = discord.Embed( title=self._t.transform('modules.auto_role.list.title'), description=self._t.transform('modules.auto_role.list.description'), color=int('ef9d0d', 16) ) - auto_roles = self._auto_roles.get_auto_roles() + server = self._servers.get_server_by_discord_id(ctx.guild.id) + auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id) if auto_roles.count() < 1: await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.error.nothing_found')) self._logger.trace(__name__, f'Finished command auto-role list') @@ -165,7 +169,8 @@ class AutoRoleGroup(DiscordCommandABC): @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) + server = self._servers.get_server_by_discord_id(interaction.guild.id) + auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id).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() @@ -212,7 +217,8 @@ class AutoRoleGroup(DiscordCommandABC): @list.autocomplete('auto_role') async def list_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) + server = self._servers.get_server_by_discord_id(interaction.guild.id) + auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id).select(lambda x: x.auto_role_id) return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles] @rule.command() @@ -259,7 +265,8 @@ class AutoRoleGroup(DiscordCommandABC): @add.autocomplete('auto_role') async def add_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) + server = self._servers.get_server_by_discord_id(interaction.guild.id) + auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id).select(lambda x: x.auto_role_id) return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles] @add.autocomplete('emoji_name') @@ -299,6 +306,7 @@ 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] + server = self._servers.get_server_by_discord_id(interaction.guild.id) + auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id).select(lambda x: x.auto_role_id) + rules = auto_roles.select_many(lambda ar: self._auto_roles.get_auto_role_rules_by_auto_role_id(ar)) + 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]