From e6c99593811eaabc5b138957d60d65ffe10bfe0d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 14 Aug 2023 20:57:59 +0200 Subject: [PATCH] Added filter to mass_move channel_from #296 --- kdb-bot/src/bot/module_list.py | 2 +- .../modules/base/command/mass_move_command.py | 4 ++- .../base/helper/voice_channel_transformer.py | 30 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 kdb-bot/src/modules/base/helper/voice_channel_transformer.py diff --git a/kdb-bot/src/bot/module_list.py b/kdb-bot/src/bot/module_list.py index aee830ef..3e5fea34 100644 --- a/kdb-bot/src/bot/module_list.py +++ b/kdb-bot/src/bot/module_list.py @@ -25,8 +25,8 @@ class ModuleList: [ CoreModule, # has to be first! DataModule, + ConfigModule, # has to be before db check DatabaseModule, - ConfigModule, # has be to after db check GraphQLModule, PermissionModule, AutoRoleModule, diff --git a/kdb-bot/src/modules/base/command/mass_move_command.py b/kdb-bot/src/modules/base/command/mass_move_command.py index dca23d73..4dd835d2 100644 --- a/kdb-bot/src/modules/base/command/mass_move_command.py +++ b/kdb-bot/src/modules/base/command/mass_move_command.py @@ -4,6 +4,7 @@ import discord from cpl_discord.command import DiscordCommandABC from cpl_discord.service import DiscordBotServiceABC from cpl_translation import TranslatePipe +from discord.app_commands import Transform from discord.ext import commands from discord.ext.commands import Context @@ -11,6 +12,7 @@ from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from bot_core.service.client_utils_service import ClientUtilsService +from modules.base.helper.voice_channel_transformer import VoiceChannelTransformer class MassMoveCommand(DiscordCommandABC): @@ -36,7 +38,7 @@ class MassMoveCommand(DiscordCommandABC): self, ctx: Context, channel_to: discord.VoiceChannel, - channel_from: discord.VoiceChannel = None, + channel_from: Transform[str, VoiceChannelTransformer] = None, ): self._logger.debug(__name__, f"Received command mass-move {ctx}") diff --git a/kdb-bot/src/modules/base/helper/voice_channel_transformer.py b/kdb-bot/src/modules/base/helper/voice_channel_transformer.py new file mode 100644 index 00000000..a9ee5df0 --- /dev/null +++ b/kdb-bot/src/modules/base/helper/voice_channel_transformer.py @@ -0,0 +1,30 @@ +import discord +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_query.extension import List +from discord import Interaction, app_commands +from discord.app_commands import Transformer, Choice + +from bot_core.abc.client_utils_abc import ClientUtilsABC + + +class VoiceChannelTransformer(Transformer): + async def transform(self, interaction: Interaction, value: str, /) -> discord.VoiceChannel: + voice_channel = ( + List(discord.VoiceChannel, interaction.guild.voice_channels) + .where(lambda x: str(x.id) == value) + .first_or_default() + ) + return voice_channel + + async def autocomplete(self, interaction: Interaction, current: str, /) -> list[Choice[str]]: + @ServiceProviderABC.inject + def get_client_utils(client_utils: ClientUtilsABC) -> ClientUtilsABC: + return client_utils + + voice_channels = List(discord.Role, interaction.guild.voice_channels).where(lambda x: len(x.members) > 0) + return [ + app_commands.Choice( + name=f"{vc.name}" if vc.category is None else f"{vc.name}: {vc.category.name}", value=vc.name + ) + for vc in get_client_utils().get_auto_complete_list(voice_channels, current, lambda x: x.name) + ]