Browse Source

[commands] Add Context.invoked_parents

pull/6468/head
Sebastian Law 4 years ago
committed by Danny
parent
commit
1afc127458
  1. 11
      discord/ext/commands/context.py
  2. 4
      discord/ext/commands/core.py

11
discord/ext/commands/context.py

@ -57,6 +57,14 @@ class Context(discord.abc.Messageable):
invoked_with: :class:`str` invoked_with: :class:`str`
The command name that triggered this invocation. Useful for finding out The command name that triggered this invocation. Useful for finding out
which alias called the command. which alias called the command.
invoked_parents: List[:class:`str`]
The command names of the parents that triggered this invocation. Useful for
finding out which aliases called the command.
For example in commands ``?a b c test``, the invoked parents are ``['a', 'b', 'c']``.
.. versionadded:: 1.7
invoked_subcommand: :class:`Command` invoked_subcommand: :class:`Command`
The subcommand that was invoked. The subcommand that was invoked.
If no valid subcommand was invoked then this is equal to ``None``. If no valid subcommand was invoked then this is equal to ``None``.
@ -79,6 +87,7 @@ class Context(discord.abc.Messageable):
self.command = attrs.pop('command', None) self.command = attrs.pop('command', None)
self.view = attrs.pop('view', None) self.view = attrs.pop('view', None)
self.invoked_with = attrs.pop('invoked_with', None) self.invoked_with = attrs.pop('invoked_with', None)
self.invoked_parents = attrs.pop('invoked_parents', [])
self.invoked_subcommand = attrs.pop('invoked_subcommand', None) self.invoked_subcommand = attrs.pop('invoked_subcommand', None)
self.subcommand_passed = attrs.pop('subcommand_passed', None) self.subcommand_passed = attrs.pop('subcommand_passed', None)
self.command_failed = attrs.pop('command_failed', False) self.command_failed = attrs.pop('command_failed', False)
@ -180,6 +189,7 @@ class Context(discord.abc.Messageable):
to_call = cmd.root_parent or cmd to_call = cmd.root_parent or cmd
view.index = len(self.prefix) view.index = len(self.prefix)
view.previous = 0 view.previous = 0
self.invoked_parents = []
view.get_word() # advance to get the root command view.get_word() # advance to get the root command
else: else:
to_call = cmd to_call = cmd
@ -192,6 +202,7 @@ class Context(discord.abc.Messageable):
view.previous = previous view.previous = previous
self.invoked_with = invoked_with self.invoked_with = invoked_with
self.invoked_subcommand = invoked_subcommand self.invoked_subcommand = invoked_subcommand
self.invoked_parents = invoked_parents
self.subcommand_passed = subcommand_passed self.subcommand_passed = subcommand_passed
@property @property

4
discord/ext/commands/core.py

@ -1342,6 +1342,8 @@ class Group(GroupMixin, Command):
injected = hooked_wrapped_callback(self, ctx, self.callback) injected = hooked_wrapped_callback(self, ctx, self.callback)
await injected(*ctx.args, **ctx.kwargs) await injected(*ctx.args, **ctx.kwargs)
ctx.invoked_parents.append(ctx.invoked_with)
if trigger and ctx.invoked_subcommand: if trigger and ctx.invoked_subcommand:
ctx.invoked_with = trigger ctx.invoked_with = trigger
await ctx.invoked_subcommand.invoke(ctx) await ctx.invoked_subcommand.invoke(ctx)
@ -1380,6 +1382,8 @@ class Group(GroupMixin, Command):
if call_hooks: if call_hooks:
await self.call_after_hooks(ctx) await self.call_after_hooks(ctx)
ctx.invoked_parents.append(ctx.invoked_with)
if trigger and ctx.invoked_subcommand: if trigger and ctx.invoked_subcommand:
ctx.invoked_with = trigger ctx.invoked_with = trigger
await ctx.invoked_subcommand.reinvoke(ctx, call_hooks=call_hooks) await ctx.invoked_subcommand.reinvoke(ctx, call_hooks=call_hooks)

Loading…
Cancel
Save