I am making a Discord bot, and one of my commands has a select menu. But I can't seem to figure out how to actually have a different response to the different options.
Here is the code for the interaction create
client.on('interactionCreate', async (click) => {
if (!click.isSelectMenu() || click.user.id != interaction.user.id || click.guildId != interaction.guildId) return;
});
So, I want to reply to the different options on my command. One way I think would work was:
if (click.values[0]) {
// do something
} else if (click.values[1]) {
// do something else
}
But this did the action at click.values[0] every time.
On the docs, I also found a function called .createMessageComponentCollector, but this only works for message and since I am using slash commands, this is not defined.
How should I do this instead?
There are few things I would like to point you about
Every select menu has a customId so when using the interactionCreate event it is always recommended to have the id filtering other interactions of select menu
Next comes click.values, it returns an Array of values
Assuming your select menu provides multiple selections, its good to loop over values instead manually going over them unless there's a specific need for that
Reason for click.values[0] being true everytime is because everytime a user selects 1 or more value, it will always have the first one
let me explain it with an example
const array = ['foo', 'bar'];
// here your array has 2 elements
// array[0] will always be true because Boolean(non empty string) always truthy
// so your "else if" will never reach because first if is never false
Finally for the collector, you can create collector on interactions after fetching them example
const reply = await interaction.reply({ content: 'Some content', fetchReply: true });
// now reply is fetched and is of type Message or APIMessage
// you can now create a collector on it as documentation tells
Note: It is not recommended to make a collector for something that is very long timed (collectors are short lived) For example you should not use collectors for select menu roles as collectors dont stay if your bot restarts, instead use interactionCreate event for these long or never stopping components like button/select menus
Hope it helped you!
Here is the basics of a collector
client.on("interactionCreate", async (interaction) => {
if (interaction.isSelectMenu()) {
if (interaction.customId !== 'test') return; // If you have more that one select menu in code
const filter = (selectInteraction) => {
i.user.id === '123456789'; // add/remove filters according to your needs
};
const collector = interaction.channel.createMessageComponentCollector({
filter,
time: 10000 //in ms
max: 1
});
collector.on('collect', async i => {
if (i.customId === 'testid') {
const value = i.values[0]
if (value === 'first_value') {
// your code here
}
if (value === 'second_value') {
// your code here
}
});
collector.on('end', () => {
// your code
console.log('test')
})
})
Find more about collectors