简体   繁体   English

角色成员计数仅统计发送命令的用户的角色 discord.js v12

[英]Role member count only counts roles of the user who sent the command discord.js v12

I'm trying to send a list of all of the roles in a server with the member count beside it.我正在尝试发送服务器中所有角色的列表,旁边有成员计数。 It successfully sends the role names but only counts the roles of the user who sent the command.它成功发送了角色名称,但只计算发送命令的用户的角色。 For example, if a user only has RoleA , instead of sending RoleA - 3 RoleB - 5 RoleC - 2 , it will send RoleA - 1 RoleB - 0 RoleC - 0 .例如,如果用户只有RoleA ,而不是发送RoleA - 3 RoleB - 5 RoleC - 2 ,它将发送RoleA - 1 RoleB - 0 RoleC - 0

const roles = client.guilds.cache.map(guild => {    
    return guild.roles.cache.map(role=>role.name).flat().filter(item => item !== '');
});

roles.sort().forEach(roleName => {
    let memberCount = msg.guild.roles.cache.find(role => role.name === roleName).members.size;
    
    if(roleName !== '@everyone')
        roles_as_list += '🔘 ' +roleName+ ' - ' +memberCount+ '\n';
});

msg.channel.send(roles_as_list);

It's not a fix for your code, but here is a way how to do it much easier:它不是您的代码的修复程序,但这里有一种方法可以更轻松地完成此操作:

let roles_as_list = msg.guild.roles.cache.filter(r => r.name !== `@everyone`).sort().map(r => `🔘 ${r.name} - ${r.members.filter(m => !m.user.bot).size}`).join(`\n`);
msg.channel.send(roles_as_list)

And just like that, you'll have server roles map that excludes bots from the list就像那样,您将拥有从列表中排除机器人的服务器角色映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM