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')
  const avatarButton = new ActionRowBuilder()
  const avatarEmbed = new EmbedBuilder()

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

Makes sense, thanks!


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