From eef8c07379065e6dc37b2a863ef994f22ff948c7 Mon Sep 17 00:00:00 2001 From: Gnome Date: Wed, 27 Oct 2021 13:30:30 +0100 Subject: [PATCH] Optimise _unwrap_slash_groups and similar --- discord/ext/commands/bot.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index c9ad8d2d..45536747 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -188,16 +188,17 @@ def _is_submodule(parent: str, child: str) -> bool: def _unwrap_slash_groups( data: ApplicationCommandInteractionData, -) -> Tuple[str, List[ApplicationCommandInteractionDataOption]]: +) -> Tuple[str, Dict[str, ApplicationCommandInteractionDataOption]]: command_name = data["name"] - command_options = data.get("options") or [] - while any(o["type"] in {1, 2} for o in command_options): # type: ignore - for option in command_options: # type: ignore - if option["type"] in {1, 2}: # type: ignore - command_name += f' {option["name"]}' # type: ignore - command_options = option.get("options") or [] - - return command_name, command_options + command_options: Any = data.get("options") or [] + while True: + try: + option = next(o for o in command_options if o["type"] in {1, 2}) + except StopIteration: + return command_name, {o["name"]: o for o in command_options} + else: + command_name += f' {option["name"]}' + command_options = option.get("options") or [] def _quote_string_safe(string: str) -> str: @@ -1299,7 +1300,7 @@ class BotBase(GroupMixin): for name, param in command.clean_params.items(): if inspect.isclass(param.annotation) and issubclass(param.annotation, FlagConverter): for name, flag in param.annotation.get_flags().items(): - option = next((o for o in command_options if o["name"] == name), None) + option = command_options.get(name) if option is None: if flag.required: @@ -1310,7 +1311,7 @@ class BotBase(GroupMixin): message.content += f" {prefix}{name}{delimiter}{option['value']}" # type: ignore continue - option = next((o for o in command_options if o["name"] == name), None) + option = command_options.get(name) if option is None: if param.default is param.empty and not command._is_typing_optional(param.annotation): raise errors.MissingRequiredArgument(param)