Creating a /avatar Command

I’m trying to create a /avatar command to get a user’s avatar. However, it’s saying that displayAvatarURL() is invalid for a user variable. I know this works because using interaction.user instead of the user variable works, but only gets the command issuer’s avatar, not the targeted person. The code is in the commented section (using /*) on this replit.

You’re trying to call displayAvatarURL() on a string, not on a user object.

You need to fetch the user with (with the ID) and then call displayAvatarURL() on the resulting user object.

Change the comment part of your code:

} else if (interaction.commandName == 'avatar') {
  const userId = interaction.options.get('user').value;
  const user = await client.users.fetch(userId);
  
  const avatarLink = new ButtonBuilder()
    .setLabel('Download Avatar')
    .setURL(user.displayAvatarURL())
    .setStyle(ButtonStyle.Link);
    
  const avatarButton = new ActionRowBuilder()
    .addComponents(avatarLink);
    
  const avatarEmbed = new EmbedBuilder()
    .setColor(0x457EDE)
    .setImage(user.displayAvatarURL());

  interaction.reply({embeds: [avatarEmbed], components: [avatarButton]});
}
3 Likes

Makes sense, thanks!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.