-2

This is my whois command:

const Discord = require("discord.js")
const moment = require('moment');

const status = {
online: "Online",
idle: "Idle",
dnd: "Do Not Disturb",
offline: "Offline/Invisible"
};

module.exports = {
config: {
    name: "whois",
    description: "userinfo",
    usage: "m/whois <mention a member/member id>",
    aliases: ['ui', 'ifno']
},
run: async (bot, message, args) => {
    var permissions = [];
    var acknowledgements = 'None';
    let whoisPermErr = new Discord.MessageEmbed()
    .setTitle("**User Permission Error!**")
    .setDescription("**Sorry, you don't have permissions to use this! ❌**")


    const member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;
    

    if(member.hasPermission("KICK_MEMBERS")){
        permissions.push("Kick Members");
    }
    
    if(member.hasPermission("BAN_MEMBERS")){
        permissions.push("Ban Members");
    }
    
    if(member.hasPermission("ADMINISTRATOR")){
        permissions.push("Administrator");
    }

    if(member.hasPermission("MANAGE_MESSAGES")){
        permissions.push("Manage Messages");
    }
    
    if(member.hasPermission("MANAGE_CHANNELS")){
        permissions.push("Manage Channels");
    }
    
    if(member.hasPermission("MENTION_EVERYONE")){
        permissions.push("Mention Everyone");
    }

    if(member.hasPermission("MANAGE_NICKNAMES")){
        permissions.push("Manage Nicknames");
    }

    if(member.hasPermission("MANAGE_ROLES")){
        permissions.push("Manage Roles");
    }

    if(member.hasPermission("MANAGE_WEBHOOKS")){
        permissions.push("Manage Webhooks");
    }

    if(member.hasPermission("MANAGE_EMOJIS")){
        permissions.push("Manage Emojis");
    }
    if(permissions.length == 0){
        permissions.push("No Key Permissions Found");
    }
    if(member.user.id == message.guild.ownerID){
        acknowledgements = 'Server Owner';
    }
    const embed = new Discord.MessageEmbed()
        .setDescription(`<@${member.user.id}>`)
        .setAuthor(`${member.user.tag}`, member.user.displayAvatarURL())
        .setColor('#2F3136')
        .setFooter(`ID: ${message.author.id}`)
        .setThumbnail(member.user.displayAvatarURL())
        .setTimestamp()
        .addField('Joined at: ',`${moment(member.joinedAt).format("dddd, MMMM Do YYYY, HH:mm:ss")}`)
        .addField('Created On', member.user.createdAt.toLocaleString())
        .addField(`Roles [${member.roles.cache.filter(r => r.id !== message.guild.id).map(roles => `\`${roles.name}\``).length}]`,`${member.roles.cache.filter(r => r.id !== message.guild.id).map(roles => `<@&${roles.id }>`).join(" **|** ") || "No Roles"}`)
        .addField("\nAcknowledgements: ", `${acknowledgements}`)
        .addField("\nPermissions: ", `${permissions.join(` | `)}`);
        
    message.channel.send({embed});

}
}

It does work, but when the member mentioned has too many roles it'll come out with an error because the length is too long and the issue is it has too many roles.

How do I make it so if the mentioned user has too many roles, it comes up with a "Too many roles" message?

I'm using Discord.js v12.

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59

2 Answers2

0

A discord embed can only hold a certain amount of characters (as mentioned in this question's answer). You can try to create the String with all the roles before building the embed, and then test the number of characters. If the number is too high, you can then return a "Too many roles" message.

The author of the answer I mentioned before had this solution :

you may want to do some checks before sending out the embed and then handle oversized embeds by either splitting them into multiple embed messages, or using reaction based pages maybe

Vincent Gonnet
  • 146
  • 1
  • 13
0

Use a try/catch statement

//embed is instance of MessageEmbed with description as the member's roles
try {
await message.channel.send(embed)
} catch(err) {
embed.setDescription('Too many roles');
message.channel.send(embed)
}

Or use .catch

message.channel.send(embed).catch(() => {
embed.setDescription('Too many roles');     
message.channel.send(embed)
})
MrMythical
  • 8,908
  • 2
  • 17
  • 45