mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 22:21:51 +00:00
perf: rolescmd with -counts option (#294)
This commit is contained in:
parent
c7751a9da1
commit
2042459fd2
1 changed files with 19 additions and 32 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { Role, TextChannel } from "discord.js";
|
import { Role } from "discord.js";
|
||||||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||||
import { sendErrorMessage } from "../../../pluginUtils";
|
import { sendErrorMessage } from "../../../pluginUtils";
|
||||||
import { chunkArray, sorter, trimLines } from "../../../utils";
|
import { chunkArray, sorter, trimLines } from "../../../utils";
|
||||||
|
@ -21,9 +21,7 @@ export const RolesCmd = utilityCmd({
|
||||||
async run({ message: msg, args, pluginData }) {
|
async run({ message: msg, args, pluginData }) {
|
||||||
const { guild } = pluginData;
|
const { guild } = pluginData;
|
||||||
|
|
||||||
let roles: Array<{ _memberCount?: number } & Role> = Array.from(
|
let roles: Role[] = Array.from(guild.roles.cache.values());
|
||||||
(msg.channel as TextChannel).guild.roles.cache.values(),
|
|
||||||
);
|
|
||||||
let sort = args.sort;
|
let sort = args.sort;
|
||||||
|
|
||||||
if (args.search) {
|
if (args.search) {
|
||||||
|
@ -31,41 +29,28 @@ export const RolesCmd = utilityCmd({
|
||||||
roles = roles.filter((r) => r.name.toLowerCase().includes(searchStr) || r.id === searchStr);
|
roles = roles.filter((r) => r.name.toLowerCase().includes(searchStr) || r.id === searchStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let roleCounts: Map<string, number> | null = null;
|
||||||
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
|
roleCounts = new Map<string, number>(guild.roles.cache.map((r) => [r.id, 0]));
|
||||||
const roleCounts: Map<string, number> = Array.from(guild.roles.cache.values()).reduce((map, role) => {
|
|
||||||
map.set(role.id, role.members.size);
|
|
||||||
return map;
|
|
||||||
}, new Map());
|
|
||||||
|
|
||||||
// The "everyone" role always has all members in it
|
for (const member of guild.members.cache.values()) {
|
||||||
roleCounts.set(guild.id, guild.memberCount);
|
for (const id of member.roles.cache.keys()) {
|
||||||
|
roleCounts.set(id, (roleCounts.get(id) ?? 0) + 1);
|
||||||
for (const role of roles) {
|
}
|
||||||
role._memberCount = roleCounts.has(role.id) ? roleCounts.get(role.id) : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The "@everyone" role always has all members in it
|
||||||
|
roleCounts.set(guild.id, guild.memberCount);
|
||||||
|
|
||||||
if (!sort) sort = "-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";
|
if (!sort) sort = "name";
|
||||||
|
|
||||||
let sortDir: "ASC" | "DESC" = "ASC";
|
let sortDir: "ASC" | "DESC" = "ASC";
|
||||||
if (sort && sort[0] === "-") {
|
if (sort[0] === "-") {
|
||||||
sort = sort.slice(1);
|
sort = sort.slice(1);
|
||||||
sortDir = "DESC";
|
sortDir = "DESC";
|
||||||
}
|
}
|
||||||
|
@ -73,7 +58,7 @@ export const RolesCmd = utilityCmd({
|
||||||
if (sort === "position" || sort === "order") {
|
if (sort === "position" || sort === "order") {
|
||||||
roles.sort(sorter("position", sortDir));
|
roles.sort(sorter("position", sortDir));
|
||||||
} else if (sort === "memberCount" && args.counts) {
|
} 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") {
|
} else if (sort === "name") {
|
||||||
roles.sort(sorter((r) => r.name.toLowerCase(), sortDir));
|
roles.sort(sorter((r) => r.name.toLowerCase(), sortDir));
|
||||||
} else {
|
} else {
|
||||||
|
@ -88,21 +73,23 @@ export const RolesCmd = utilityCmd({
|
||||||
const roleLines = chunk.map((role) => {
|
const roleLines = chunk.map((role) => {
|
||||||
const paddedId = role.id.padEnd(longestId, " ");
|
const paddedId = role.id.padEnd(longestId, " ");
|
||||||
let line = `${paddedId} ${role.name}`;
|
let line = `${paddedId} ${role.name}`;
|
||||||
if (role._memberCount != null) {
|
const memberCount = roleCounts?.get(role.id);
|
||||||
line += role._memberCount === 1 ? ` (${role._memberCount} member)` : ` (${role._memberCount} members)`;
|
if (memberCount !== undefined) {
|
||||||
|
line += ` (${memberCount} ${memberCount === 1 ? "member" : "members"})`;
|
||||||
}
|
}
|
||||||
return line;
|
return line;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const codeBlock = "```py\n" + roleLines.join("\n") + "```";
|
||||||
if (i === 0) {
|
if (i === 0) {
|
||||||
msg.channel.send(
|
msg.channel.send(
|
||||||
trimLines(`
|
trimLines(`
|
||||||
${args.search ? "Total roles found" : "Total roles"}: ${roles.length}
|
${args.search ? "Total roles found" : "Total roles"}: ${roles.length}
|
||||||
\`\`\`py\n${roleLines.join("\n")}\`\`\`
|
${codeBlock}
|
||||||
`),
|
`),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
msg.channel.send("```py\n" + roleLines.join("\n") + "```");
|
msg.channel.send(codeBlock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue