Skip to content

Discord.JS How to wait for member reaction

I am making bot to manage my multiple Discord guilds. And I’d like to make confirmation system, like:

  • User does X thing,
  • Bot sends message in sufficient channel,
  • Bot waits for user to react with :thumbdsup: or :thumbsdown: up to 60 seconds
  • If thumbs up, do A, else – do B. If time is up, do C action

How can I build system like that, due to I have no idea.

Answer

Adding and Setting Up the Event Listener

First we start by defining discord.js & adding an event listener:

const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot

client.on('message', async message => {  //when a user sends a message

});

Then you would need to tell the bot what it does after that:

const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot

client.on('message', async message => {  //when a user sends a message
    if (message.author.bot) return; //If a bot sent the message we want to ignore it.

    if (message.content.toLowerCase() === 'what message your looking for' {  //what your looking for (make sure this string is all in lower case)
        //what you want the bot to do after the message you are looking for has been sent
    }
});

Now, if you want the bot to add the reaction to the message you will do the following:

const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot

client.on('message', async message => {  //when a user sends a message
    if (message.author.bot) return; //If a bot sent the message we want to ignore it.

    if (message.content.toLowerCase() === 'what message your looking for' {  //what your looking for (make sure this string is all in lower case)
        await message.react('👍'); //reacting to the message with a thumbs up emoji
        await message.react('👎'); //reacting to the message with a thumbs down emoji
    }
});

If you wanted the bot to reply to the message use:

const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot

client.on('message', async message => {  //when a user sends a message
    if (message.author.bot) return; //If a bot sent the message we want to ignore it.

    if (message.content.toLowerCase() === 'what message your looking for' {  //what your looking for (make sure this string is all in lower case)
        message.channel.send('The bots message here') //what you want the bot to reply with
    }
});

Awaiting Reactions

Here it all depends on if you want to await reactions on the bot’s message or the user’s message.
If you would like to await reactions from the bot’s message use:

const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot

client.on('message', async message => {  //when a user sends a message
    if (message.author.bot) return; //If a bot sent the message we want to ignore it.

    if (message.content.toLowerCase() === 'what message your looking for' {  //what your looking for (make sure this string is all in lower case)
        message = await message.channel.send('The bots message here') //waiting for the message to be sent

        const filter = (reaction, user) => { //filtering the reactions from the user
            return (
            ['👎', '👍'].includes(reaction.emoji.name) && user.id === message.author.id
            );
        }
        message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] }) //awaiting the reactions - remember the time is in milliseconds
        .then((collected) => {
        const reaction = collected.first();

        if (reaction.emoji.name === '👍') { //if the reaction was a thumbs up
            //A action
            reaction.users.remove(message.author.id) //If you wanted to remove the reaction
      } else { //if the reaction was a thumbs down
        //B action
        reaction.users.remove(message.author.id) //If you wanted to remove the reaction
      }
    }).catch((collected) => { //when time is up
      //C action
    });
    }
});


If you wanted to await message from the user’s message you would do the same thing except change:

if (message.content.toLowerCase() === 'what message your looking for' {  //what your looking for (make sure this string is all in lower case)
    message.channel.send('The bots message here') //sending the message but not awaiting reactions from it