mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-07 02:21:54 +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())
|
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:
|
try:
|
||||||
module = converter.__module__
|
module = converter.__module__
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@ -126,14 +126,17 @@ def make_converter_transformer(converter: Any) -> Type[app_commands.Transformer]
|
|||||||
converter = CONVERTER_MAPPING.get(converter, converter)
|
converter = CONVERTER_MAPPING.get(converter, converter)
|
||||||
|
|
||||||
async def transform(cls, interaction: discord.Interaction, value: str) -> Any:
|
async def transform(cls, interaction: discord.Interaction, value: str) -> Any:
|
||||||
|
ctx = interaction._baton
|
||||||
|
ctx.current_parameter = parameter
|
||||||
|
ctx.current_argument = value
|
||||||
try:
|
try:
|
||||||
if inspect.isclass(converter) and issubclass(converter, Converter):
|
if inspect.isclass(converter) and issubclass(converter, Converter):
|
||||||
if inspect.ismethod(converter.convert):
|
if inspect.ismethod(converter.convert):
|
||||||
return await converter.convert(interaction._baton, value)
|
return await converter.convert(ctx, value)
|
||||||
else:
|
else:
|
||||||
return await converter().convert(interaction._baton, value) # type: ignore
|
return await converter().convert(ctx, value) # type: ignore
|
||||||
elif isinstance(converter, Converter):
|
elif isinstance(converter, Converter):
|
||||||
return await converter.convert(interaction._baton, value) # type: ignore
|
return await converter.convert(ctx, value) # type: ignore
|
||||||
except CommandError:
|
except CommandError:
|
||||||
raise
|
raise
|
||||||
except Exception as exc:
|
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:
|
async def transform(cls, interaction: discord.Interaction, value: str) -> Any:
|
||||||
view = StringView(value)
|
view = StringView(value)
|
||||||
result = []
|
result = []
|
||||||
|
ctx = interaction._baton
|
||||||
|
ctx.current_parameter = parameter
|
||||||
while True:
|
while True:
|
||||||
view.skip_ws()
|
view.skip_ws()
|
||||||
arg = view.get_quoted_word()
|
ctx.current_argument = arg = view.get_quoted_word()
|
||||||
if arg is None:
|
if arg is None:
|
||||||
break
|
break
|
||||||
|
|
||||||
# This propagates the exception
|
# This propagates the exception
|
||||||
converted = await run_converters(interaction._baton, converter, arg, parameter)
|
converted = await run_converters(ctx, converter, arg, parameter)
|
||||||
result.append(converted)
|
result.append(converted)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
@ -228,14 +233,14 @@ def replace_parameter(
|
|||||||
app_commands.rename(**renames)(callback)
|
app_commands.rename(**renames)(callback)
|
||||||
|
|
||||||
elif is_converter(converter) or converter in CONVERTER_MAPPING:
|
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:
|
elif origin is Union:
|
||||||
if len(args) == 2 and args[-1] is _NoneType:
|
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
|
# Special case Optional[X] where X is a single type that can optionally be a converter
|
||||||
inner = args[0]
|
inner = args[0]
|
||||||
is_inner_tranformer = is_transformer(inner)
|
is_inner_tranformer = is_transformer(inner)
|
||||||
if is_converter(inner) and not is_inner_tranformer:
|
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:
|
else:
|
||||||
raise
|
raise
|
||||||
elif origin:
|
elif origin:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user