# @sapphire/plugin-subcommands

> Plugin for @sapphire/framework that adds support for subcommands.

Latest version **7.0.1** (published 2024-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sapphire/plugin-subcommands
pnpm add @sapphire/plugin-subcommands
yarn add @sapphire/plugin-subcommands
bun add @sapphire/plugin-subcommands
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 7.0.1 |
| Published | 2024-11-02 |
| First published | 2021-02-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=v18 |
| Dependencies | 1 |
| Unpacked size | 291.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 80 |
| Author | @sapphire |
| Maintainers | favna, vladfrangu, kyranet |
| Keywords | sapphiredev, plugin, bot, typescript, ts, yarn, discord, sapphire, subcommands |

## Links

- npm: https://www.npmjs.com/package/@sapphire/plugin-subcommands
- Repository: https://github.com/sapphiredev/plugins
- Homepage: https://github.com/sapphiredev/plugins/tree/main/packages/subcommands
- Issues: https://github.com/sapphiredev/plugins/issues
- npm.io page: https://npm.io/package/@sapphire/plugin-subcommands

## Dependencies (1)

- [@sapphire/utilities](https://npm.io/package/@sapphire/utilities.md) ^3.18.0

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 7.0.1 (latest) — 2024-11-02
- 7.0.2-next.77bb4e0 (next) — 2026-03-08
- 6.0.3-pr-534.00b527b.0 (pr-534) — 2024-01-06
- 6.0.2-pr-531.68fab22.0 (pr-531) — 2023-12-29
- 6.0.0-pr-513.cc0467d.0 (pr-513) — 2023-12-05
- 6.0.0-pr-512.86f3d06.0 (pr-512) — 2023-12-04
- 4.2.2-pr-475.0a017c0.0 (pr-475) — 2023-09-06
- 4.1.1-pr-469.d3a0e5e.0 (pr-469) — 2023-08-23
- 4.1.0-pr-465.0a92431.0 (pr-465) — 2023-08-12
- 4.0.2-pr-437.cfcbc0f.0 (pr-437) — 2023-05-03
- 3.3.0-pr-403.05623a4.0 (pr-403) — 2023-01-06
- 3.2.2-pr-363.e4e2a04.0 (pr-363) — 2022-09-25
- 2.2.3-pr-334.52c8c14.0 (pr-334) — 2022-08-08
- 3.0.0-pr-271.0258c7b7.0 (pr-271) — 2022-08-08
- 2.2.1-pr-285.d64fcad.0 (pr-285) — 2022-05-05
- … 863 more at https://npm.io/package/@sapphire/plugin-subcommands/versions

## README

<div align="center">

![Sapphire Logo](https://raw.githubusercontent.com/sapphiredev/assets/main/banners/SapphireCommunity.png)

# @sapphire/plugin-subcommands

**Plugin for <a href="https://github.com/sapphiredev/framework">@sapphire/framework</a> so your commands can have subcommands.**

[![GitHub](https://img.shields.io/github/license/sapphiredev/plugins)](https://github.com/sapphiredev/plugins/blob/main/LICENSE.md)
[![npm bundle size](https://img.shields.io/bundlephobia/min/@sapphire/plugin-subcommands?logo=webpack&style=flat-square)](https://bundlephobia.com/result?p=@sapphire/plugin-subcommands)
[![npm](https://img.shields.io/npm/v/@sapphire/plugin-subcommands?color=crimson&logo=npm&style=flat-square)](https://www.npmjs.com/package/@sapphire/plugin-subcommands)

</div>

## 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!

-   [`@sapphire/framework`](https://www.npmjs.com/package/@sapphire/framework)
-   [`@sapphire/utilities`](https://www.npmjs.com/package/@sapphire/utilities)
-   [`discord.js`](https://www.npmjs.com/package/discord.js)

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

```sh
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**

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

**TypeScript**

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

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

```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:_

```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](https://www.sapphirejs.dev/docs/Guide/plugins/Subcommands/getting-started)

## 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 With   |                       Address                       |
| :-------------: | :-------------------------------------------------: |
| Open Collective | [Click Here](https://sapphirejs.dev/opencollective) |
|      Ko-fi      |      [Click Here](https://sapphirejs.dev/kofi)      |
|     Patreon     |    [Click Here](https://sapphirejs.dev/patreon)     |
|     PayPal      |     [Click Here](https://sapphirejs.dev/paypal)     |

## Contributors

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

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

<a href="https://github.com/sapphiredev/plugins/graphs/contributors">
  <img src="https://contrib.rocks/image?repo=sapphiredev/plugins" />
</a>

[contributing]: https://github.com/sapphiredev/.github/blob/main/.github/CONTRIBUTING.md

---
_Source: https://npm.io/package/@sapphire/plugin-subcommands · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
