Commands
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:
- Root-level commands are loaded as-is. They must be
AbstractStandaloneCommand,AbstractParentCommand, orAbstractContextMenuCommand. - A
AbstractParentCommandmay have a children folder with the same name as itsdata.name. The children folder must containAbstractSubCommandand/orAbstractSubCommandGroupclasses, which are wired to the parent. - A
AbstractSubCommandGroupmay have a nested children folder with the same name as itsdata.name. The nested children folder must containAbstractSubCommandclasses, which are wired to the group. - Any subdirectory that does not match a parent/group
data.nameis 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. - 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
Assuming the exported command classes have their data.name set the same as their file names, the loader will produce the following command hierarchy:
| File | Type | Notes |
|---|---|---|
ping.js | AbstractStandaloneCommand | Root-level, loaded as-is |
report-user.js | AbstractContextMenuCommand | Root-level, loaded as-is |
mod.js | AbstractParentCommand | data.name === "mod", children folder matches name |
mod/ban.js | AbstractSubCommand | Inside children folder, wired to parent |
mod/settings.js | AbstractSubCommandGroup | data.name === "settings", nested folder matches name |
mod/settings/view.js | AbstractSubCommand | Inside children folder, wired to group |
utils/utils.js | AbstractStandaloneCommand | Inside organizational folder, loaded as root-level |
utils/format.js | AbstractStandaloneCommand | Inside 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.
Last updated on
