Search code examples
javascriptnode.jsdiscorddiscord.js

How can I access collected reaction from awaitReactions?


This is my code and I want to access the collected emojis and send them back as a reply. I am also inserting the object I am getting from collected.

Here is the link for the object data: https://pastebin.com/MDm9dsvD

const emojis = [
  'booknquill',
  'balloon1',
  'rail',
  'Alex',
  'cookie1',
  'fish1',
  'Agent',
  'cake1',
  'pickaxe',
  'water',
  'Steve',
  'Apple',
  'carro1t',
  'panda',
  'sign',
  'potion',
  'map1',
  'llama1',
];

let msg = await message.channel.send('`What is your Code ?....`');

for (let i = 0; i < emojis.length; i++) {
  let reactionEmoji = message.guild.emojis.cache.find(
    (emoji) => emoji.name === emojis[i],
  );
  msg.react(reactionEmoji);
}

let reactedEmojis = [];

for (let i = 0; i < emojis.length; i++) {
  reactedEmojis.push(
    message.guild.emojis.cache.find((emoji) => emoji.name === emojis[i]),
  );
}

const filter = (reaction, user) => {
  return !user.bot;
};

msg
  .awaitReactions({ filter, max: 4, time: 60000, errors: ['time'] })
  .then((collected) => {
    console.log(collected.size);
    let userReaction = collected[0];
    console.log(collected[0]);
    message.channel.send(userReaction._emoji.name);
  });

Solution

  • collected is a collection, not an array, so you can't simply access the first item using [0]. You can use methods like .first(), .last(), .at(), etc to access items.

    To get the first collected item, you can use let userReaction = collected.first() or let userReaction = collected.at(0).