Section titled Creating slash commandsCreating slash commands

Discord allows developers to register slash commands, which provide users a first-class way of interacting directly with your application.

Slash commands provide a huge number of benefits over manual message parsing, including:

  • Integration with the Discord client interface.
  • Automatic command detection and parsing of the associated options/arguments.
  • Typed argument inputs for command options, e.g. "String", "User", or "Role".
  • Validated or dynamic choices for command options.
  • In-channel private responses (ephemeral messages).
  • Pop-up form-style inputs for capturing additional information.

...and many more!

For fully functional slash commands, there are three important pieces of code that need to be written. They are:

  1. The individual command files, containing their definitions and functionality.
  2. The command handler, which dynamically reads the files and executes the commands.
  3. The command deployment script, to register your slash commands with Discord so they appear in the interface.

These steps can be done in any order, but all are required before the commands are fully functional.

On this page, you'll complete Step 1. Make sure to also complete the other pages linked above!

Read first!

Section titled Before you continueBefore you continue

Assuming you've followed the guide so far, your project directory should look something like this:


_10
discord-bot/
_10
├── node_modules
_10
├── config.json
_10
├── index.js
_10
├── package-lock.json
_10
└── package.json

Section titled Individual command filesIndividual command files

Create a new folder named commands, which is where you'll store all of your command files.

At a minimum, the definition of a slash command must have a name and a description. Slash command names must be between 1-32 characters and contain no capital letters, spaces, or symbols other than - and _. Using the builder, a simple _ping_ command definition would look like this:


_10
/** @type {import('discord.js').RESTPostAPIApplicationCommandsJSONBody} */
_10
export const data = {
_10
name: 'ping',
_10
description: 'Replies with Pong!',
_10
};

A slash command also requires a function to run when the command is used, to respond to the interaction. Using an interaction response method confirms to Discord that your bot successfully received the interaction, and has responded to the user. Discord enforces this to ensure that all slash commands provide a good user experience (UX). Failing to respond will cause Discord to show that the command failed, even if your bot is performing other actions as a result.

The simplest way to acknowledge and respond to an interaction is the interaction.reply() method. Other methods of replying are covered on the Response methods page later in this section.


_10
/** @param {import('discord.js').CommandInteraction} interaction */
_10
export async function execute(interaction) {
_10
await interaction.reply('Pong!');
_10
}

@type and @param tags allow you to annotate your code with type information. The tags are not required for your code to run but provide autocomplete and type information for fields and parameters, which can majorly improve your developer experience when working with them.

@type and @param tags

Put these two together by creating a commands/ping.js file for your first command. Inside this file, you're going to define and export two items.

  • The data property, which will provide the command definition shown above for registering to Discord.
  • The execute method, which will contain the functionality to run from our event handler when the command is used.

The export keyword ensures these values can be imported and read by other files; namely the command loader and command deployment scripts mentioned earlier.

commands/ping.js

_10
/** @type {import('discord.js').RESTPostAPIApplicationCommandsJSONBody} */
_10
export const data = {
_10
name: 'ping',
_10
description: 'Replies with Pong!',
_10
};
_10
_10
/** @param {import('discord.js').CommandInteraction} interaction */
_10
export async function execute(interaction) {
_10
await interaction.reply('Pong!');
_10
}

module.exports is how you export data in Node.js so that you can require() it in other files.

If you need to access your client instance from inside a command file, you can access it via interaction.client. If you need to access external files, packages, etc., you should require() them at the top of the file.

Tip

That's it for your basic ping command. Below are examples of two more commands we're going to build upon throughout the guide, so create two more files for these before you continue reading.

commands/user.js
commands/server.js

_14
/** @type {import('discord.js').RESTPostAPIApplicationCommandsJSONBody} */
_14
export const data = {
_14
name: 'user',
_14
description: 'Provides information about the user.',
_14
};
_14
_14
/** @param {import('discord.js').CommandInteraction} interaction */
_14
export async function execute(interaction) {
_14
// interaction.user is the object representing the User who ran the command
_14
// interaction.member is the GuildMember object, which represents the user in the specific guild
_14
await interaction.reply(
_14
`This command was run by ${interaction.user.username}, who joined on ${interaction.member.joinedAt}.`,
_14
);
_14
}

Section titled Next stepsNext steps

You can implement additional commands by creating additional files in the commands folder, but these three are the ones we're going to use for the examples as we go on. For now let's move on to the code you'll need for command handling, to load the files and respond to incoming interactions.

Section titled Resulting codeResulting code