From fbed02dc4d5ecd7d179bc87861cd8559a29292ec Mon Sep 17 00:00:00 2001 From: Soheab Date: Tue, 12 Oct 2021 22:53:37 +0200 Subject: [PATCH 1/4] Add ability to set a flag description. This PR adds the ability to set a flag description that shows in the slash command options menu. --- discord/ext/commands/core.py | 16 +++++++++++++--- discord/ext/commands/flags.py | 10 ++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 9650bd6f..32cdda2c 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1226,15 +1226,25 @@ class Command(_BaseCommand, Generic[CogT, P, T]): ctx.command = original def _param_to_options( - self, name: str, annotation: Any, required: bool, varadic: bool + self, name: str, annotation: Any, required: bool, varadic: bool, description=None ) -> List[Optional[ApplicationCommandInteractionDataOption]]: + + if description is not None: + self.option_descriptions[name] = description + + description = self.option_descriptions[name] origin = getattr(annotation, "__origin__", None) + if inspect.isclass(annotation) and issubclass(annotation, FlagConverter): return [ param for name, flag in annotation.get_flags().items() for param in self._param_to_options( - name, flag.annotation, required=flag.required, varadic=flag.annotation is tuple + name, + flag.annotation, + required=flag.required, + varadic=flag.annotation is tuple, + description=flag.description if flag.description is not MISSING else None, ) ] @@ -1250,7 +1260,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]): "type": 3, "name": name, "required": required, - "description": self.option_descriptions[name], + "description": description, } if origin is None: diff --git a/discord/ext/commands/flags.py b/discord/ext/commands/flags.py index 367127a4..f23f5d0d 100644 --- a/discord/ext/commands/flags.py +++ b/discord/ext/commands/flags.py @@ -81,6 +81,8 @@ class Flag: ------------ name: :class:`str` The name of the flag. + description: :class:`str` + The description of the flag. aliases: List[:class:`str`] The aliases of the flag name. attribute: :class:`str` @@ -96,7 +98,8 @@ class Flag: Whether multiple given values overrides the previous value. """ - name: str = MISSING + name: str = MISSING, + description: str = MISSING, aliases: List[str] = field(default_factory=list) attribute: str = MISSING annotation: Any = MISSING @@ -117,6 +120,7 @@ class Flag: def flag( *, name: str = MISSING, + description: str = MISSING, aliases: List[str] = MISSING, default: Any = MISSING, max_args: int = MISSING, @@ -129,6 +133,8 @@ def flag( ------------ name: :class:`str` The flag name. If not given, defaults to the attribute name. + description: :class:`str` + Description of the flag for the slash commands options. The default value is `'no description'`. aliases: List[:class:`str`] Aliases to the flag name. If not given no aliases are set. default: Any @@ -143,7 +149,7 @@ def flag( Whether multiple given values overrides the previous value. The default value depends on the annotation given. """ - return Flag(name=name, aliases=aliases, default=default, max_args=max_args, override=override) + return Flag(name=name, description=description, aliases=aliases, default=default, max_args=max_args, override=override) def validate_flag_name(name: str, forbidden: Set[str]): -- 2.47.2 From b8e4d23d44d3e7b525cb0fab39b1163a287f23f1 Mon Sep 17 00:00:00 2001 From: Soheab Date: Wed, 13 Oct 2021 16:17:14 +0200 Subject: [PATCH 2/4] Push requested changes. --- discord/ext/commands/core.py | 2 +- discord/ext/commands/flags.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 32cdda2c..3f383f57 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1226,7 +1226,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]): ctx.command = original def _param_to_options( - self, name: str, annotation: Any, required: bool, varadic: bool, description=None + self, name: str, annotation: Any, required: bool, varadic: bool, description: Optional[str] = None ) -> List[Optional[ApplicationCommandInteractionDataOption]]: if description is not None: diff --git a/discord/ext/commands/flags.py b/discord/ext/commands/flags.py index f23f5d0d..d353c524 100644 --- a/discord/ext/commands/flags.py +++ b/discord/ext/commands/flags.py @@ -98,8 +98,8 @@ class Flag: Whether multiple given values overrides the previous value. """ - name: str = MISSING, - description: str = MISSING, + name: str = MISSING + description: str = MISSING aliases: List[str] = field(default_factory=list) attribute: str = MISSING annotation: Any = MISSING -- 2.47.2 From e9010454dd7c9032c2ee50d386b8c7eb7b5383cb Mon Sep 17 00:00:00 2001 From: Soheab Date: Wed, 13 Oct 2021 19:35:22 +0200 Subject: [PATCH 3/4] formatted with black(?) --- discord/ext/commands/core.py | 2 +- discord/ext/commands/flags.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 3f383f57..643dfd5e 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1234,7 +1234,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]): description = self.option_descriptions[name] origin = getattr(annotation, "__origin__", None) - + if inspect.isclass(annotation) and issubclass(annotation, FlagConverter): return [ param diff --git a/discord/ext/commands/flags.py b/discord/ext/commands/flags.py index d353c524..10656167 100644 --- a/discord/ext/commands/flags.py +++ b/discord/ext/commands/flags.py @@ -149,7 +149,9 @@ def flag( Whether multiple given values overrides the previous value. The default value depends on the annotation given. """ - return Flag(name=name, description=description, aliases=aliases, default=default, max_args=max_args, override=override) + return Flag( + name=name, description=description, aliases=aliases, default=default, max_args=max_args, override=override + ) def validate_flag_name(name: str, forbidden: Set[str]): -- 2.47.2 From 46b00f0d4a55df52dcc73f148f81bfeced9f4763 Mon Sep 17 00:00:00 2001 From: Soheab Date: Wed, 13 Oct 2021 20:51:09 +0200 Subject: [PATCH 4/4] Merge with 2.0 branch and reformat. --- discord/ext/commands/core.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 95b56a68..88f31421 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1294,7 +1294,11 @@ class Command(_BaseCommand, Generic[CogT, P, T]): elif all([arg in application_option_channel_types for arg in annotation.__args__]): option["type"] = 7 - option["channel_types"] = [discord_value for arg in annotation.__args__ for discord_value in application_option_channel_types[arg]] + 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