Ajit Kumar
Ajit Kumar

Reputation: 417

Discord.js - Setting the Avatar

I am using discord.js for building a bot in nodejs. It is hosted in Replit. Below, is the code I am using to send a message, containing the sender's avatar. But, unfortunately, it doesn't work as expected ! (couldn't see the image ).

enter image description here

client.on("message", msg => {
 if (msg.channel.name === 'bot-is-here') {
    if (msg.content === "avatar") {

      const embed = new Discord.MessageEmbed()
        .setTitle('Avatar!')
        .setAuthor("Your Avatar", msg.author.avatar)
        .setImage(msg.author.avatar)
        .setColor('RANDOM')
        .setDescription('Avatar URL')

      msg.reply(embed);
    }
  }
})

Any help is greatly appreciated !

Upvotes: 0

Views: 515

Answers (1)

Radnerus
Radnerus

Reputation: 592

Avatar is a property of an User object which returns the ID of the user's avatar as a String. In order do get the avatar of the user, you need to do msg.author.avatarURL() or msg.author.displayAvatarURL(). The difference between these two is that, the avatarURL() method returns the avatar if the user, only if it's present. The displayAvatarURL() method returns the user's avatar if they have one. Otherwise a link to their default avatar will be returned.

Upvotes: 2

Related Questions