Skip to Content
nyx logonyx guide
Command TypesParent Commands

SubCommands

Reading time:2 min read

It's recommended to read the Commands Overview and Parent Commands pages first.

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

Creation

Extend the AbstractSubCommand class:

import {  } from '@nyx-discord/djs';
class  extends  {}

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.Id field.
  • The execution start date, via the TypedFields.CreationDate field.
  • The bot whose Client sent the interaction, via the TypedFields.Bot field.

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

Click on each card to learn more about that feature.

On this page