Skip to Content
nyx logonyx guide

Commands

Reading time:4 min read

It's recommended to read the Commands Overview page first.

The CommandLoader docs scans a directory for command classes and returns a TopLevelCommand[] ready for commandManager.addCommands().

Folder structure

The loader expects a specific layout, following these rules:

  1. Root-level commands are loaded as-is. They must be AbstractStandaloneCommand, AbstractParentCommand, or AbstractContextMenuCommand.
  2. A AbstractParentCommand may have a children folder with the same name as its data.name. The children folder must contain AbstractSubCommand and/or AbstractSubCommandGroup classes, which are wired to the parent.
  3. A AbstractSubCommandGroup may have a nested children folder with the same name as its data.name. The nested children folder must contain AbstractSubCommand classes, which are wired to the group.
  4. Any subdirectory that does not match a parent/group data.name is treated as an organizational folder and recursed into as a new root, continuing the scan with the same rules above. This allows you to organize commands into subfolders without affecting the command hierarchy.
  5. Files with multiple classes have each class processed independently.

About filenames

The filenames are completely irrelevant for the loader. The command hierarchy is determined entirely by the data.name of each class and the folder structure.

Example

ping.js
report-user.js
mod.js
ban.js
mute.js
settings.js
view.js
edit.js

Assuming the exported command classes have their data.name set the same as their file names, the loader will produce the following command hierarchy:

FileTypeNotes
ping.jsAbstractStandaloneCommandRoot-level, loaded as-is
report-user.jsAbstractContextMenuCommandRoot-level, loaded as-is
mod.jsAbstractParentCommanddata.name === "mod", children folder matches name
mod/ban.jsAbstractSubCommandInside children folder, wired to parent
mod/settings.jsAbstractSubCommandGroupdata.name === "settings", nested folder matches name
mod/settings/view.jsAbstractSubCommandInside children folder, wired to group
utils/utils.jsAbstractStandaloneCommandInside organizational folder, loaded as root-level
utils/format.jsAbstractStandaloneCommandInside organizational folder, loaded as root-level

Usage

By default, commands are automatically registered via commandManager.addCommands():

import {  } from '@nyx-discord/loaders';

await .<DjsInteractionTypes>({ , : './dist/commands' });

Pass register: false to handle registration yourself:

import {  } from '@nyx-discord/loaders';

const  = await .<DjsInteractionTypes>({
  ,
  : './dist/commands',
  : false,
});

.().addCommands(...);

Writing commands for the loader

Top-level commands (standalone, parent, context menu) are instantiated with no constructor arguments:

import {  } from '@nyx-discord/djs';
import type {  } from '@nyx-discord/djs';
import { , type  } from 'discord.js';

export class  extends  {
  protected readonly  = new ()
    .('ping')
    .('Replies with pong')
    .();

  async (: , : ) {
    await .('Pong!');
  }
}

Alternatively, you can specify a static create(bot) factory:

import {  } from '@nyx-discord/djs';
import type { ,  } from '@nyx-discord/djs';
import {  } from 'discord.js';

export class  extends  {
  protected readonly  = new ()
    .('ping')
    .('Replies with pong')
    .();

  public static (: <any>) {
    return new ();
  }

  // ...
}

The loader will call static create instead of the constructor if it exists. This allows you to access the bot instance, or add logic, for example to inject services or other dependencies with a DI container.

Edit on GitHub

Last updated on

On this page