Home > Net >  How to set select menu in individual file discord.js
How to set select menu in individual file discord.js

Time:06-10

I am making a Discord bot in discord.js and set the slash commands up in individual files, so I don't get lost in the code.

But one of those commands has a select menu in it, but since select menu's are an interaction on their own, it does not know what file to look for to find it's execution.

I use the following code to detect commands

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'Hmm, we appear to have run into an error. Please try again in a few minutes, if this error persists, please let us know!', ephemeral: true });
    }
});

You could do the same for select menu's

client.on('interactionCreate', async interaction => {
    if (!interaction.isSelectMenu()) return;

    if (interaction.customId === 'helpMenu') {
        await interaction.update('Worked!');
    }
});

But since my commands are in their own folders, this does not work.

I tried moving the code of responding to the interaction to my main file, but it does not recognize the customId, because the select menu is not defined in that file.

How am I supposed to do this? Or am I missing something really obvious?

Thanks in advance,

cheers

CodePudding user response:

One way you could do this would be to pass the client made in the main.js file as an argument to the command file where you created the MessageSelectMenu. Then, you could then create an event listener for any click on the MessageSelectMenu. But you would have to check whether the user id was the same as the one who ran the slash command and also check if the guild id was the same. An example is this:

interaction.reply({ components: [row] }); // Send the MessageSelectMenu
client.on("interactionCreate", (click) => {
  if (
    click.user.id !== interaction.user.id || // Check if the user who clicked the MessageSelectMenu was the same as the one who ran the slash command
    click.guildId !== interaction.guildId || // Check if the guild id where the user clicked the MessageSelectMenu was the same as the one where the slash command was run 
    !click.isSelectMenu() // Check if the interaction was a MessageSelectMenu click
  )
    return;
});
  • Related