diff --git a/backend/src/plugins/Utility/commands/RolesCmd.ts b/backend/src/plugins/Utility/commands/RolesCmd.ts index 6734d167..86c09554 100644 --- a/backend/src/plugins/Utility/commands/RolesCmd.ts +++ b/backend/src/plugins/Utility/commands/RolesCmd.ts @@ -1,4 +1,4 @@ -import { Role, TextChannel } from "discord.js"; +import { Role } from "discord.js"; import { commandTypeHelpers as ct } from "../../../commandTypes"; import { sendErrorMessage } from "../../../pluginUtils"; import { chunkArray, sorter, trimLines } from "../../../utils"; @@ -21,9 +21,7 @@ export const RolesCmd = utilityCmd({ async run({ message: msg, args, pluginData }) { const { guild } = pluginData; - let roles: Array<{ _memberCount?: number } & Role> = Array.from( - (msg.channel as TextChannel).guild.roles.cache.values(), - ); + let roles: Role[] = Array.from(guild.roles.cache.values()); let sort = args.sort; if (args.search) { @@ -31,41 +29,28 @@ export const RolesCmd = utilityCmd({ roles = roles.filter((r) => r.name.toLowerCase().includes(searchStr) || r.id === searchStr); } + let roleCounts: Map | null = null; 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()); + roleCounts = new Map(guild.roles.cache.map((r) => [r.id, 0])); - // 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; + 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); + if (!sort) sort = "-memberCount"; - roles.sort((a, b) => { - if (a._memberCount! > b._memberCount!) return -1; - if (a._memberCount! < b._memberCount!) return 1; - return 0; - }); - } else { - // Otherwise sort by name - roles.sort((a, b) => { - if (a.name.toLowerCase() > b.name.toLowerCase()) return 1; - if (a.name.toLowerCase() < b.name.toLowerCase()) return -1; - return 0; - }); } if (!sort) sort = "name"; let sortDir: "ASC" | "DESC" = "ASC"; - if (sort && sort[0] === "-") { + if (sort[0] === "-") { sort = sort.slice(1); sortDir = "DESC"; } @@ -73,7 +58,7 @@ export const RolesCmd = utilityCmd({ if (sort === "position" || sort === "order") { roles.sort(sorter("position", sortDir)); } else if (sort === "memberCount" && args.counts) { - roles.sort(sorter("_memberCount", sortDir)); + roles.sort((first, second) => roleCounts!.get(second.id)! - roleCounts!.get(first.id)!); } else if (sort === "name") { roles.sort(sorter((r) => r.name.toLowerCase(), sortDir)); } else { @@ -88,21 +73,23 @@ export const RolesCmd = utilityCmd({ const roleLines = chunk.map((role) => { const paddedId = role.id.padEnd(longestId, " "); let line = `${paddedId} ${role.name}`; - if (role._memberCount != null) { - line += role._memberCount === 1 ? ` (${role._memberCount} member)` : ` (${role._memberCount} members)`; + const memberCount = roleCounts?.get(role.id); + if (memberCount !== undefined) { + line += ` (${memberCount} ${memberCount === 1 ? "member" : "members"})`; } return line; }); + const codeBlock = "```py\n" + roleLines.join("\n") + "```"; if (i === 0) { msg.channel.send( trimLines(` ${args.search ? "Total roles found" : "Total roles"}: ${roles.length} - \`\`\`py\n${roleLines.join("\n")}\`\`\` + ${codeBlock} `), ); } else { - msg.channel.send("```py\n" + roleLines.join("\n") + "```"); + msg.channel.send(codeBlock); } } },