From 11932d9e9d965b1ec0b2c643eec91a26d5d9332a Mon Sep 17 00:00:00 2001 From: BobDotCom <71356958+BobDotCom@users.noreply.github.com> Date: Mon, 11 Apr 2022 11:58:18 -0500 Subject: [PATCH] Fix conversion --- discord/ext/bridge/core.py | 67 ++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/discord/ext/bridge/core.py b/discord/ext/bridge/core.py index 1123de348d..f8b9eafb80 100644 --- a/discord/ext/bridge/core.py +++ b/discord/ext/bridge/core.py @@ -41,6 +41,8 @@ __all__ = ("BridgeCommand", "bridge_command", "BridgeExtCommand", "BridgeSlashCommand") +from ..commands.converter import _convert_to_bool + from ...utils import get @@ -140,38 +142,41 @@ def attachment_callback(*args): # pylint: disable=unused-argument class BridgeOption(Option, Converter): async def convert(self, ctx, argument) -> Any: - if self.converter is not None: - converted = await self.converter.convert(ctx, argument) - else: - mapping = { - SlashCommandOptionType.string: str, - SlashCommandOptionType.integer: int, - SlashCommandOptionType.boolean: bool, - SlashCommandOptionType.user: UserConverter, - SlashCommandOptionType.channel: GuildChannelConverter, - SlashCommandOptionType.role: RoleConverter, - SlashCommandOptionType.mentionable: MentionableConverter, - SlashCommandOptionType.number: float, - SlashCommandOptionType.attachment: attachment_callback, - } - converter = mapping[self.input_type] - if issubclass(converter, Converter): - converted = await converter().convert(ctx, argument) - else: - converted = converter(argument) - if self.choices: - choices_names = [choice.name for choice in self.choices] - if converted in choices_names: - converted = get(self.choices, name=converted).value + try: + if self.converter is not None: + converted = await self.converter.convert(ctx, argument) else: - choices = [choice.value for choice in self.choices] - if converted not in choices: - print(self.choices) - raise ValueError( - f"{argument} is not a valid choice. Valid choices: {list(set(choices_names + choices))}" - ) - - return converted + mapping = { + SlashCommandOptionType.string: str, + SlashCommandOptionType.integer: int, + SlashCommandOptionType.boolean: lambda val: _convert_to_bool(str(val)), + SlashCommandOptionType.user: UserConverter, + SlashCommandOptionType.channel: GuildChannelConverter, + SlashCommandOptionType.role: RoleConverter, + SlashCommandOptionType.mentionable: MentionableConverter, + SlashCommandOptionType.number: float, + SlashCommandOptionType.attachment: attachment_callback, + } + converter = mapping[self.input_type] + if issubclass(converter, Converter): + converted = await converter().convert(ctx, argument) + else: + converted = converter(argument) + if self.choices: + choices_names = [choice.name for choice in self.choices] + if converted in choices_names: + converted = get(self.choices, name=converted).value + else: + choices = [choice.value for choice in self.choices] + if converted not in choices: + print(self.choices) + raise ValueError( + f"{argument} is not a valid choice. Valid choices: {list(set(choices_names + choices))}" + ) + + return converted + except ValueError as exc: + raise BadArgument() from exc discord.commands.options.Option = BridgeOption