javascript - 在awaitReaction之后访问 react 集合node.js

标签 javascript node.js collections discord.js

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

我的理解是这个集合是一张 map 。我尝试了各种访问 map 的方法,但所有方法要么是未定义的,要么是[对象对象]等......而不是发送收集到的 react 的人的用户名。根据文档,reaction.users 是我认为访问集合的方式,但它对我不起作用......

我知道它确认了谁发送了回复,因为我在 message.channel.send(`${reaction.users}... “行,在我使用react后它确实会立即回复该行,并且它确实正确显示了用户名。我尝试过使用 collection.get() 就像我看到有些人使用的那样,但这也不起作用......我如果不让它发挥作用,我的机器人就无法取得进一步的进展。

我需要使用react的用户的用户名、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`);
    });
    }

正如我上面所说,我想获取发送 react 的人的用户名,而不是[对象映射]。我还需要获取用户的 id。我尝试的一切都没有成功。

预先感谢您的帮助!

最佳答案

我认为您需要使用 MessageReaction.users 而不是 reaction.user这将提供对消息使用react的所有用户的集合。然后你可以得到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!`)
    }
});

关于javascript - 在awaitReaction之后访问 react 集合node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56745322/

相关文章:

arrays - 掉落(其中 : {}) remove(where: {}) function in Swift?

javascript - 使用javascript更改字体大小

javascript - 找不到 null 的 innerHTML,css3 过渡翻转

node.js - Apollo 服务器在等待流数据时超时

javascript - 无法有意发出 http 请求

java - 我们可以有一个字段在 Java 中实现两个或多个接口(interface)吗?

c# - 在字符串集合中搜索的最快方法

javascript - 这是克隆任何对象/函数/等的有效/安全方法吗?在 ES6 中?

javascript - Angularjs Controller 和模板封装与 Angular 组件

javascript - Node 强大的单选按钮