Search code examples
javascriptnode.jsdiscord.js

How do I specify options for commands?


I want to specify choices for an option for my command in Discord.js. How do I do that?

The command:

module.exports = {
            name: 'gifsearch',
            description: 'Returns a gif based on your search term.',
          options: [{
            name: 'type',
            type: 'STRING',
            description: 'Whether to search for gifs or stickers.',
            choices: //***this is the area where I have a question***
            required: true
          },
          {
            name: 'search_term',
            type: 'STRING',
            description: 'The search term to use when searching for gifs.',
            required: true,
          }],
            async execute(interaction) {
            let searchTerm = interaction.options.getString('search_term')
                
            const res = fetch(`https://api.giphy.com/v1/gifs/search?q=${searchTerm}&api_key=${process.env.giphyAPIkey}&limit=1&rating=g`)
            .then((res) => res.json())
            .then((json) => {
              if (json.data.length <= 0) return interaction.reply({ content: `No gifs found!` })
              interaction.reply({content: `${json.data[0].url}`})
            })
            },
    };

I have read the discord.js documentation/guide and I know about the .addChoice() method, but it doesn't look like that will be compatible with my bot's current code.


Solution

  • The discord.js api describes this as ApplicationCommandOptionChoices. So you basically just insert an array of this in your choices.

    module.exports = {
          ...
          choices: [
            {
              name: "name to display",
              value: "the actual value"
            },
            {
              name: "another option",
              value: "the other value"
            }
          ]
          ...
        };