Search code examples
javascriptdiscord.jsslash

Discord.js v13 choices in options - slash commands


in discordjs.guide is how to set choice in option. But there isn't how to get the choice and how to use it with code. I have /animal command with choices (cat, dog, etc..) and when user click on dog, it send dog picture, same with cat. But i don't know where to put the code for image. I tried and this isn't work, writes This interaction failed. How to do it? Thanks (there is text instead of image for testing)

module.exports = {
    data: new SlashCommandBuilder()
        .setName('animal')
        .setDescription('Sends a animal')
        .addStringOption(option =>
            option.setName('animal')
                .setDescription('Select animal for photo')
                .setRequired(true)
                .addChoice('Cat', 'cat')
                .addChoice('Dog', 'dog')),
    async execute(interaction) {
        if (interaction.options.getString() === 'cat') {
            await interaction.reply('cat')
        } else if (interaction.options.getString() === 'dog') {
            await interaction.reply("dog")
        }
    }
}

Solution

  • Its simple. You normally do interaction.options.getString('animal');

    It will give you cat if the user chooses Cat. So basically it is same as how you normally get a option value