0
votes

So, I got my code and it works just as I want it to. the message pops up changes everything, it's perfect.
Now I want to add so the bot knows when I react to its message and then does something else. What I mean is: bot sends a message with reacts, and whenever some user clicks the reaction something happens, but I have no idea how to do that.

I've tried many things like if (reaction.emoji.name === ':bomb:'), but multiple errors popped out and I didn't know how to fix that. Here's the code:

const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
var lastbuffer;
lastbuffer = 0;
const client = new Discord.Client();
client.once('ready', () => {
  console.log('Ready!');

});
client.on('message', message => {
  if(message.content.startsWith(`${prefix}start`)){
    message.delete()
    setInterval(function(){
      lastbuffer++;
      const Buffer = new Discord.MessageEmbed()
      .setColor('#8300FF')
      .setTitle("**It's time to check buffers!**")
      .setDescription("**It's been **" + "`" + lastbuffer + " Hour" + "`" + "** since last buffercheck, <@&675688526460878848>**." + " **Check now!**")
      .setThumbnail('https://art.pixilart.com/88534e2f28b65a4.png')
      .setFooter('WEEEEEWOOOOO')
      .setTimestamp();
      client.channels.cache.get("700296799482675230").send(Buffer).then(msg => {
        msg.react('✅');
        msg.react('????');
        msg.delete({timeout: 4000})
      });
    }, 5000)
  }
}); 

client.login(token);
2
"Multiple errors popped out" sounds interesting. Can you share even one of these errors?Nico Haase
can’t tell you now, i managed to fix that nowwex

2 Answers

1
votes

You are going to have to use a ReactionCollector using the createReactionCollector() method.

You can follow this guide to under ReactionCollectors better

1
votes

You need to use a reaction collector.

client.channels.cache.get("700296799482675230").send(Buffer).then(async msg => {
  // I'm using await here so the emojis react in the right order
  await msg.react('✅');
  await msg.react('💣');
  msg.awaitReactions(
    // only collect the emojis from the message author
    ({emoji}, user) => ['✅', '💣'].includes(emoji.name) && user.id === message.author.id,
    // stop collecting when 1 reaction has been collected or throw an error after 4 seconds
    {max: 1, time: 4000, errors: ['time']}
  )
    .then(collected => {
      const reaction = collected.first()
      // do something
    })
    .catch(() => {
      // I'm assuming you want to delete the message if the user didn't react in time
      msg.delete()
    })

What this code does:

  • Sends the embed (Buffer) to the channel with the id 700296799482675230
  • Reacts with the ✅ and then the 💣 emojis on the message with the embed
  • Waits for a ✅ or 💣 reaction from the author of the original message
    • If the user reacts within 4 seconds, runs the // do something part
    • If the user does not react within 4 seconds, deletes the message with the embed