首页 > 解决方案 > 如何从集合映射中获取特定值

问题描述

所以我在这里尝试使用 DiscordJS 制作一个机器人。我目前正在尝试显示用户详细信息。当我获得详细信息时,我不确定如何获取特定详细信息。

所以我通过这样做来获得用户

const user = await utils.findUser(message, args[0]);

现在在这里,我根据用户在 args 中输入的任何内容来获取用户。用户变量看起来像这样:

Collection [Map] {
'211975876747853825' => GuildMember {
guild: Guild {
  members: [GuildMemberManager],   
  channels: [GuildChannelManager], 
  roles: [RoleManager],
  presences: [PresenceManager],    
  voiceStates: [VoiceStateManager],
  deleted: false,
  available: true,
  shardID: 0,
  name: 'Elchea Kingdom',
  icon: 'cd8156aed6c8ebc482df2252936afcee',
  splash: null,
  region: 'eu-west',
  large: false,
  features: [],
  applicationID: null,
  afkTimeout: 300,
  embedEnabled: undefined,
  premiumTier: 0,
  premiumSubscriptionCount: null,
  verificationLevel: 'MEDIUM',
  explicitContentFilter: 'DISABLED',
  mfaLevel: 0,
  defaultMessageNotifications: 'MENTIONS',
  systemChannelFlags: [SystemChannelFlags],
  vanityURLCode: null,
  description: null,
  banner: null,
  rulesChannelID: null,
  publicUpdatesChannelID: null,
  ownerID: '211975876747853825',
  emojis: [GuildEmojiManager]
},
user: User {
  bot: false,
  discriminator: '0006',
  avatar: '5ebd62928be9bd413a2b5f6e638587ea',
  lastMessageID: null,
  lastMessageChannelID: null,
  flags: [UserFlags]
},
premiumSinceTimestamp: null,
deleted: false,
_roles: [
  '600421802702929920',
  '600737807664939008',
  '603343858549391360',
  '600425094598361091',
  '600422841279578118',
  '603314411243044875',
  '617699524474830859',
  '603316305495326740',
  '617505016328224790',
  '617449137767710743'
]
}
}

现在假设我想获得公会名称“Elchea Kingdom”。我怎么能那样做?我曾尝试使用 Map.prototype.values() 并对其进行迭代,但它不起作用。我得到一个未定义的值。我试过做一些愚蠢的事情,比如 user[0] 和 user.Guild 来获取公会信息。但我总是不断得到未定义的价值。我做错了什么,请您指导我。

标签: javascriptdiscord.js

解决方案


https://discord.js.org/#/docs/main/stable/class/Collection

您应该调用变量用户,然后通过.first()

const users = await utils.findUser(message, args[0]);
const member = users.first();
const user = member.user;

您将哪个库用于变量utilsbtw?


推荐阅读