Here is how to store app_commands.AppCommand to access later for mentioning or anything.
There are two options here:
1. Subclass commands.Bot and add some methods and attributes
2. Subclass app_commands.CommandTree (recommended)
Here is how to use option 1
Code: here
You can either copypaste the whole paste into your code and use it OR read over the code and copy the parts you need into your own code. Using it as easy as doing the following:
bot = MyBot(command_prefix=..., intents=...)
You can change the class to subclass discord.Client
instead if you want.
After defining an instance, you can start implementing the methods it your code.
A sync command would look something the following:
# define our tree
tree = bot.tree
# sync and assign a var to get the return
commands = await tree.sync(guild=...)
# call our custom method to cache the commands
await bot.update_app_commands_cache(commands, guild=...)
What about mentioning?! That would be something like the following:
# need to mention a global command called "ping"?
command = bot.get_app_command("ping")
# or a guild command named "foo"
command = bot.get_app_command("foo", guild=guild_it_belongs_to)
# subcommands? yes!
# guild kwarg is optional if the group is global
command = bot.get_app_command("full name", guild=...)
await ...send(command.mention)
You can also call update_app_commands_cache
without any commands, it will fetch all commands in that case.
Here is how to use option 2
Code: here
You can either copypaste the whole paste into your code and use it OR read over the code and copy the parts you need into your own code. Using it as easy as doing the following:
bot = commands.Bot(command_prefix=..., intents=..., tree_cls=MyCommandTree)
# using discord.Client? that would be:
client = discord.Client(intents=...)
client.tree = MyCommandTree(client)
A sync command would look something the following:
# define our tree
tree = client/bot.tree
await tree.sync(guild=...)
What about mentioning?! That would be something like the following:
# need to mention a global command called "ping"?
command = client/bot.tree.get_app_command("ping")
# or a guild command named "foo"
command = client/bot.tree.get_app_command("foo", guild=guild_it_belongs_to)
# subcommands? yes!
# guild kwarg is optional if the group is global
command = client/bot.tree.get_app_command("full name", guild=...)
await ...send(command.mention)
The provided MyCommandTree
subclass will automatically cache all app commands if you used the following methods: .sync, .fetch_command
and .fetch_commands
. .clear_commands
will also clear the cache commands (per scope)
These are for both options
- There is a method called
find_app_command_by_names
it's for kinda fuzzysearching through your commands to find the right one. It's used like this:
res = <bot>(.tree?).find_app_command_by_names("ping", "foo", "bar", guild=...)
it will try to search through all commands (including subcommands) to find one command with the passed names.
get_app_command
also supports getting the AppCommand from its ID if you got that somehow.