Skip to content
This repository was archived by the owner on Aug 28, 2019. It is now read-only.

Commit a6fdc75

Browse files
committed
[commands] Assign current parameter and argument in hybrid commands
1 parent 863df7d commit a6fdc75

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

discord/ext/commands/hybrid.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def required_pos_arguments(func: Callable[..., Any]) -> int:
116116
return sum(p.default is p.empty for p in sig.parameters.values())
117117

118118

119-
def make_converter_transformer(converter: Any) -> Type[app_commands.Transformer]:
119+
def make_converter_transformer(converter: Any, parameter: Parameter) -> Type[app_commands.Transformer]:
120120
try:
121121
module = converter.__module__
122122
except AttributeError:
@@ -126,14 +126,17 @@ def make_converter_transformer(converter: Any) -> Type[app_commands.Transformer]
126126
converter = CONVERTER_MAPPING.get(converter, converter)
127127

128128
async def transform(cls, interaction: discord.Interaction, value: str) -> Any:
129+
ctx = interaction._baton
130+
ctx.current_parameter = parameter
131+
ctx.current_argument = value
129132
try:
130133
if inspect.isclass(converter) and issubclass(converter, Converter):
131134
if inspect.ismethod(converter.convert):
132-
return await converter.convert(interaction._baton, value)
135+
return await converter.convert(ctx, value)
133136
else:
134-
return await converter().convert(interaction._baton, value) # type: ignore
137+
return await converter().convert(ctx, value) # type: ignore
135138
elif isinstance(converter, Converter):
136-
return await converter.convert(interaction._baton, value) # type: ignore
139+
return await converter.convert(ctx, value) # type: ignore
137140
except CommandError:
138141
raise
139142
except Exception as exc:
@@ -158,14 +161,16 @@ def make_greedy_transformer(converter: Any, parameter: Parameter) -> Type[app_co
158161
async def transform(cls, interaction: discord.Interaction, value: str) -> Any:
159162
view = StringView(value)
160163
result = []
164+
ctx = interaction._baton
165+
ctx.current_parameter = parameter
161166
while True:
162167
view.skip_ws()
163-
arg = view.get_quoted_word()
168+
ctx.current_argument = arg = view.get_quoted_word()
164169
if arg is None:
165170
break
166171

167172
# This propagates the exception
168-
converted = await run_converters(interaction._baton, converter, arg, parameter)
173+
converted = await run_converters(ctx, converter, arg, parameter)
169174
result.append(converted)
170175

171176
return result
@@ -228,7 +233,7 @@ def replace_parameter(
228233
app_commands.rename(**renames)(callback)
229234

230235
elif is_converter(converter) or converter in CONVERTER_MAPPING:
231-
param = param.replace(annotation=make_converter_transformer(converter))
236+
param = param.replace(annotation=make_converter_transformer(converter, original))
232237
elif origin is Union:
233238
if len(args) == 2 and args[-1] is _NoneType:
234239
# Special case Optional[X] where X is a single type that can optionally be a converter

0 commit comments

Comments
 (0)