Skip to Content
nyx logonyx guide

Location

Reading time:1 min read

Sometimes you may need a reference to a currently registered command. You can do so using the CommandRepository of your command manager.

const  = .().();
.locateBy
  • locateByNameTree
  • locateByClassTree

Location by name

Use the locateByNameTree() method to find a command by its name tree, which is a string representation of the command's hierarchy, e.g. parent sub-command.

const topLevel = .('top-level-command');
const topLevel: TopLevelCommand<InteractionTypes> | null
const subCommandOrGroup = .('parent', 'sub-command-or-group');
const subCommandOrGroup: SubCommand<InteractionTypes> | SubCommandGroup<InteractionTypes> | null
const subCommand = .('parent', 'sub-command-group', 'sub-command');
const subCommand: SubCommand<InteractionTypes> | null

This however only returns the generic base types, not a specific class instance. If you need a specific type, use the following method.

Location by class

The locateByClassTree() method allows you to find a command by its class hierarchy, which is useful if you want to get a specific command type.

const standalone = .();
const standalone: MyStandaloneCommand | null
const subCommand = .(, );
const subCommand: MySubCommand | null
const subCommandGroup = .(, );
const subCommandGroup: MySubCommandGroup | null

Notice how the return type of each call is the respective instance (or null), not the generic types.

Getting commands

Alternatively, you can get all top level commands with the getCommands() method, which returns a ReadonlyCollection of them, keyed by name.

This returns the local store, not a copy of it. Hence why it's typed as readonly. However, you may type assert and manipulate it, but it's not safe to do so.
const topLevels = .();
const topLevels: ReadonlyCollection<string, TopLevelCommand<InteractionTypes>>

On this page