Skip to content

Commit

Permalink
[commands] Assign current parameter and argument in hybrid commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapptz committed May 12, 2022
1 parent 863df7d commit 6e2fcd4
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions discord/ext/commands/hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 6e2fcd4

Please sign in to comment.