Setting up Slash Commands
In order for slash commands to run when placed inside their respective sub-folders of the command directory, you need to set the object properties for each command based off the non-slash commands and also include slash method and properties shown below.
Usage
Here is a sample slash command example created using the previous ping.js command we created in the last doc page:
const { SlashCommandBuilder } = require("discord.js");
// I recommend you define the command name and description here to prevent
// retyping them if you decide to have prefix and slash command to call the cmd
const name = "ping";
const description = "Ping Pong Command";
module.exports = {
name,
description,
aliases: ['p'],
guildOnly: true,
permissions: 0,
minArgs: 0,
usage: '',
// defines as slash command using Discord.js SlashCommandBuilder
data: new SlashCommandBuilder()
.setName(name)
.setDescription(description),
execute(message, args, client) {
return message.channel.send({
content: 'Pong'
});
},
async interactionReply(interaction) { // define what the slash command does
await interaction.reply({
content: 'Pong!'
});
}
};
const { SlashCommandBuilder } = require("discord.js");
// I recommend you define the command name and description here to prevent
// retyping them if you decide to have prefix and slash command to call the cmd
const name = "ping";
const description = "Ping Pong Command";
module.exports = {
name,
description,
aliases: ['p'],
guildOnly: true,
permissions: 0,
minArgs: 0,
usage: '',
data: new SlashCommandBuilder()
.setName(name)
.setDescription(description)
.addUserOption((option) =>
option
.setName("someone")
.setDescription("Mention Someone")
.setRequired(true)
),
execute(message, args, client) {
return message.channel.send('Pong.');
},
async interactionReply(interaction) {
const { options } = interaction;
const user = await options.getUser('someone');
await interaction.reply({
content: `Hello ${user.username}!`
});
}
};Properties
data
new SlashCommandBuilder class object, You can read more about it in the Discord.js Documentation about SlashCommandBuilder and the Discord Developer Documentation.
Type: SlashCommandBuilder
Required: true
interactionReply(interaction, client, level)
This is the interaction async Function method used to execute slash command.
You are required by Discord to return await interaction.reply({...}) in order to display result of the slash command.
level
number
false
Permission level of user
returns: await interaction.reply(...);
Last updated