提问者:小点点

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


我在做一个机器人,玩家可以在那里互相挑战。在发布challenge命令之后,我让我的机器人获得了这些用户的不和ID,该命令包括列出他们想要向其发出挑战的玩家。

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

在下面的代码中,uniquePlayersIDArray是discord用户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`);
    });
    }

我知道在过滤器语句中,不需要在“。includes(reaction.emoji.name)”后面添加任何内容就可以工作,但是它会收集所有的反应。

我只想让那些没有受到挑战的人的所有反应都被忽视。

谢谢你提前帮忙。


共1个答案

匿名用户

您可以添加一个MessageCollector来检查executor的作者是否是正在进行反应的人,并使用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 ❌');
                    }
                })