Search code examples
javascriptdiscorddiscord.js

DiscordJS v14 Slash commands option depends on option


I want to make a Discord slash command where you need to select one choice and then another choice.

But depending of what choice is made in the first place (in choice 1) the options/values in the choice 2 are changing.

Is that possible?

Example: /command choice1 choice2

If I select "A" in choice1 I get "One" and "Two" as choices in choice. But when I select "B" in choice1, I will get "Three" and "Four" as choice2 options.

Best Regards,

Paul


Solution

  • Choices are predetermined values, that are set when the command is registered, and cannot be changed afterwords.

    The way to dynamically change option choices is through "autocomplete," or depending on the situation, "subcommands" could also work.

    In order to set up autocomplete to change based on other values, you'd need to access interaction.options.get(choice2) for choice1 and interaction.options.get(choice1) for choice2 (the interaction here would be an AutocompleteInteraction)

    Here's an example:

    client.on('interactionCreate', async interaction => {
        if (!interaction.isAutocomplete()) return;
    
        if (interaction.commandName === 'command') {
            const focusedOption = interaction.options.getFocused(true);
            let choices;
    
            if (focusedOption.name === 'option1') {
                choices = ["A", "B"];
            }
    
            if (focusedOption.name === 'option2') {
                let option1 = interaction.options.get('option1')?.value;
                if (option1 === "A") {
                    choices = ["One", "Two"];
                }
                else if (option1 === "B") {
                    choices = ["Three", "Four"];
                }
                else {
                    choices = ["One", "Two", "Three", "Four"];
                }
            }
    
            const filtered = choices.filter(choice => choice.startsWith(focusedOption.value));
            await interaction.respond(
                filtered.map(choice => ({ name: choice, value: choice })),
            );
        }
    });
    

    As with all interactions, you must respond within 3 seconds. But you cannot defer AutocompleteInteractions, so I'd recommend .catch()ing the interaction response in case of errors.

    See here for more info:

    [1] https://discordjs.guide/interactions/autocomplete.html

    [2] https://discord.js.org/#/docs/main/main/class/AutocompleteInteraction