How to get specific member username with discord.js

穿精又带淫゛_ 提交于 2020-06-17 02:36:19

问题


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.

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


回答1:


You can retrieve a user by their ID from the client's cache of 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(). Keep in mind, it returns a Promise.

Example:

// Async context needed for 'await'

try {
  const devID = '436577130583949315';
  const dev = client.users.get(devID) || await client.fetchUser(devID);
  // Retrieve the user from the client's cache.
  // If they haven't been cached yet, fetch them.

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


来源:https://stackoverflow.com/questions/56619167/how-to-get-specific-member-username-with-discord-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!