I am trying to make a discord.js avatar command, and the mentioning portion doesn't work correctly

64,777

Solution 1

You have a check if (!message.mentions.users.size) { which makes the command run only if you do not mention somebody. You either need to use an else { in your code or do:

 if (message.content.startsWith(config.prefix + 'avatar')) {
    const user = message.mentions.users.first() || message.author;
    const avatarEmbed = new Discord.RichEmbed()
        .setColor(0x333333)
        .setAuthor(user.username)
        .setImage(user.avatarURL);
    message.channel.send(avatarEmbed);
}

The const user = message.mentions.users.first() || message.author; tries to get the user that was mentioned but if it does not find anyone it will use the author's used.

This can also be used like this:

if (!message.mentions.users.size) {
    message.channel.send('Nobody was mentioned');
    return;
}
// continue command here, after guard clause

Solution 2

There's nothing like avatarUrl unless you have defined it.
Use this code to get the url of a user:

message.channel.send("https://cdn.discordapp.com/avatars/"+message.author.id+"/"+message.author.avatar+".jpeg");

Just replacemessage.author with the user who is mentioned

Solution 3

These is the updated version of the answer that works

if (message.content.startsWith(config.prefix + 'avatar')) {
const user = msg.mentions.users.first() || msg.author;
const avatarEmbed = new MessageEmbed()
  .setColor(0x333333)
  .setAuthor(`${user.username}'s Avatar`)
  .setImage(
    `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png?size=256`
  );
msg.lineReply(avatarEmbed);
}

This uses discord's avatar url, and msg.lineReply(avatarEmbed); is a function that sends the embed as a reply to the message

Solution 4

My

if (msg.content.startsWith(prefix + 'avatar')) {
    const user = msg.mentions.users.first() || msg.author;
    const avatarEmbed = new MessageEmbed()
      .setColor('')
      .setAuthor(`${user.username}'s Avatar`)
      .setImage(
        `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png?size=256`
      );
    msg.reply(avatarEmbed);
    }

})

Share:
64,777

Related videos on Youtube

cyliim
Author by

cyliim

Updated on July 05, 2022

Comments

  • cyliim
    cyliim almost 2 years

    I have an avatar command in my discord bot. When the user uses h.avatar, it outputs their avatar, which works fine. Whenever they try to use h.avatar @user, nothing happens.
    Here is my code:

     } if (message.content.startsWith(config.prefix + "avatar")) {
          if (!message.mentions.users.size) {
            const avatarAuthor = new Discord.RichEmbed()
          .setColor(0x333333)
          .setAuthor(message.author.username)
          .setImage(message.author.avatarURL)
            message.channel.send(avatarAuthor);
            let mention = message.mentions.members.first();
            const avatarMention = new Discord.RichEmbed()
            .setColor(0x333333)
            .setAuthor(mention.user.username)
            .setImage(mention.user.avatarURL)
            message.channel.send(avatarMention);
    
    • PLASMA chicken
      PLASMA chicken over 4 years
      This Questions still seems to be unresolved, could you please mark a Answer as accepted, or self answer if you have found the Solution? @cyliim
  • Dharman
    Dharman over 4 years
    Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
  • spex
    spex over 2 years
    There is. It used to be a property called avatarUrl but now it's a method called displayAvatarURL() discord.js.org/#/docs/main/stable/class/… This question is from 2019 when avatarUrl was correct. Things have changed since then. You do not need to construct the URL yourself.
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • Soham
    Soham over 2 years
    If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review