1
votes

I currently have a Discord bot which reads information of a API that deals with a game server panel. In this Bot there is tasks that are to start/restart/stop/kill the server. i want to give the option for the end user to react to a embed posted by a bot with a certain reaction to trigger these tasks. The Command and embed that are posted by the bot are:

enter image description here

The code which currently checks which reaction has been triggered looks like this:

message.channel.send(embed).then(async (sentEmbed) => {
                                sentEmbed.react("????")
                                sentEmbed.react("????")
                                sentEmbed.react("????")
                                sentEmbed.react("❌")
                                const filter = (reaction, user) => {
                                    console.log(reaction.emoji.name)
                                    return reaction.emoji.name === '????' && user.id === message.author.id;
                                };
                                const collector = sentEmbed.createReactionCollector(filter, {time: 20000});

                                collector.on('collect', (reaction, user) => {
                                    Client.startServer(args[0]).then((response) => {
                                        const start_embed = new Discord.MessageEmbed()
                                            .setTitle(response)
                                            .setColor(settings.embed.color.default)
                                            .setFooter(settings.embed.footer);
                                        message.channel.send(start_embed);

                                    }).catch((error) => {
                                        message.channel.send(client.embederror(error))
                                    });
                                });
                                const filter2 = (reaction, user) => {
                                    return reaction.emoji.name === '????' && user.id === message.author.id;
                                };
                                const collector2 = sentEmbed.createReactionCollector(filter2, {time: 20000});
                                collector.on('collect', (reaction, user) => {
                                    Client.restartServer(args[0]).then((response) => {
                                        const restart_embed = new Discord.MessageEmbed()
                                            .setTitle(response)
                                            .setColor(settings.embed.color.default)
                                            .setFooter(settings.embed.footer);
                                        message.channel.send(restart_embed);
                                    }).catch((error) => {
                                        message.channel.send(client.embederror(error))
                                    });
                                });
                                const filter3 = (reaction, user) => {
                                    return reaction.emoji.name === '????' && user.id === message.author.id;
                                };
                                const collector3 = sentEmbed.createReactionCollector(filter3, {time: 30000});
                                collector.on('collect', (reaction, user) => {
                                    console.log(`User Stopped There Server`);
                                });
                                const filter4 = (reaction, user) => {
                                    return reaction.emoji.name === '❌' && user.id === message.author.id;
                                };
                                const collector4 = sentEmbed.createReactionCollector(filter4, {time: 30000});
                                collector.on('collect', (reaction, user) => {
                                    console.log(`User Killed There Server`);
                                });
                            })

This code works for detecting the reactions onto the message, however when a user reacts with any reaction it runs all of the trigger code, so the bot posts two embeds which i have defined as what the bot should output when a reaction is triggered.

I just want the user to click one reaction, then the bot does something, then the use can click another and the bot does something else.

Thanks in advance

1
First beautify your code, second why do you have 4 filters and reaction collectors? Just have one filter that accepts all 4 emojis and requires the user.id to be msg.author.id, and then on the collector you would check what the emoji name is, and use a switch/if statementsuser13429955

1 Answers

1
votes

The issue was you used collector.on("collect") 4 times instead of using collector2, collector3 and collector4

It's best not to have variables like that since like it shows you might get confused

Plus you should not have 4 different filters and collectors, especially since you repeat a lot of the code

const validEmojis = ['🟩', '🔁', '🟥', '❌'];

const filter = (reaction, user) => {
  return validEmojis.includes(reaction.emoji.name) && user.id === message.author.id;
};

const collector = sentEmbed.createReactionCollector(filter, { time: 20000, maxEmojis: 1 });

collector.on('collect', (reaction, user) => {
  const name = reaction.emoji.name;
  //you only use it in two cases but I assume you will use it for all later on
  const embed = new Discord.MessageEmbed()
    .setColor(settings.embed.color.default)
    .setFooter(settings.embed.footer);

  if (name === '🟩' || name === '🔁') {
    const method = name === '🟩' ? "startServer" : "restartServer";
    Client[method](args[0])
      .then(response => {
        embed.setTitle(response);
        message.channel.send(start_embed);
      }).catch((error) => {
        message.channel.send(client.embederror(error))
      });
  } else if (name === '🟥') {
    console.log(`User Stopped There Server`);
  } else if (name === '❌') {
    console.log(`User Killed There Server`);
  }
});