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!'
		});
	}
};

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.

PropertyTypeRequiredDescription

interaction

true

Represent an Discord Interaction

client

false

This is the discord client object.

level

number

false

Permission level of user

returns: await interaction.reply(...);

interaction.reply takes in the same argument as message.reply

Last updated