过滤特定用户的反应discord.js

时间:2019-06-23 14:26:14

标签: node.js async-await discord.js collectors

我正在制作一个机器人,玩家可以互相挑战。发布挑战命令(涉及列出要向其发出挑战的玩家)后,我让我的机器人为每个用户获取了不和谐ID。

然后,我在挑战频道中发布了bot帖子,列出了玩家并等待仅那些受到挑战的玩家的反应。我做了一个反应收集器来收集那些反应。我想过滤掉那些反应,只包括那些受到挑战的反应,以避免随机玩家劫持挑战。

在下面的代码中,uniquePlayersIDArray是不和谐用户ID的数组。我只希望该机器人仅收集来自该阵列中用户的反应。

我尝试使用uniquePlayersIDArray.indexOf(user.id)来检测用户是否在数组中。如下所示,我尝试了uniquePlayersIDArray.includes(user.id)。

async function acceptQuestion() {

  const agree = "✅"
  const disagree = "❌"
  let msg = await message.channel.send(`A challenge has been issued by ${author}?\n\n**IF** you are listed above, please select one of the following to accept or deny the challenge...`)
  await msg.react(agree)
  await msg.react(disagree)

  const filter = (reaction, user) => {
  return ['✅', '❌'].includes(reaction.emoji.name) && uniquePlayersIDArray.includes(user.id);

  };
 const collector = await msg.createReactionCollector(filter, { time: 15000 });

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

  console.log(`Collected ${reaction.emoji.name}`)

  if (reaction.emoji.name === '✅') {
  message.channel.send(`${reaction.user} has **accepted** the challenge! Please await 5 minutes to pass or for all challenged players to respond!`)
  } else if (reaction.emoji.name === '❌') {
  message.channel.send(`${reaction.user} has **declined* the challenge! Please await 5 minutes to pass or for all challenged players to respond!`)
    }
    });

    collector.on('end', collected => {
    console.log(`Collected ${collected.size} items`);
    });
    }

我知道此方法无需在filter语句中的“ .includes(reaction.emoji.name)”之后添加任何内容,但随后它将收集所有反应。

我只希望不受挑战的人们的所有反应都被忽略。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以添加一个MessageCollector来检查执行者的作者是否是该作者的响应者,并使用message.awaitReactions而不是createReactionCollector

message.awaitReactions(filter, { max: 1, time: 500000, errors: ['time'] })
                .then(collected => {
                    const reaction = collected.first();

                    if (reaction.emoji.name === '✅') {
                        msg.channel.send(logSetup);
                            const collector = new Discord.MessageCollector(msg.channel, m => m.author.id === msg.author.id, { time: 500000 });
                            console.log(collector)
                            collector.on('collect', message => {

                                message.channel.send("You clicked ✅");

                            })
                    } else {
                        msg.reply('You clicked ❌');
                    }
                })