SubCommands
Subcommands are executable commands stored inside a Parent Command or a SubCommand Group.
While they don't depend on a specific bot, they do depend on their parent (a parent command or a group).
Creation
Implementation
Implement data that stores the command's data, and execute() that executes the command.
import { , type } from 'discord.js';
import { } from '@nyx-discord/djs';
class extends {
protected readonly = new ()
.('my-command')
.('My command')
.();
public async (: ) {
await .('Hello!');
}
}Addition to its parent
Instantiate and add it to a parent command or a subcommand group.
const = new ();
.();Or do it in the parent or group's class:
import { , } from '@nyx-discord/djs';
import { } from 'discord.js';
// On a parent command:
class extends {
constructor() {
super();
this.(new (this));
}
}
// Or on a subcommand group:
class extends {
constructor(: ) {
super();
this.(new (this));
}
}Then it'll show up when the top-level parent command is registered.
Execution Meta
The second argument of the execute method is a Metadata object, which is a Record that stores metadata about the command call.
Apart from the keys saved by the caller or the middleware, the dispatch meta contains:
- The execution ID, via the
TypedFields.Idfield. - The execution start date, via the
TypedFields.CreationDatefield. - The bot whose
Clientsent the interaction, via theTypedFields.Botfield.
To learn more about TypedFields, check the Metadata page.
Autocompletion
If your subcommand has autocompletable options, you can implement the autocomplete() method.
import { } from '@nyx-discord/djs';
import type { } from 'discord.js';
class extends {
public async (: ) {
await .([
{ : 'First option', : 'first' },
{ : 'Second option', : 'second' },
]);
}
}Metadata
When extending nyx with plugins, you may want to specify extra data for them. You can do so with the Metadata object, saved on the subcommand.
While subcommands internally save a Metadata, the return type of #getMeta() is a ReadonlyMetadata,
meaning that external objects can read the object, but not modify it.
import { } from '@nyx-discord/djs';
class extends {
protected override readonly = {
: 'someValue',
};
}
const = new ();
const = .();
const = ['someKey']; // 'someValue'For a more type-safe approach, you can use TypedFields. Check the Metadata page for more information.
Advanced features
Command components
Handle components interactions directly in the subcommand that sent them, without a separate interactionCreate listener.
Filters
Add pre-execution checks to your subcommand, like permissions or cooldowns.
Middlewares
Run code before every command's execution, like logging or pre-conditions.
