From 8699436fd2bf1bc648200b50a4b6280e45f6f022 Mon Sep 17 00:00:00 2001 From: Chiggy-Playz Date: Wed, 13 Oct 2021 00:36:32 +0530 Subject: [PATCH 1/4] Fix Optional Literal not showing choices --- discord/ext/commands/core.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 9650bd6f..50c3a481 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1242,9 +1242,10 @@ class Command(_BaseCommand, Generic[CogT, P, T]): annotation = str origin = None - if not required and origin is not None and len(annotation.__args__) == 2: + if not required and origin is Union and annotation.__args__[-1] is type(None): # Unpack Optional[T] (Union[T, None]) into just T - annotation, origin = annotation.__args__[0], None + annotation = annotation.__args__[0] + origin = getattr(annotation, '__origin__', None) option: Dict[str, Any] = { "type": 3, -- 2.47.2 From 80775db1a443e5564067c64633c98966c137cd42 Mon Sep 17 00:00:00 2001 From: Chiggy-Playz Date: Wed, 13 Oct 2021 00:56:08 +0530 Subject: [PATCH 2/4] Format with black --- discord/ext/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 50c3a481..30b75198 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1245,7 +1245,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]): if not required and origin is Union and annotation.__args__[-1] is type(None): # Unpack Optional[T] (Union[T, None]) into just T annotation = annotation.__args__[0] - origin = getattr(annotation, '__origin__', None) + origin = getattr(annotation, "__origin__", None) option: Dict[str, Any] = { "type": 3, -- 2.47.2 From ac3187f62e58b4d2ae10d5e1a1e382f666a3eb8a Mon Sep 17 00:00:00 2001 From: Chiggy-Playz Date: Wed, 13 Oct 2021 14:11:01 +0530 Subject: [PATCH 3/4] Add support for channel_types in options --- discord/ext/commands/core.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 30b75198..47b4dd02 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -136,6 +136,14 @@ application_option_type_lookup = { discord.Role: 8, float: 10, } +application_option_channel_types = { + discord.VoiceChannel: [2], + discord.TextChannel: [0, 5, 6], + discord.CategoryChannel: [4], + discord.Thread: [10, 11, 12], + discord.StageChannel: [13], +} + if TYPE_CHECKING: P = ParamSpec("P") @@ -1266,12 +1274,19 @@ class Command(_BaseCommand, Generic[CogT, P, T]): for python_type, discord_type in application_option_type_lookup.items(): if issubclass(annotation, python_type): option["type"] = discord_type + # Set channel types + if discord_type == 7: + option["channel_types"] = application_option_channel_types[annotation] break elif origin is Union: if annotation in {Union[discord.Member, discord.Role], Union[MemberConverter, RoleConverter]}: option["type"] = 9 + elif all([issubclass(x, (discord.abc.GuildChannel, discord.abc.Messageable)) for x in annotation.__args__]): + option["type"] = 7 + option["channel_types"] = [x for y in annotation.__args__ for x in application_option_channel_types[y]] + elif origin is Literal: literal_values = annotation.__args__ python_type = type(literal_values[0]) -- 2.47.2 From e84cef2607ba4ca09c5d37f8a9fddee0a2f374d1 Mon Sep 17 00:00:00 2001 From: Chiggy-Playz Date: Wed, 13 Oct 2021 22:01:06 +0530 Subject: [PATCH 4/4] Update with requested changes --- discord/ext/commands/core.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 47b4dd02..42ef5ff9 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1250,10 +1250,9 @@ class Command(_BaseCommand, Generic[CogT, P, T]): annotation = str origin = None - if not required and origin is Union and annotation.__args__[-1] is type(None): + if not required and origin is not None and len(annotation.__args__) == 2: # Unpack Optional[T] (Union[T, None]) into just T - annotation = annotation.__args__[0] - origin = getattr(annotation, "__origin__", None) + annotation, origin = annotation.__args__[0], None option: Dict[str, Any] = { "type": 3, @@ -1283,9 +1282,9 @@ class Command(_BaseCommand, Generic[CogT, P, T]): if annotation in {Union[discord.Member, discord.Role], Union[MemberConverter, RoleConverter]}: option["type"] = 9 - elif all([issubclass(x, (discord.abc.GuildChannel, discord.abc.Messageable)) for x in annotation.__args__]): + elif all([arg in application_option_channel_types for arg in annotation.__args__]): option["type"] = 7 - option["channel_types"] = [x for y in annotation.__args__ for x in application_option_channel_types[y]] + option["channel_types"] = [discord_value for arg in annotation.__args__ for discord_value in application_option_channel_types[arg]] elif origin is Literal: literal_values = annotation.__args__ -- 2.47.2