提问者:小点点

访问awaitReaction Node.js之后的反应集合


下面的代码运行良好,直到我尝试在选择X或check之后发送消息。我得到的是https://I.gyazo.com/2b8c4cfcd047121df364218ef0e8d7e9.png

我的理解是,这本藏品就是一张地图。我尝试过各种访问地图的方法,所有的方法都不是未定义的,就是[对象对象]等等。而不是发送收集的反应的人的用户名。根据文档,Reaction.Users是我认为访问集合的方式,但它对我不起作用。。。

我知道它确认了谁发送了反应,因为我添加了一行“message.reply('你接受了挑战。')”就在message.channel.send(`${reaction.users}。。。“行的上方,它确实在我做出反应后立即用这行进行回复,并且它确实正确地显示了用户名。我尝试过使用collection.get(),就像我看到一些人使用的那样,但也不起作用。如果不让它起作用,我就无法为我的bot继续前进。

我需要用户名,id等。对于做出反应的用户来说,因为他们的信息被用来提交到一个mysql表中,这个表中保存着这个挑战所针对的游戏的统计数据。

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`);
    });
    }

正如我上面所说的,我想要得到的不是[对象映射],而是发送反应的人的用户名。我还需要得到用户的id以及。我试过都没用。

提前感谢您的任何帮助!


共1个答案

匿名用户

我认为不需要使用reaction.user,而是使用Messagereaction.users,它将提供对消息作出反应的所有用户的集合。然后您可以从集合中获取第一个用户,并得到他/她的名字。

一些示例代码(这没有经过测试,但应该可以帮助您找到正确的方向):

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

    let firstUser = reaction.users.filter(user => !user.bot).first();

    if (reaction.emoji.name === '✅') {
        message.channel.send(`${firstUser.username} 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(`${firstUser.username} has **declined* the challenge! Please await 5 minutes to pass or for all challenged players to respond!`)
    }
});