mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-21 00:07:51 +00:00
[commands] Assign current parameter and argument in hybrid commands
This commit is contained in:
parent
863df7d049
commit
6e2fcd4762
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user