Discord.js: How to send a message and collect reactions from it

北战南征 提交于 2020-03-26 03:03:41

问题


I want the discord.js bot to send a message when a command is executed, react on it and then catch all reactions from users to do certain stuff, like respond to them. No time limit should be applied to reactions. I should also mention that I'm using a command handler and the message is being sent from another file than index.js (in which the collect even should occur).

I tried doing a collector but no matter how I do it, the collector variable is not defined or not working. I know it should be a collector for a fact though.

Making a filter for the checkmark emoji:

  return reaction.emoji.name === `✅` && user.id === call.message.author.id;
}

Trying to catch reactions outside the command block itself (because if I do it IN the block the event won't work:

collector.on(`collect`, (reaction, reactionCollector) => {
    console.log(`Caught ${reaction.emoji.name}`);
});

The errors I get are pretty much expected but I want to solve that:

collector.on(`collect`, (reaction, reactionCollector) => {
^

ReferenceError: collector is not defined

Maybe a global variable or setting the message id and then fetching? Help me.


回答1:


Why not include the Collector in your command.js?

 const time = 60000 //amount of time to collect for in milliseconds

 receivedMessage.channel.send("Hello World")
 .then(async function (message) {
      await message.react('✅')
      const filter = (reaction, user) => {
           return //YOUR FILTER HERE;
      };

      const collector = message.createReactionCollector(filter, { time: time });

      collector.on('collect', (reaction, reactionCollector) => {
           //do stuff
      });
 });


来源:https://stackoverflow.com/questions/57452056/discord-js-how-to-send-a-message-and-collect-reactions-from-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!