【问题标题】:How to get the discord id of users who have reacted to a specific message discord.js如何获取对特定消息做出反应的用户的不和谐 ID discord.js
【发布时间】:2021-09-10 20:25:00
【问题描述】:

我想获取对特定消息做出反应的用户的id 并将他们推送到变量players,我有console.log() 多件事搜索解决方案,使用谷歌,并询问朋友,这并不成功。我希望我能找到有足够经验的人来帮助我找到答案或其他问题来解决我遇到的这个小问题。

let players = [message.author.id]
message.react('????')
const filter = (reaction, user) => reaction.emoji.name === '????'
message.awaitReactions(filter, { time: 20000 })
       .then(collected => {
           console.log(collected.user.id)
           players.push(collected.user.id)
        })
       .catch(console.error);

【问题讨论】:

  • 当你登录collected.user.id时,它会打印id吗?
  • 不,它没有,它打印未定义。
  • 我不知道下一步该做什么

标签: node.js discord discord.js


【解决方案1】:

awaitReactions 返回 CollectionMessageReactions。因此collected.user 将是未定义的。

您必须从Collection 中获取第一个MessageReaction 并访问其users 属性,过滤掉机器人的ID。


// Creating a filter.
const Filter = (reaction, user) => reaction.emoji.name === "?";

// Reacting to the message.
await message.react("?");

// Awaiting reactions.
message.awaitReactions(Filter, { time: 6000 }).then((collected) => {
    const reaction = collected.first();

    // Making sure that at least one user reacted to the message
    if (reaction) {
        const players = reaction.users.cache.filter((user) => user.id !== client.user.id);

        console.log(`Users that reacted: ${players.map((user) => user.username).join(", ")}`);
    } else {
        console.log("No one reacted to this message.");
    }
});

如果您收到诸如 The 'await' operator can only be used within an async method. 之类的错误,则必须使您的方法异步。

例子:

client.on("message", async (message) => {
    // Code
});

【讨论】:

    【解决方案2】:

    查看此文档:https://discordjs.guide/popular-topics/reactions.html#reacting-in-order

    const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(userId));
    try {
        for (const reaction of userReactions.values()) {
            await reaction.users.remove(userId);
        }
    } catch (error) {
        console.error('Failed to remove reactions.');
    }
    

    您可以使用此示例,但您可以执行以下操作,而不是删除 userId:

    const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(userId));
    try {
        players.push(userId)
    } catch (error) {
        console.error('Failed to push reaction');
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      
      const filter = (reaction, user) => reaction.emoji.name === '?'
          message.awaitReactions(filter, { time: 20000 })
          .then(collected => {
             collected.first().users.cache.forEach(u => players.push(u.id))
          })
          .catch(console.error);
      

      我无法测试它。请告诉我您可能遇到的任何错误。

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
      • 效果很好,谢谢! (我测试了两个答案,所以如果其他人路过寻找答案,对他们来说会更容易一些)
      • 如果答案最好,您可以单击复选标记
      猜你喜欢
      • 2020-12-10
      • 2021-06-24
      • 2021-01-31
      • 2021-08-08
      • 1970-01-01
      • 2020-09-07
      • 2021-05-08
      • 2019-12-27
      • 1970-01-01
      相关资源
      最近更新 更多