从用户ID获取用户头像 [英] Get user avatar from user ID
本文介绍了从用户ID获取用户头像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在开发一个使用discord.js的生日公告机器人。我想要显示用户的头像,使用他们不一致的用户ID,然后在嵌入中使用。
使用client.fetchUser('[userID]').avatarURL
当前不起作用。
const exampleEmbed = new Discord.RichEmbed()
.setColor('#0099ff')
.setAuthor('🎂 Birthday Announcement:')
.setThumbnail(cleint.fetchUser('[userID]').avatarURL)
.setDescription('🎉 Happy day of birth [user ID]! 🎁')
.setFooter('May 25');
client.on('message', message => {
if (message.content.toLowerCase().startsWith('f.test')) {
message.channel.send(exampleEmbed);
}
});
它将在没有图像的情况下正常发送嵌入内容,但它将每隔几秒钟发送一次throw er
。
推荐答案
Client.fetchUser()
返回Promise。本质上,您必须等待它返回值。您可以使用关键字await
或通过将then()
方法附加到Promise来完成此操作。但是,在使用catch()
方法或try...catch
语句拒绝承诺时,您还应该捕获任何错误。
我建议您阅读thisMDN文档,了解更多关于使用JavaScript进行异步编程的信息。
示例1:
// This needs to be inside of an async function to use 'await'
const { displayAvatarURL } = await client.fetchUser('id')
.catch(console.error);
embed.setThumbnail(displayAvatarURL);
示例2:
client.fetchUser('id')
.then(user => {
const embed = new Discord.RichEmbed()
.setThumbnail(user.displayAvatarURL);
})
.catch(console.error);
在这些示例中,我使用displayAvatarURL
,因为如果用户没有设置自己的化身URL,它将返回默认化身URL。
这篇关于从用户ID获取用户头像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文