Slash Commands

Let us use slash command this time .

This is the Basic syntax :

module.exports = {
    name : 'command-name',
    description : 'A-small-description-of-the-command',
    permissions : [Array of Permissions],
    slash : true,
    options : [Array of Options] , // (OPTIONAL) 
    type : type-of-the-slash-command , // (OPTIONAL) Default is "CHAT_INPUT"
    async execute({interaction , options}){
        // YOUR-CODE
    }
}

A basic example without "options" :

const { Permissions, Constants } = require("discord.js");

module.exports = {
    name : 'ping',
    description : 'Replies with a pong !',
    permissions : [Permissions.FLAGS.SEND_MESSAGES],
    type : Constants.ApplicationCommandTypes.CHAT_INPUT,
    slash : true,
    async execute({interaction}){

        if(interaction){
            await interaction.reply({content : 'Pong !'})
        }
    }
}

Output :

You will need the if construct for interaction because the command can be run both as a legacy command and a slash command . Thus it will result in an error if a legacy command is run in this case.

An Example with "options" :

const { Permissions, Constants } = require("discord.js");

module.exports = {
    name : 'echo',
    description : 'Echoes your words.',
    permissions : [Permissions.FLAGS.SEND_MESSAGES],
    type : Constants.ApplicationCommandTypes.CHAT_INPUT,
    options : [
        {
            name : 'text' , 
            description : 'Text',
            type : Constants.ApplicationCommandOptionTypes.STRING,
            required : true
        }
    ],
    slash : true,
    async execute({interaction , options}){

        if(interaction){
            const text = options.getString('text')
            await interaction.reply({content : `You said **${text}**`})
        }
    }
}

Output :

Last updated