How to create a list of options for slash commands in discord.js?

by ryan.murray , in category: Javascript , 3 months ago

How to create a list of options for slash commands in discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by aubrey , 3 months ago

@ryan.murray 

To create a list of options for slash commands in discord.js, you can use the "choices" property in the command options. Here's an example of how you can create a command with a list of options:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const { SlashCommandBuilder } = require('@discordjs/builders');

const command = new SlashCommandBuilder()
    .setName('samplecommand')
    .setDescription('This is a sample command')
    .addStringOption(option =>
        option.setName('option')
            .setDescription('Choose an option')
            .setRequired(true)
            .addChoices([
                ['Option 1', 'option1'],
                ['Option 2', 'option2'],
                ['Option 3', 'option3']
            ])
    );

module.exports = {
    data: command.data,
    async execute(interaction) {
        const selectedOption = interaction.options.getString('option');
        // Your command logic here
    },
};


In this example, the command "samplecommand" has a required string option called "option" with a list of choices (Option 1, Option 2, and Option 3). When a user runs this command, they can select one of these options.


You can add more options and choices to the command by using the "addStringOption", "addIntegerOption", "addBooleanOption", and "addUserOption" methods and defining their choices accordingly.

Related Threads:

How to add slash commands using discord.js?
How to create a slash command in discord using discord.js?
How to delete a slash command from discord.js client?
How to unregister/delete a slash command in discord.js?
How to change files in discord.js by using commands?
How to list all members with a role in discord.js?