在awaitReaction node.js之后访问反应集合

时间:2019-06-25 00:15:05

标签: javascript node.js collections discord.js

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

我的理解是,此收藏集是一张地图。我尝试了各种访问地图的方法,所有方法都是未定义的或[对象对象]等...,而不是发送收集到的反应的人的用户名。根据文档,reaction.users是我认为可以访问集合的方式,但是它对我不起作用...

我知道它确认是谁发送了响应,因为我在message.channel.send(`$ {reaction.users} ...的正上方添加了“ message.reply('您接受了挑战。')”行。行,它会在我做出反应后立即回复该行,并且确实会正确显示用户名。我尝试使用collection.get(),就像我看到有人使用过一样,也不起作用...我没有让它起作用,我的机器人无法进一步发展。

我需要做出反应的用户的用户名,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 个答案:

答案 0 :(得分:1)

我认为您需要使用MessageReaction.users来代替reaction.user,它会提供对邮件做出反应的所有用户的集合。然后,您可以从集合中获取first user并获取他/她的名字。

一些示例代码(未经测试,但是应该向正确的方向发展):

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