Home > other >  how to set a default value for the dropdown menu with discord.js v13
how to set a default value for the dropdown menu with discord.js v13

Time:11-11

I am making a help command that when you execute it, shows an embed with a dropdown menu to select a category. The code works perfectly fine but I just wanted to know how to set a default value for the dropdown menu. Can anyone help me with that?

my code is:

const helpEmbed = new Discord.MessageEmbed()
            .setColor(696969)
            .setTitle("No category selected")

        const select = new Discord.MessageSelectMenu()
            

        for (const category of categoriesFolders) {
            const len = category.length;

            if (fs.readdirSync(`./commands/${category}`).length < 1) {continue};
            select.addOptions([{
                label: uppercaseFirst(category.substring(0, len - '-commands'.length)),
                value: category.substring(0, len - '-commands'.length)
            }]);
        }
        
        const row = new Discord.MessageActionRow().addComponents(select);
        
        const filter = (interaction) =>
            interaction.isSelectMenu() &&
            interaction.user.id === message.author.id;

        const collector = message.channel.createMessageComponentCollector({
            filter
        })

        collector.on('collect', async(collected) => {
            const value = collected.values[0];

            const embed = new Discord.MessageEmbed()
                .setColor(696969)


            const fls = fs.readdirSync(`./commands/${collected.values[0]}-commands`).filter(file => file.endsWith('.js'));
            for (const cmd of fls) {
                const command = require(`../${collected.values[0]}-commands/${cmd}`)
        
                let val = `${command.name}`
                embed.addField(val, command.description);
            }

            collected.message.edit({ embeds: [embed] }).catch(() => {});
            await collected.deferUpdate();
        })

        message.channel.send({ embeds: [helpEmbed], components: [row] });

CodePudding user response:

To make one of dropdown menu values default you need to add default to one of your values and set it to true!

  • Related