From b35b023b0260d6ec7f51573ee828d7a36a03762a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 19:47:06 +0200 Subject: [PATCH] [WIP] Added auto-role-rules list #54 --- src/modules/autorole/auto_role_module.py | 2 + .../autorole/command/auto_role_command.py | 2 +- .../command/auto_role_rule_command.py | 81 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/modules/autorole/command/auto_role_rule_command.py diff --git a/src/modules/autorole/auto_role_module.py b/src/modules/autorole/auto_role_module.py index 2d6c5377a6..61f51b8e22 100644 --- a/src/modules/autorole/auto_role_module.py +++ b/src/modules/autorole/auto_role_module.py @@ -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.command.auto_role_command import AutoRoleCommand +from modules.autorole.command.auto_role_rule_command import AutoRoleRuleCommand 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 @@ -24,6 +25,7 @@ class AutoRoleModule(ModuleABC): services.add_transient(ReactionHandler) # commands self._dc.add_command(AutoRoleCommand) + self._dc.add_command(AutoRoleRuleCommand) # events self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, AutoRoleOnRawReactionAddEvent) self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_remove.value, AutoRoleOnRawReactionRemoveEvent) diff --git a/src/modules/autorole/command/auto_role_command.py b/src/modules/autorole/command/auto_role_command.py index 75af4a1bff..0960c6eb66 100644 --- a/src/modules/autorole/command/auto_role_command.py +++ b/src/modules/autorole/command/auto_role_command.py @@ -41,7 +41,7 @@ class AutoRoleCommand(DiscordCommandABC): async def auto_role(self, ctx: Context): pass - @auto_role.command() + @auto_role.command(alias='auto-roles') @commands.guild_only() async def list(self, ctx: Context, wait: int = None): self._logger.debug(__name__, f'Received command auto-role list {ctx}') diff --git a/src/modules/autorole/command/auto_role_rule_command.py b/src/modules/autorole/command/auto_role_rule_command.py new file mode 100644 index 0000000000..3e2ab240ae --- /dev/null +++ b/src/modules/autorole/command/auto_role_rule_command.py @@ -0,0 +1,81 @@ +from typing import List + +import discord +from cpl_discord.command import DiscordCommandABC +from cpl_discord.service import DiscordBotServiceABC +from cpl_translation import TranslatePipe +from discord import app_commands +from discord.ext import commands +from discord.ext.commands import Context + +from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC +from bot_core.abc.message_service_abc import MessageServiceABC +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 + + +class AutoRoleRuleCommand(DiscordCommandABC): + + def __init__( + self, + logger: CommandLogger, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsServiceABC, + translate: TranslatePipe, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._message_service = message_service + self._bot = bot + self._client_utils = client_utils + self._t = translate + self._servers = servers + self._auto_roles = auto_roles + + self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + + @commands.hybrid_group(name="auto-role-rule") + @commands.guild_only() + async def auto_role(self, ctx: Context): + pass + + @auto_role.command(alias='auto-role-rules') + @commands.guild_only() + async def list(self, ctx: Context, auto_role: int, wait: int = None): + 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) + + 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) + ) + + await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) + self._logger.trace(__name__, f'Finished command auto-role list') + + @list.autocomplete('auto_role') + async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: + auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id) + return [app_commands.Choice(name=key, value=key) for key in auto_roles] + + @auto_role.command() + @commands.guild_only() + async def add(self, ctx: Context): + self._logger.debug(__name__, f'Received command auto-role-rule add {ctx}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + + @auto_role.command() + @commands.guild_only() + async def remove(self, ctx: Context): + self._logger.debug(__name__, f'Received command auto-role-rule remove {ctx}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return