Discord.js - Getting multiple reactions to execute a command through awaitReactions method

Viewed 214

I'm currently in the process of making a little game of TicTacToe, and the idea I had in mind is to make it so instead of the channel being spammed with constant embeds asking for the person's next move, is to simply make it reaction-based, where you get to pick 1 out of 9 reactions (And of course, you wont be able to pick it again if the other player has already picked it).

I have never really worked with requiring multiple reactions, therefore I'd like to ask your help on how exactly to make it so that the message command execution isn't a one-time thing, but will go on until there's eventually a winner.

So far, with the code I have written, this does work 2 times, but then it randomly stops and no longer works.

In addition, when I'm trying to declare a spot as an x or a circle, the spot turns completely blank.

Please help!

The code I have so far: https://sourceb.in/S7cayfoYjp

Edit: I have now also found that the bot at first kind of skips the whole awaitReactions code. I used 'console.log(i)' for this, so that every time it loops it prints out 'i', and it seemed to be printing out the numbers 0-8 immediately, meaning it's not properly going through the code.

1 Answers

I think what you can use best there is a reactionCollector. It's a temporary reaction listener, attached to a message. A sample code for that would be:


const msg = await message.channel.send('tic tac toe test');

const acceptedEmojis = ['↖️', '⬆️', '↗️', '⬅️', '⏺️', '➡️', '↙️', '⬇️', '↘️']

 const filter = (reaction, user) => {
                return acceptedEmojis.includes(reaction.emoji.name) && user.id === turnId;
            }
//here you create the collector. It has following attributes: it stops after 10 minutes or after 2 minutes of not collecting anything. 
const collector = msg.createReactionCollector(filter, { time: 600000, idle: 120000});

//here you start the listener
collector.on('collect', (reaction, user) => {
  if (reaction.emoji.name === '↖️') {
    //remove the reaction
    await msg.reactions.resolve('↖️')
    acceptedEmojis.splice(acceptedEmojis.indexOf('↖️'), 1);
    //rest of your code...

  } else if (reaction.emoji.name === '⬆️') {
    ...
  }
});
Related