javascript - 如何从集合映射中获取特定值
问题描述
所以我在这里尝试使用 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 来获取公会信息。但我总是不断得到未定义的价值。我做错了什么,请您指导我。
解决方案
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;
您将哪个库用于变量utils
btw?
推荐阅读
- slider - 如何在滑块旋转中向滑块背景图像添加 ID 或类
- html - 在 Iphone 中看不到使用 React.js 输入的值
- node.js - 如何存储 GitHub PAT(个人访问令牌)以供应用使用
- sql - 使用MSSQL一次交易获取N个最常购买的组合产品
- reactjs - ReactJS:道具未在初始渲染时加载。无法在未定义的变量上使用映射
- path - 构建 LLVM 基本块的路径
- flutter - 我有一个数据表,我需要根据文本输入添加数据行
- amazon-ec2 - Terraform、Windows、带有批处理脚本的用户数据
- windows - 如何使用 PowerShell 在多台计算机上替换文件中的一行
- node.js - 获取“您无权查看此目录或页面。” 在 azure 上部署 react 和 node.js 之后