Browse Source

[commands] Add Context.current_argument

pull/7824/head
jack1142 3 years ago
committed by GitHub
parent
commit
3d914e08e0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      discord/ext/commands/context.py
  2. 10
      discord/ext/commands/core.py

7
discord/ext/commands/context.py

@ -96,6 +96,11 @@ class Context(discord.abc.Messageable, Generic[BotT]):
The parameter that is currently being inspected and converted. The parameter that is currently being inspected and converted.
This is only of use for within converters. This is only of use for within converters.
.. versionadded:: 2.0
current_argument: Optional[:class:`str`]
The argument string of the :attr:`current_parameter` that is currently being converted.
This is only of use for within converters.
.. versionadded:: 2.0 .. versionadded:: 2.0
prefix: Optional[:class:`str`] prefix: Optional[:class:`str`]
The prefix that was used to invoke the command. The prefix that was used to invoke the command.
@ -141,6 +146,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
subcommand_passed: Optional[str] = None, subcommand_passed: Optional[str] = None,
command_failed: bool = False, command_failed: bool = False,
current_parameter: Optional[inspect.Parameter] = None, current_parameter: Optional[inspect.Parameter] = None,
current_argument: Optional[str] = None,
): ):
self.message: Message = message self.message: Message = message
self.bot: BotT = bot self.bot: BotT = bot
@ -155,6 +161,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
self.subcommand_passed: Optional[str] = subcommand_passed self.subcommand_passed: Optional[str] = subcommand_passed
self.command_failed: bool = command_failed self.command_failed: bool = command_failed
self.current_parameter: Optional[inspect.Parameter] = current_parameter self.current_parameter: Optional[inspect.Parameter] = current_parameter
self.current_argument: Optional[str] = current_argument
self._state: ConnectionState = self.message._state self._state: ConnectionState = self.message._state
async def invoke(self, command: Command[CogT, P, T], /, *args: P.args, **kwargs: P.kwargs) -> T: async def invoke(self, command: Command[CogT, P, T], /, *args: P.args, **kwargs: P.kwargs) -> T:

10
discord/ext/commands/core.py

@ -604,10 +604,10 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
previous = view.index previous = view.index
if consume_rest_is_special: if consume_rest_is_special:
argument = view.read_rest().strip() ctx.current_argument = argument = view.read_rest().strip()
else: else:
try: try:
argument = view.get_quoted_word() ctx.current_argument = argument = view.get_quoted_word()
except ArgumentParsingError as exc: except ArgumentParsingError as exc:
if self._is_typing_optional(param.annotation): if self._is_typing_optional(param.annotation):
view.index = previous view.index = previous
@ -630,7 +630,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
view.skip_ws() view.skip_ws()
try: try:
argument = view.get_quoted_word() ctx.current_argument = argument = view.get_quoted_word()
value = await run_converters(ctx, converter, argument, param) # type: ignore value = await run_converters(ctx, converter, argument, param) # type: ignore
except (CommandError, ArgumentParsingError): except (CommandError, ArgumentParsingError):
view.index = previous view.index = previous
@ -646,7 +646,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
view = ctx.view view = ctx.view
previous = view.index previous = view.index
try: try:
argument = view.get_quoted_word() ctx.current_argument = argument = view.get_quoted_word()
value = await run_converters(ctx, converter, argument, param) # type: ignore value = await run_converters(ctx, converter, argument, param) # type: ignore
except (CommandError, ArgumentParsingError): except (CommandError, ArgumentParsingError):
view.index = previous view.index = previous
@ -754,7 +754,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
# kwarg only param denotes "consume rest" semantics # kwarg only param denotes "consume rest" semantics
if self.rest_is_raw: if self.rest_is_raw:
converter = get_converter(param) converter = get_converter(param)
argument = view.read_rest() ctx.current_argument = argument = view.read_rest()
kwargs[name] = await run_converters(ctx, converter, argument, param) kwargs[name] = await run_converters(ctx, converter, argument, param)
else: else:
kwargs[name] = await self.transform(ctx, param) kwargs[name] = await self.transform(ctx, param)

Loading…
Cancel
Save