Replying to slash commands

Discord provides developers the option to create client-integrated slash commands. In this section, we'll cover how to respond to these commands using discord.js!

TIP

You need at least one slash command registered on your application to continue with the instructions on this page. If you haven't done that yet, refer to the previous page.

Receiving interactions

Every slash command is an interaction, so to respond to a command, you need to set up an event listener that will execute code when your application receives an interaction:

client.on('interactionCreate', interaction => {
	console.log(interaction);
});
1
2
3

However, not every interaction is a slash command (e.g. MessageComponents). Make sure to only receive slash commands by making use of the CommandInteraction#isCommand() method:

client.on('interactionCreate', interaction => {
	if (!interaction.isCommand()) return;
	console.log(interaction);
});

 


1
2
3
4

Responding to a command

There are multiple ways of responding to a slash command, each of these are covered in the following segments. The most common way of sending a response is by using the CommandInteraction#reply() method:

WARNING

Initially an interaction token is only valid for three seconds, so that's the timeframe in which you are able to use the CommandInteraction#reply() method. Responses that require more time ("Deferred Responses") are explained later in this page.

client.on('interactionCreate', async interaction => {
	if (!interaction.isCommand()) return;

	if (interaction.commandName === 'ping') {
		await interaction.reply('Pong!');
	}
});
 


 
 
 

1
2
3
4
5
6
7

Restart your bot and then send the command to a channel your bot has access to. If all goes well, you should see something like this:

User used /ping
Guide Bot07/12/2021
Pong!

You've successfully sent a response to a slash command! This is only the beginning, there's more to look out for so let's move on to further ways of replying to a command!

Ephemeral responses

You may not always want everyone who has access to the channel to see a slash command's response. Thankfully, Discord implemented a way to hide messages from everyone but the executor of the slash command. This type of message is called ephemeral and can be set by using ephemeral: true in the InteractionReplyOptions, as follows:

client.on('interactionCreate', async interaction => {
	if (!interaction.isCommand()) return;

	if (interaction.commandName === 'ping') {
		await interaction.reply({ content: 'Pong!', ephemeral: true });
	}
});




 


1
2
3
4
5
6
7

Now when you run your command again, you should see something like this:

User used /ping
Guide Bot07/12/2021
Pong!
Only you can see this

Editing responses

After you've sent an initial response, you may want to edit that response for various reasons. This can be achieved with the CommandInteraction#editReply() method:

WARNING

After the initial response, an interaction token is valid for 15 minutes, so this is the timeframe in which you can edit the response and send follow-up messages.

const wait = require('util').promisify(setTimeout);

client.on('interactionCreate', async interaction => {
	if (!interaction.isCommand()) return;

	if (interaction.commandName === 'ping') {
		await interaction.reply('Pong!');
		await wait(2000);
		await interaction.editReply('Pong again!');
	}
});
 






 
 


1
2
3
4
5
6
7
8
9
10
11

Deferred responses

As previously mentioned, you have three seconds to respond to an interaction before its token becomes invalid. But what if you have a command that performs a task which takes longer than three seconds before being able to reply?

In this case, you can make use of the CommandInteraction#defer() method, which triggers the <application> is thinking... message and also acts as initial response. This allows you 15 minutes to complete your tasks before responding.

const wait = require('util').promisify(setTimeout);

client.on('interactionCreate', async interaction => {
	if (!interaction.isCommand()) return;

	if (interaction.commandName === 'ping') {
		await interaction.defer();
		await wait(4000);
		await interaction.editReply('Pong!');
	}
});






 
 
 


1
2
3
4
5
6
7
8
9
10
11

If you have a command that performs longer tasks, be sure to call defer() as early as possible.

You can also pass an ephemeral flag to the InteractionDeferOptions:

await interaction.defer({ ephemeral: true });
1

Follow-ups

Replying to slash commands is great and all, but what if you want to send multiple responses instead of just one? Follow-up messages got you covered, you can use CommandInteraction#followUp() to send multiple responses:

WARNING

After the initial response, an interaction token is valid for 15 minutes, so this is the timeframe in which you can edit the response and send follow-up messages.

client.on('interactionCreate', async interaction => {
	if (!interaction.isCommand()) return;

	if (interaction.commandName === 'ping') {
		await interaction.reply('Pong!');
		await interaction.followUp('Pong again!');
	}
});





 


1
2
3
4
5
6
7
8

If you run this code you should end up having something that looks like this:

User used /ping
Guide Bot07/12/2021
Pong!
Guide Bot Bot Pong!
Guide Bot07/12/2021
Pong again!

You can also pass an ephemeral flag to the InteractionReplyOptions:

await interaction.followUp({ content: 'Pong again!', ephemeral: true });
1
User used /ping
Guide Bot07/12/2021
Pong!
Guide Bot Bot Pong!
Guide Bot07/12/2021
Pong again!
Only you can see this

That's all, now you know everything there is to know on how to reply to slash commands!

TIP

Interaction responses can use masked links (e.g. [text](http://site.com)) and global emojis in the message content.

Parsing options

In this section, we'll cover how to access the values of a command's options. Let's assume you have a command that contains the following options:

const data = {
	name: 'ping',
	description: 'Replies with Pong!',
	options: [
		{
			name: 'input',
			description: 'Enter a string',
			type: 'STRING',
		},
		{
			name: 'num',
			description: 'Enter an integer',
			type: 'INTEGER',
		},
		{
			name: 'choice',
			description: 'Select a boolean',
			type: 'BOOLEAN',
		},
		{
			name: 'target',
			description: 'Select a user',
			type: 'USER',
		},
		{
			name: 'destination',
			description: 'Select a channel',
			type: 'CHANNEL',
		},
		{
			name: 'muted',
			description: 'Select a role',
			type: 'ROLE',
		},
	],
};



 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

You can get() these options from the CommandInteraction#options Collection:

const { value: string } = interaction.options.get('input');
const { value: integer } = interaction.options.get('num');
const { value: boolean } = interaction.options.get('choice');
const { user } = interaction.options.get('target');
const { member } = interaction.options.get('input');
const { channel } = interaction.options.get('destination');
const { role } = interaction.options.get('muted');

console.log([string, integer, boolean, user, member, channel, role]);
1
2
3
4
5
6
7
8
9

TIP

If you want the snowflake of a structure instead, access it via the value property.

Fetching and deleting responses

DANGER

You cannot fetch nor delete an ephemeral message.

In addition to replying to a slash command, you may also want to delete the initial reply. You can use CommandInteraction#deleteReply() for this:

await interaction.reply('Pong!');
await interaction.deleteReply();

 
1
2

Lastly, you may require the Message object of a reply for various reasons, such as adding reactions. You can use the CommandInteraction#fetchReply() method to fetch the Message instance of an initial response:

await interaction.reply('Pong!');
const message = await interaction.fetchReply();
console.log(message);
1
2
3