Skip to main content

Acquiring an Application Command Registry

To do anything with the Application Command Registry we need to "acquire" one. To "acquire" an Application Command Registry means to retrieve an already existing one for the command name you specify or generating a brand new one. This may sound more complicated than it actually is, so bare with us here. Acquiring an Application Command Registry can be done in two ways:

  • By calling the ApplicationCommandRegistries.acquire method
  • By using the registerApplicationCommands method of the Command class (which receives a pre-defined registry for the command it's present in)

Command#registerApplicationCommands

This method is present inside of the Command class and can be used to acquire the command's ApplicationCommandRegistry and use it directly. Let's see a quick example of how to use it:

import { ApplicationCommandRegistry, Command } from '@sapphire/framework';export class PingCommand extends Command {  public override registerApplicationCommands(registry: ApplicationCommandRegistry) {    // registry is specifically for the "ping" command  }}

This means that this registry will only concern itself about the current ping command - it's not going to impact any other commands.

ApplicationCommandRegistries.acquire

caution

For most use cases the method above should suffice. Acquiring the Application Command Registry through ApplicationCommandRegistries.acquire should be considered advanced. An example usage of this is consolidating all the registering outside of the command, or creating TypeScript decorators for registering.

This method is used to acquire the application command registry by it's name. It will return the application command registry for command name provided.

If command name is already registered in the registry then it will return the existing registry else it will create a new and then returns it. Let's see an example of how to use it:

import { ApplicationCommandRegistries } from '@sapphire/framework';// Gets the registry for a command with the name of "ping"const pingRegistry = ApplicationCommandRegistries.acquire('ping');

It returns a ApplicationCommandRegistry for the command name provided, which can be used to perform operations on the registry for this command.

What's next?

Now that we have learned how the acquire the ApplicationCommandRegistry we can start registering commands. To learn how to register Slash Commands see Registering Chat Input Commands, or for learning how to register Context Menu Commands see Registering Context Commands