mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-23 09:35:02 +00:00
perf: rolescmd with -counts option
This commit is contained in:
parent
c84d1a0be1
commit
dabec4539d
1 changed files with 7 additions and 6 deletions
|
@ -34,17 +34,18 @@ export const RolesCmd = utilityCmd({
|
||||||
if (args.counts) {
|
if (args.counts) {
|
||||||
await refreshMembersIfNeeded(guild);
|
await refreshMembersIfNeeded(guild);
|
||||||
|
|
||||||
// If the user requested role member counts as well, fetch them and sort the roles by their member count
|
const roleCounts = new Map<string, number>();
|
||||||
const roleCounts: Map<string, number> = Array.from(guild.roles.cache.values()).reduce((map, role) => {
|
for (const member of guild.members.cache.values()) {
|
||||||
map.set(role.id, role.members.size);
|
for (const id of member.roles.cache.keys()) {
|
||||||
return map;
|
roleCounts.set(id, (roleCounts.get(id) ?? 0) + 1);
|
||||||
}, new Map());
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The "everyone" role always has all members in it
|
// The "everyone" role always has all members in it
|
||||||
roleCounts.set(guild.id, guild.memberCount);
|
roleCounts.set(guild.id, guild.memberCount);
|
||||||
|
|
||||||
for (const role of roles) {
|
for (const role of roles) {
|
||||||
role._memberCount = roleCounts.has(role.id) ? roleCounts.get(role.id) : 0;
|
role._memberCount = roleCounts.get(role.id) ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sort) sort = "-memberCount";
|
if (!sort) sort = "-memberCount";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue