0
votes

I'm making a user info command in my discord bot using discord.js v12. The only problem I have with it is, that if an user doesn't have any roles, the highest role shows as @@everyone, not @everyone. So I would like to avoid it, and make it so if the user doesn't have any roles, it will say 'This user has no roles in this server'. My code is here (only the bit that has anything to do with the roles):

const { DiscordAPIError } = require("discord.js");
const Discord = require("discord.js");
const moment = require("moment");

module.exports = {
    name: "profile",
    description: "The bot will return the info about the user",
    execute(message, args){
        let userinfoget = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member

var highestRoleID = userinfoget.roles.highest.id;
        var joined = moment(userinfoget.joinedAt).format('DD/MM/YY, HH:mm:ss')
        console.log(`Highest role = ${highestRoleID}`);

        console.log(`User = ${userinfoget}`);
        console.log(userinfoget.roles)
        if(!userinfoget.roles) console.log('no roles')

const embed = new Discord.MessageEmbed()

        .setColor(userinfoget.displayHexColor)
        .setAuthor(`${userinfoget.user.tag}`, userinfoget.user.displayAvatarURL())
        .addFields(
            {name: `User ping`,
            value: `<@${userinfoget.id}>`}
        )
        .addFields(
            {name: `User ID`,
            value: `${userinfoget.id}`}
        )
        .addFields(
            {name: 'Joined Server',
            value: moment(userinfoget.joinedAt).format('LLLL') + ' ' + timeFromJoiningServerMessage} // or moment(userinfoget.joinedAt).format('DD/MM/YY, HH:mm:ss')
        )
        .addFields(
            {name: 'Joined Discord',
            value: moment(userinfoget.user.createdAt).format('LLLL')} // or moment(userinfoget.createdAt).format('DD/MM/YY, HH:mm:ss')
        )
        .addFields(
            {name: 'Highest role',
            value: `<@&${highestRoleID}>`}
        )
        .addFields(
            {name: 'Online Status',
            value: `${status}`}
        )
        .addFields(
            {name: 'Is a bot?',
            value: `${isBot}`}
        )
        .setFooter('Bot made by mkpanda')
        message.channel.send(embed);
}

As you can see, I tried doing !userinfoget.roles, but when logging it, it shows all the information about the user, not just the roles. How could I detect whether an user has any role or not? Thanks in advance :)

2
The whole point of using addFields() is that you can add multiple fields at once. I suggest you condense your embed structure.Lioness100

2 Answers

1
votes

GuildMember.roles.cache is a Collection (very similar to an Array). Therefore, you can use Collection#filter and check if the Role's name equals @everyone. That'll remove the @everyone role from the Collection and you can check if the GuildMember has any roles.


.addFields({
    name: 'Highest role',
    value: message.member.roles.cache.filter(role => role.name !== "@everyone").size > 0 ? message.member.roles.highest : "This member has no roles."
})

Useful links I strongly recommend you to visit:

Collection#map

Collection#filter

Conditional (ternary) operator

-2
votes

You could use this ;

userinfoget.roles.cache.size === 1 ? 'No roles' : userinfoget.roles.cache.map(r => '<@&' + r.id + '>').join(', ')

Since if no user has no roles then they will only be "@everyone" so the role size would be one. You can use this information to see if the user has or not roles