如何仅从discord.js v12中的message.guild.members.fetch()获取用户ID?

时间:2020-05-21 12:38:44

标签: javascript node.js discord discord.js

我正试图获得一个随机的获奖者,我想采用整个对象变量,只是对整个对象进行隔离,所以我只获得了UserID的列表

这里有一个我做console.log(message.guild.members.fetch())时得到的例子:

Promise {
  Collection [Map] {
    '123456789012345678' => GuildMember {        
      guild: [Guild],
      user: [User],
      joinedTimestamp: 1234567890123,
      lastMessageID: '012345678901234567',       
      lastMessageChannelID: '012345678901234567',
      deleted: false,
      _roles: [Array]
    },
    '234567890123456789' => GuildMember {
      guild: [Guild],
      user: [User],
      joinedTimestamp: 1234567890123,
      lastMessageID: null,
      lastMessageChannelID: null,
      premiumSinceTimestamp: null,
      deleted: false,
      _roles: [Array]
    },
    '345678901234567890' => GuildMember {
      guild: [Guild],
      user: [ClientUser],
      joinedTimestamp: 1234567890123,
      lastMessageChannelID: null,
      premiumSinceTimestamp: null,
      deleted: false,
      _roles: [Array]
    }
  }
}

我只想获得123456789012345678行的'123456789012345678' => GuildMember {部分

1 个答案:

答案 0 :(得分:2)

使用keyArray方法:

message.guild.messages.fetch().then(members => {
  const userIDs = members.keyArray()
  // Do something with the IDs here
})

使用异步/等待:

const userIDs = (await message.guild.messages.fetch()).keyArray()
// Do something with the IDs here