Skip to main content

Sapphire Logo

@sapphire/plugin-subcommands

Plugin for @sapphire/framework so your commands can have subcommands.

GitHub npm bundle size npm

Description

Subcommands are a way to split 1 command into multiple. This can in particular be very useful for configuration commands with subcommands such as set, reset and remove.

Features

  • Fully ready for TypeScript!
  • Includes ESM ready entrypoint
  • Type generics for easy extension in TypeScript
  • Input/Output mapping

Installation

@sapphire/plugin-subcommands depends on the following packages. Be sure to install these along with this package!

You can use the following command to install this package, or replace npm install with your package manager of choice.

npm install @sapphire/plugin-subcommands @sapphire/framework @sapphire/utilities discord.js

Usage

Start by importing the registration file in your project to use the plugins error loggers:

JavaScript

require('@sapphire/plugin-subcommands/register');

TypeScript

import '@sapphire/plugin-subcommands/register';

Then, you can create new subcommands by extending the Subcommand class. With TypeScript:

import { Subcommand } from '@sapphire/plugin-subcommands';
import { ApplyOptions } from '@sapphire/decorators';
import type { Args } from '@sapphire/framework';
import type { Message, CommandInteraction } from 'discord.js';

// Using ApplyOptions decorator makes it easy to configure
@ApplyOptions<Subcommand.Options>({
subcommands: [
{
name: 'add',
messageRun: 'messageAdd',
chatInputRun: 'chatInputAdd'
},
{
name: 'remove',
messageRun: 'messageRemove',
chatInputRun: 'chatInputRemove'
},
{
name: 'list',
messageRun: 'messageList',
chatInputRun: 'chatInputList'
}
]
})
// Extend `Subcommand` instead of `Command`
export class UserCommand extends Subcommand {
public async messageAdd(message: Message, args: Args) {}

public async messageRemove(message: Message, args: Args) {}

public async messageList(message: Message, args: Args) {}

public async chatInputAdd(interaction: Subcommand.ChatInputInteraction) {}

public async chatInputRemove(interaction: Subcommand.ChatInputInteraction) {}

public async chatInputList(interaction: Subcommand.ChatInputInteraction) {}
}

With JavaScript:

const { Subcommand } = require('@sapphire/plugin-subcommands');

// Extend `Subcommand` instead of `Command`
module.exports = class UserCommand extends Subcommand {
constructor(context, options) {
super(context, {
...options,
subcommands: [
{
name: 'add',
messageRun: 'messageAdd',
chatInputRun: 'chatInputAdd'
},
{
name: 'remove',
messageRun: 'messageRemove',
chatInputRun: 'chatInputRemove'
},
{
name: 'list',
messageRun: 'messageList',
chatInputRun: 'chatInputList'
}
]
});
}

async messageAdd(message, args) {}

async messageRemove(message, args) {}

async messageList(message, args) {}

async chatInputAdd(interaction) {}

async chatInputRemove(interaction) {}

async chatInputList(interaction) {}
};

For more documentation please refer to guide

Buy us some doughnuts

Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!

We accept donations through Open Collective, Ko-fi, Paypal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.

Donate WithAddress
Open CollectiveClick Here
Ko-fiClick Here
PatreonClick Here
PayPalClick Here

Contributors

Please make sure to read the Contributing Guide before making a pull request.

Thank you to all the people who already contributed to Sapphire!