简体   繁体   English

如何使用discord.js获取特定成员用户名

[英]How to get specific member username with discord.js

I would like to add one specific member information (username + avatar) into an embed message.我想在嵌入的消息中添加一个特定的成员信息(用户名 + 头像)。 Does someone know how to do that?有人知道怎么做吗?

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")

On the code above, I would like to change "message.author.username" and "message.author.avatarUrl" by a specific discord member identification id such as : 436577130583949315 for example.在上面的代码中,我想通过特定的不和谐成员标识 ID 更改“message.author.username”和“message.author.avatarUrl”,例如:436577130583949315。

However I don't know what is the way from that discord identification number to be able to show the username and the avatar.但是我不知道从那个不和谐标识号到能够显示用户名和头像的方式是什么。

Thanks in advance for your help :)在此先感谢您的帮助 :)

The following code must be modified to use the latest version of Discord.js (v12 at the time of this edit) due to the implementation of Managers.由于 Managers 的实现,必须修改以下代码以使用最新版本的 Discord.js(本次编辑时为 v12)。


You can retrieve a user by their ID from the client's cache of users, Client#users .您可以通过用户的 ID 从客户端的用户缓存Client#users检索Client#users However, every user isn't guaranteed to be cached at all times, so you can fetch a user from Discord using Client#fetchUser() .但是,不能保证每个用户都一直被缓存,因此您可以使用Client#fetchUser()从 Discord 获取用户。 Keep in mind, it returns a Promise .请记住,它返回一个Promise If the user is in the cache, the method will return it.如果用户在缓存中,该方法将返回。

Example:例子:

// 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM