使用 Embed 列出没有角色的用户在太多时会出错

Posted

技术标签:

【中文标题】使用 Embed 列出没有角色的用户在太多时会出错【英文标题】:Listing users without roles with Embed getting error when too many 【发布时间】:2021-10-04 17:46:37 【问题描述】:

当没有角色的用户只有大约 1-15 个用户列表时它可以工作,但是当它超过 100 个用户时它无法打印并且它给出错误,这是说它达到了文本的限制嵌入..你们能帮我了解如何生成具有页面功能的列表吗?

编辑:人们说阅读错误,我知道错误超出了嵌入的限制,我试图寻求帮助是一种为嵌入创建动态页面的方法,我已经尝试过 discord.js-pagination 但没有希望它是手动创建而不是动态的。

顺便说一下,我使用 discord.js^12.5.1

版本

    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: []
    

我也试过正常的消息发送

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'))

错误:

【问题讨论】:

我有一个简短的问题,与您的问题无关,但是您为什么要过滤掉少于 2 个角色的成员? 我想我当时是因为意图而设置的.. 【参考方案1】:

我已经修好了。

//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");
                      ;

【讨论】:

【参考方案2】:

错误清楚地表明消息太长。限制消息的长度。

【讨论】:

仔细阅读上面的文字,反正我解决了

以上是关于使用 Embed 列出没有角色的用户在太多时会出错的主要内容,如果未能解决你的问题,请参考以下文章

如何在雪花数据库中列出所有用户的角色?

根据用户角色swagger列出api

使用mysql中的访问列在php中设置用户角色时出错

创建角色会出错

基于RBAC权限管理

如何在 ASP.NET MVC 5 中列出具有角色名称的用户