Search code examples
javascriptdiscorddiscord.jsbots

How do I make a dropdown help embed that changes when you select an option?


I am making a Discord bot with discord.js. I want to make a help menu which has a drop down. When a user selects an option I want the help embed to change accordingly to the option to show the list of commands for that category. Here is my code:

const{ Discord, MessageActionRow, MessageSelectMenu} =
require("discord.js")

const {MessageEmbed} = require("discord.js")

module.exports = {
    config: {
        name: 'help',
        description: 'Get a list of all the commands',
        timeout: "5000",
        usage: `!ping`
    },
    
    run: async (client, message, args) => {
    
        const row = new MessageActionRow()
        .addComponents(
          new MessageSelectMenu()
          .setCustomId("test") // this is the id i made in ../../events/interactionCreate
          .setPlaceholder('Dropdown')
          .setMinValues(1)
          .setMaxValues(1)
          .addOptions([
            {
              label:"Main" ,
              description:"View our main commands.",
              value:"main",
              emoji:"👑" // This can also be replaced with a custom emoji
            },
            {
              label:"Utility",
              description:"View our utility commands.",
              value:"utility",
              emoji:"🔧" // Same as above
            },
            
            ])
          )
        
        const embed = new MessageEmbed ()
        .setTitle("Help")
        .setDescription("Select a catagory to see a list of commands for it.")
        
        message.channel.send({embeds: [embed], components: [row]})
        
      }
    }

Solution

  • You simply need to create a MessageComponentCollector, and when a button is pressed you edit your embed.

    // we store the message in a variable to create the collector on it
    const MSG = await message.channel.send({embeds: [embed], components: [row]});
    // filter out bots
    const filter = i => !i.user.bot;
    // create the collector on your message
    const collector = MSG.createMessageComponentCollector({ filter, time: 35000 });
    
    collector.on("collect", i => {
    
      // only allow the author to interact
      if(i.member.id != message.author.id) {
        return i.reply({ content: `${i.member} This Menu is not for you`, ephemeral: true})
      }
      // access your menu option by using i.values[index]
      if(i.values[0] === "utility") {
        embed.setDescription("Your new modified embed")
        MSG.edit({ embeds: [embed] });
      }
    })