【问题标题】:Get list of objects of users who reacted to a message with certain emoji in Discord获取在 Discord 中对带有特定表情符号的消息做出反应的用户对象列表
【发布时间】:2021-10-05 16:29:00
【问题描述】:

下面是用 Discord.js 编写的 Discord bot 的代码 sn-p:

client.channels.fetch('channelID here').then(function (channel) {
 channel.messages.fetch('messageID here').then(function (message) {
  console.log(message.reactions.cache.get('EmojiID here').users);
 });
});

在控制台中得到如下输出:

ReactionUserManager {
  cacheType: [class Collection extends Collection],
  cache: Collection [Map] {},
  reaction: MessageReaction {
    message: Message {
      channel: [TextChannel],
      deleted: false,
      id: 'MessageID here',
      type: 'DEFAULT',
      system: false,
      content: 'What role do you want?\n' +
        'React with:\n' +
        '<:Red:870224025811558450>  for <@&870162738561814578> \n' +
        '<:Blue:870224213976444959> for <@&870162842983206922> \n' +
        '<:Yellow:870224106061172776> for <@&870162885412810773>\n' +
        'You will be assigned the role corresponding to your most recent reaction.\n' +
        'Unreact to remove the role.',
      author: [User],
      pinned: false,
      tts: false,
      nonce: null,
      embeds: [],
      attachments: Collection [Map] {},
      createdTimestamp: 1627548937713,
      editedTimestamp: 1627617831107,
      reactions: [ReactionManager],
      mentions: [MessageMentions],
      webhookID: null,
      application: null,
      activity: null,
      _edits: [],
      flags: [MessageFlags],
      reference: null
    },
    me: true,
    users: [Circular],
    _emoji: ReactionEmoji {
      animated: undefined,
      name: 'Red',
      id: 'EmojiID here',
      deleted: false,
      reaction: [Circular]
    },
    count: 2
  }
}

我可以在输出中看到count: 2。我想获取这两个用户的对象列表。如何做到这一点?

【问题讨论】:

    标签: javascript discord.js bots javascript-objects


    【解决方案1】:

    你离得很近。 ReactionUserManager 是经理;这意味着它有一个 cache 属性,该属性返回 collectionUser 对象。

    您也可以获取用户:

    client.channels.fetch('channelID').then(function (channel) {
     channel.messages.fetch('messageID').then(function (message) {
      const reaction = message.reactions.cache.get('EmojiID')
      reaction.users.fetch().then(function (users) {
        console.log(users)
      })
     })
    })
    

    如果你可以使用 async-await(即你在一个异步函数中),你可以让它更具可读性:

    const channel = await client.channels.fetch('channelID')
    const message = await channel.messages.fetch('messageID')
    const reaction = message.reactions.cache.get('EmojiID')
    const users = await reaction.users.fetch()
    
    console.log(users)
    
    // e.g. add a role to each user
    users.each(async (user) => {
      // get the member object as users don't have roles
      const member = await message.guild.members.fetch(user.id)
      member.roles.add('ROLE ID')
    })
    

    【讨论】:

    • 我在发布之前就尝试了 .cache 属性。我又试了一次,这是输出:Collection [Map] {}
    • 是的,我告诉过你你很亲密。收藏不是你想要的吗?集合是对象的列表。我还添加了一个链接,以便您可以检查集合具有哪些方法。你想用那个列表做什么?
    • 我想为这些用户分配一个角色。而且,我不知道如何使用集合。我是 js 新手。
    • 对了,这张地图Collection [Map] {}不是空的吗?它应该包含两个用户。
    • 感谢您的帮助,您的回答完全符合我的预期。
    猜你喜欢
    • 2018-08-18
    • 2020-07-22
    • 2019-12-27
    • 2021-12-17
    • 1970-01-01
    • 2021-03-25
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多