javascript - 如何使用 discord.js 获取特定的会员用户名
问题描述
我想在嵌入消息中添加一个特定的成员信息(用户名 + 头像)。有人知道该怎么做吗?
const feedback = new discord.RichEmbed()
.setColor([0, 0, 255])
.setFooter("Bot created by : " + message.author.username, message.author.avatarURL)
.setDescription("The text I want to be sent")
在上面的代码中,我想通过特定的不和谐成员标识 id 更改“message.author.username”和“message.author.avatarUrl”,例如:436577130583949315。
但是我不知道从那个不和谐的识别号中能够显示用户名和头像的方式是什么。
在此先感谢您的帮助 :)
解决方案
由于管理器的实现,必须修改以下代码以使用最新版本的 Discord.js(本次编辑时为 v12)。
您可以通过用户的 ID 从客户端的用户缓存中检索用户,Client#users
. 但是,不能保证每个用户都始终被缓存,因此您可以使用Client#fetchUser()
. 请记住,它返回一个Promise。如果用户在缓存中,该方法将返回它。
例子:
// Async context needed for 'await'
try {
const devID = '436577130583949315';
const dev = await client.fetchUser(devID);
const feedback = new discord.RichEmbed()
.setColor([0, 0, 255])
.setFooter(`Bot created by ${dev.tag}.`, dev.displayAvatarURL)
.setDescription('Your text here.');
await message.channel.send(feedback);
} catch(err) {
console.error(err);
}
推荐阅读
- django - Django:在 UpdateView 中添加自定义 CSS
- r - 如何使用传单使数据集与 shapefile 对齐?
- pyspark - 使用 pyspark 将特定单词删除到数据框中
- mysql - 为什么我收到“连接太多”错误
- java - Log4j1.x 迁移到 log4j-1.2-api。需要使用 AbstractAppender 而不是 AppenderSkeleton
- javascript - Discord.js:TypeError:无法读取未定义的属性“删除”
- angular - 当我在 vscode 中保存任何打字稿文件时。所有编辑的单引号变成双引号
- jquery - 如何从外部链接打开(单选按钮)选项卡?
- c++ - 如何在 C++ 中打印字典?这是我正在运行的代码,我无法使用 printDicionary 方法
- swift - 在设备之间同步键值