Search code examples
javascriptdiscorddiscord.jsembed

Listing users without roles with Embed getting error when too many


It is working when the users without role only has around 1-15 user lists but when it goes 100+ users it can't print and it give error which is it was saying that it reach the limit of the text in the embed.. Can you guys help me on how I can generate the list with pages functionality?

Edit: People saying read the error, i know the error exceeding the limit of the embed what im trying to ask for help is a way of creating dynamic page for embed, i've already tried discord.js-pagination but no hope it is manual creation and not dyamic.

By the way im using discord.js with a version of ^12.5.1

enter image description here

    require ('dotenv').config()
    const Discord = require('discord.js');
    const usedCommand = new Set();
    const config = process.env;

    module.exports.run = async (bot, message, args) => {
        if(usedCommand.has(message.author.id)){
            message.reply("You cannot use " + config.prefix + "unorole command beacuse of the cooldown.")
        } else {
            if (message.content.toLowerCase().startsWith(config.prefix + "unorole")) {
                //check if no perm
                if(!message.member.hasPermission("ADMINISTRATOR")){
                  return message.reply("You don't have `ADMINISTRATOR` permission to do that!");
                }  
                    
                  var memberscount = message.guild.members.cache.filter(member => member.roles.cache.array().length < 2).size;
            
                    if (memberscount == 0){
                    message.reply('Looks like everyone has a role already.') 
                    }
                    else{  
                    
                    //Embed List of users without role
                    const ListEmbed = new Discord.MessageEmbed() 
                   .setTitle(`Prune Bot | Users`)
                   .setDescription(`Users that has no role.`)
                   .setColor('#b491c8')
                   .addFields(
                  { name: 'Users:', value: message.guild.members.cache.filter(member => member.roles.cache.array().length < 2).map(member => member.user.tag).join('\n') },)
                 .setFooter('PruneBot is created by Mashwishi', 'https://i.imgur.com/DxWDaGv.png');
                 message.channel.send(ListEmbed);  
                  
                    } 
            }
            usedCommand.add(message.author.id);
            setTimeout(() => {
                usedCommand.delete(message.author.id);
            }, 5000); 
        }
    }

    module.exports.config = {
        name: "unorole",
        description: "",
        usage: "?unorole",
        accessableby: "Admins",
        aliases: []
    }

I Also tried the normal message send

message.channel.send(`Users that has no role.`);    
message.channel.send(message.guild.members.cache.filter(member => member.roles.cache.array().length < 2).map(member => member.user.tag).join('\n'))

Error: enter image description here


Solution

  • I already fixed it.

    //display the gathered data of users
                      const therole = message.guild.roles.cache.find(role => role.id == mentionedrole);             
                      //message.guild.roles.cache.get(mentionedrole).members.map(m=>m.user.tag.toUpperCase()).sort().join('\n') 
                      const ListEmbed = new Discord.MessageEmbed() 
                      .setTitle(`Prune Bot | Users`)
                      .setDescription(`Users that has ${therole} role. \n React ▶️ to view the list`)
                      .setColor('#b491c8')                  
                      .setFooter('PruneBot is created by Mashwishi', 'https://i.imgur.com/DxWDaGv.png');
                      var listMsg = await message.channel.send(ListEmbed); 
                          var page = parseInt(args[0]);
                          if (!page) {
                              page = 1;
                          };
                          await listMsg.react("◀️");
                          await listMsg.react("▶️");
                          const filter = (reaction, user) => ["◀️", "▶️"].includes(reaction.emoji.name) && user.id === message.author.id;
                          const collector = listMsg.createReactionCollector(filter, {
                              time: 120000
                          });
                          collector.on('collect', (reaction, user) => {
                              reaction.emoji.reaction.users.remove(user.id);
                              switch (reaction.emoji.name) {
                                  case "◀️":
                                      --page;
                                      if (page < 1) {
                                          page = 1;
                                      };
                                      const newlistMsga = new Discord.MessageEmbed()
                                      .setDescription(`Users that has ${therole} role.`)
                                      .setColor('#b491c8')      
                                      .addFields(
                                        { name: 'List:', value: getUsers(page)} 
                                      )                                         
                                      .setFooter('PruneBot is created by Mashwishi', 'https://i.imgur.com/DxWDaGv.png');  
                                      listMsg.edit(newlistMsga);
                                      break;
                                  case "▶️":
                                      //Increment the page number
                                      ++page;
                                      const newlistMsgb = new Discord.MessageEmbed()
                                      .setTitle(`Prune Bot | Users`)
                                      .setDescription(`Users that has ${therole} role.`)
                                      .setColor('#b491c8')      
                                      .addFields(
                                        { name: 'List:', value: getUsers(page)} 
                                      )                                         
                                      .setFooter('PruneBot is created by Mashwishi', 'https://i.imgur.com/DxWDaGv.png');                                                                    
                                      listMsg.edit(newlistMsgb);
                                      break;
                              };
                          });
                          collector.on('end', collected => {
                              return message.channel.send("I'm done looking for reactions on the message!");
                          });
                          function getUsers(n) {
                              const list = message.guild.roles.cache.get(mentionedrole).members.map(m => m.user.tag.toUpperCase()).sort();
                              var pageNum = (n * 10) - 10;
                              if (!pageNum) {
                                  pageNum = 0;
                              };
                              return list.slice(pageNum, pageNum + 9).join("\n");
                          };