From dabec4539d46f6351eae177bfdf51dca449f689a Mon Sep 17 00:00:00 2001 From: almeidx Date: Tue, 21 Sep 2021 13:26:34 +0100 Subject: [PATCH] perf: rolescmd with -counts option --- backend/src/plugins/Utility/commands/RolesCmd.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/backend/src/plugins/Utility/commands/RolesCmd.ts b/backend/src/plugins/Utility/commands/RolesCmd.ts index 6734d167..5e7726dd 100644 --- a/backend/src/plugins/Utility/commands/RolesCmd.ts +++ b/backend/src/plugins/Utility/commands/RolesCmd.ts @@ -34,17 +34,18 @@ export const RolesCmd = utilityCmd({ if (args.counts) { await refreshMembersIfNeeded(guild); - // If the user requested role member counts as well, fetch them and sort the roles by their member count - const roleCounts: Map = Array.from(guild.roles.cache.values()).reduce((map, role) => { - map.set(role.id, role.members.size); - return map; - }, new Map()); + const roleCounts = new Map(); + for (const member of guild.members.cache.values()) { + for (const id of member.roles.cache.keys()) { + roleCounts.set(id, (roleCounts.get(id) ?? 0) + 1); + } + } // The "everyone" role always has all members in it roleCounts.set(guild.id, guild.memberCount); 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";