From 6e2fcd47626b364586047b847ae54e1afc93d1ab Mon Sep 17 00:00:00 2001 From: Rapptz Date: Thu, 12 May 2022 06:15:51 -0400 Subject: [PATCH] [commands] Assign current parameter and argument in hybrid commands --- discord/ext/commands/hybrid.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/discord/ext/commands/hybrid.py b/discord/ext/commands/hybrid.py index 282a88451..2848fe004 100644 --- a/discord/ext/commands/hybrid.py +++ b/discord/ext/commands/hybrid.py @@ -116,7 +116,7 @@ def required_pos_arguments(func: Callable[..., Any]) -> int: return sum(p.default is p.empty for p in sig.parameters.values()) -def make_converter_transformer(converter: Any) -> Type[app_commands.Transformer]: +def make_converter_transformer(converter: Any, parameter: Parameter) -> Type[app_commands.Transformer]: try: module = converter.__module__ except AttributeError: @@ -126,14 +126,17 @@ def make_converter_transformer(converter: Any) -> Type[app_commands.Transformer] converter = CONVERTER_MAPPING.get(converter, converter) async def transform(cls, interaction: discord.Interaction, value: str) -> Any: + ctx = interaction._baton + ctx.current_parameter = parameter + ctx.current_argument = value try: if inspect.isclass(converter) and issubclass(converter, Converter): if inspect.ismethod(converter.convert): - return await converter.convert(interaction._baton, value) + return await converter.convert(ctx, value) else: - return await converter().convert(interaction._baton, value) # type: ignore + return await converter().convert(ctx, value) # type: ignore elif isinstance(converter, Converter): - return await converter.convert(interaction._baton, value) # type: ignore + return await converter.convert(ctx, value) # type: ignore except CommandError: raise except Exception as exc: @@ -158,14 +161,16 @@ def make_greedy_transformer(converter: Any, parameter: Parameter) -> Type[app_co async def transform(cls, interaction: discord.Interaction, value: str) -> Any: view = StringView(value) result = [] + ctx = interaction._baton + ctx.current_parameter = parameter while True: view.skip_ws() - arg = view.get_quoted_word() + ctx.current_argument = arg = view.get_quoted_word() if arg is None: break # This propagates the exception - converted = await run_converters(interaction._baton, converter, arg, parameter) + converted = await run_converters(ctx, converter, arg, parameter) result.append(converted) return result @@ -228,14 +233,14 @@ def replace_parameter( app_commands.rename(**renames)(callback) elif is_converter(converter) or converter in CONVERTER_MAPPING: - param = param.replace(annotation=make_converter_transformer(converter)) + param = param.replace(annotation=make_converter_transformer(converter, original)) elif origin is Union: if len(args) == 2 and args[-1] is _NoneType: # Special case Optional[X] where X is a single type that can optionally be a converter inner = args[0] is_inner_tranformer = is_transformer(inner) if is_converter(inner) and not is_inner_tranformer: - param = param.replace(annotation=Optional[make_converter_transformer(inner)]) # type: ignore + param = param.replace(annotation=Optional[make_converter_transformer(inner, original)]) # type: ignore else: raise elif origin: