Search code examples
node.jsdiscorddiscord.js

user info command works only witout a mention


i have a problem with my code for the command userinfo it works witout a mantion but not wen a mention somoen

wenn i run the code without a mention it works but wenn i try to test it with a user mentiond the code dosent give me an error it just ignors it

this is the code

  if (message.content === prefix + 'userinfo') {
    if (message.mentions.users.size) {
      const embed = new MessageEmbed()
        .setTitle(`${message.mentions.users.first().tag}`)
        .setColor("03fc94")
        .setThumbnail(message.mentions.users.first().avatarURL())
        .addFields(
          { name: 'ID', value: `${message.mentions.users.first().id}`, inline: true }, 
          { name: 'Username', value: `${message.mentions.users.first().username}`, inline: true },
          { name: 'Discriminator', value: `${message.mentions.users.first().discriminator}`, inline: true },
          { name: 'Bot', value: `${message.mentions.users.first().bot}`, inline: true },
          { name: 'Created At', value: `${message.mentions.users.first().createdAt}`, inline: true },
          { name: 'Roles', value: `${message.mentions.users.first().roles.cache.map(r => r.name).join(', ')}`, inline: true },
        )
        .setTimestamp()
        .setFooter('');
        message.channel.send({ embeds: [embed]});
    } else {
      const embed = new MessageEmbed()
        .setTitle(`${message.author.tag}`)
        .setColor("03fc94")
        .setThumbnail(message.author.avatarURL())
        .addFields(
          { name: 'ID', value: `${message.author.id}`, inline: true },
          { name: 'Discriminator', value: `${message.author.discriminator}`, inline: true },
          { name: 'roles', value: `${message.member.roles.cache.map(r => r.name).join(', ')}`, inline: true },
          { name: 'Joined', value: `${message.member.joinedAt}`, inline: true },
          { name: 'Created', value: `${message.author.createdAt}`, inline: true },
          { name: 'Is Bot', value: `${message.author.bot}`, inline: true },
          { name: 'Is Owner', value: `${message.author.id === owner}`, inline: true },
        )
        .setTimestamp()
        .setFooter('');
      message.channel.send({ embeds: [embed]});
    }
  }
}); ` ` `

Solution

  • Instead of message.mentions.users.size you can use message.mentions.members.first(). To get this command works, you can also use if() {} else if() {} statement. For example:

    if(!args[0]) {
       //This is the part for non-mentioned members. 
    } else if(args[0]) {
       //This is the part for mentioned members.
    }
    

    You can also use if() {} else {} statement.

    if(!args[0]) {
       //This is the part for non-mentioned members.
    } else {
       //This is the part for mentioned members.
    }
    

    In addition:

     if(!args[0]) {
       const author = message.author.id //or you can do message.author also
       //Then your codes here
    } else {
       const member = message.mentions.members.first() //You can use ID's also
       //Then your codes here. 
    }
    

    To use it on embed. This is not the right calling about the member

    { name: 'ID', value: `${message.mentions.users.first().id}`, inline: true }, 
    

    So change it to:

    { name: 'ID', value: `${member.user.id}`, inline: true },