aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Law <[email protected]>2021-02-22 05:17:13 -0800
committerDanny <[email protected]>2021-02-23 03:39:08 -0500
commit1afc12745813ba20e817a51bed73bcfbd246c655 (patch)
tree9f86dbeee52a7e667c6e18103bad8b138f5ee76e
parentDeprecate non-bot methods (diff)
downloaddiscord.py-1afc12745813ba20e817a51bed73bcfbd246c655.tar.xz
discord.py-1afc12745813ba20e817a51bed73bcfbd246c655.zip
[commands] Add Context.invoked_parents
-rw-r--r--discord/ext/commands/context.py11
-rw-r--r--discord/ext/commands/core.py4
2 files changed, 15 insertions, 0 deletions
diff --git a/discord/ext/commands/context.py b/discord/ext/commands/context.py
index c4691155..45d96f2b 100644
--- a/discord/ext/commands/context.py
+++ b/discord/ext/commands/context.py
@@ -57,6 +57,14 @@ class Context(discord.abc.Messageable):
invoked_with: :class:`str`
The command name that triggered this invocation. Useful for finding out
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`
The subcommand that was invoked.
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.view = attrs.pop('view', 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.subcommand_passed = attrs.pop('subcommand_passed', None)
self.command_failed = attrs.pop('command_failed', False)
@@ -180,6 +189,7 @@ class Context(discord.abc.Messageable):
to_call = cmd.root_parent or cmd
view.index = len(self.prefix)
view.previous = 0
+ self.invoked_parents = []
view.get_word() # advance to get the root command
else:
to_call = cmd
@@ -192,6 +202,7 @@ class Context(discord.abc.Messageable):
view.previous = previous
self.invoked_with = invoked_with
self.invoked_subcommand = invoked_subcommand
+ self.invoked_parents = invoked_parents
self.subcommand_passed = subcommand_passed
@property
diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py
index baf2dca5..9cef19e5 100644
--- a/discord/ext/commands/core.py
+++ b/discord/ext/commands/core.py
@@ -1342,6 +1342,8 @@ class Group(GroupMixin, Command):
injected = hooked_wrapped_callback(self, ctx, self.callback)
await injected(*ctx.args, **ctx.kwargs)
+ ctx.invoked_parents.append(ctx.invoked_with)
+
if trigger and ctx.invoked_subcommand:
ctx.invoked_with = trigger
await ctx.invoked_subcommand.invoke(ctx)
@@ -1380,6 +1382,8 @@ class Group(GroupMixin, Command):
if call_hooks:
await self.call_after_hooks(ctx)
+ ctx.invoked_parents.append(ctx.invoked_with)
+
if trigger and ctx.invoked_subcommand:
ctx.invoked_with = trigger
await ctx.invoked_subcommand.reinvoke(ctx, call_hooks=call_hooks)