2020-07-05 05:00:54 +03:00
|
|
|
import { utilityCmd } from "../types";
|
2020-07-06 03:05:40 +03:00
|
|
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
2021-06-01 02:05:55 +02:00
|
|
|
|
2020-07-05 05:00:54 +03:00
|
|
|
import { chunkArray, sorter, trimLines } from "../../../utils";
|
|
|
|
import { refreshMembersIfNeeded } from "../refreshMembers";
|
|
|
|
import { sendErrorMessage } from "../../../pluginUtils";
|
2021-06-02 04:07:50 +02:00
|
|
|
import { Role, TextChannel } from "discord.js";
|
2020-07-05 05:00:54 +03:00
|
|
|
|
2020-07-05 15:03:51 +03:00
|
|
|
export const RolesCmd = utilityCmd({
|
|
|
|
trigger: "roles",
|
|
|
|
description: "List all roles or roles matching a search",
|
|
|
|
usage: "!roles mod",
|
|
|
|
permission: "can_roles",
|
|
|
|
|
|
|
|
signature: {
|
2020-08-02 22:31:56 +02:00
|
|
|
search: ct.string({ required: false, catchAll: true }),
|
2020-07-05 05:00:54 +03:00
|
|
|
|
2020-07-06 03:05:40 +03:00
|
|
|
counts: ct.switchOption(),
|
|
|
|
sort: ct.string({ option: true }),
|
2020-07-05 05:00:54 +03:00
|
|
|
},
|
|
|
|
|
2020-07-05 15:03:51 +03:00
|
|
|
async run({ message: msg, args, pluginData }) {
|
2020-07-05 05:00:54 +03:00
|
|
|
const { guild } = pluginData;
|
|
|
|
|
2021-06-02 04:07:50 +02:00
|
|
|
let roles: Array<{ _memberCount?: number } & Role> = Array.from(
|
|
|
|
(msg.channel as TextChannel).guild.roles.cache.values(),
|
|
|
|
);
|
2020-07-05 05:00:54 +03:00
|
|
|
let sort = args.sort;
|
|
|
|
|
|
|
|
if (args.search) {
|
|
|
|
const searchStr = args.search.toLowerCase();
|
|
|
|
roles = roles.filter(r => r.name.toLowerCase().includes(searchStr) || r.id === searchStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.counts) {
|
|
|
|
await refreshMembersIfNeeded(guild);
|
|
|
|
|
|
|
|
// If the user requested role member counts as well, calculate them and sort the roles by their member count
|
2021-06-02 04:07:50 +02:00
|
|
|
const roleCounts: Map<string, number> = Array.from(guild.members.cache.values()).reduce((map, member) => {
|
|
|
|
for (const roleId of member.roles.cache) {
|
2020-07-05 05:00:54 +03:00
|
|
|
if (!map.has(roleId)) map.set(roleId, 0);
|
|
|
|
map.set(roleId, map.get(roleId) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}, new Map());
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sort) sort = "-memberCount";
|
|
|
|
roles.sort((a, b) => {
|
2020-11-09 20:03:57 +02:00
|
|
|
if (a._memberCount! > b._memberCount!) return -1;
|
|
|
|
if (a._memberCount! < b._memberCount!) return 1;
|
2020-07-05 05:00:54 +03:00
|
|
|
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] === "-") {
|
|
|
|
sort = sort.slice(1);
|
|
|
|
sortDir = "DESC";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sort === "position" || sort === "order") {
|
|
|
|
roles.sort(sorter("position", sortDir));
|
|
|
|
} else if (sort === "memberCount" && args.counts) {
|
|
|
|
roles.sort(sorter("_memberCount", sortDir));
|
|
|
|
} else if (sort === "name") {
|
|
|
|
roles.sort(sorter(r => r.name.toLowerCase(), sortDir));
|
|
|
|
} else {
|
|
|
|
sendErrorMessage(pluginData, msg.channel, "Unknown sorting method");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const longestId = roles.reduce((longest, role) => Math.max(longest, role.id.length), 0);
|
|
|
|
|
|
|
|
const chunks = chunkArray(roles, 20);
|
|
|
|
for (const [i, chunk] of chunks.entries()) {
|
|
|
|
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)`;
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (i === 0) {
|
2021-06-02 04:07:50 +02:00
|
|
|
msg.channel.send(
|
2020-07-05 05:00:54 +03:00
|
|
|
trimLines(`
|
|
|
|
${args.search ? "Total roles found" : "Total roles"}: ${roles.length}
|
|
|
|
\`\`\`py\n${roleLines.join("\n")}\`\`\`
|
|
|
|
`),
|
|
|
|
);
|
|
|
|
} else {
|
2021-06-02 04:07:50 +02:00
|
|
|
msg.channel.send("```py\n" + roleLines.join("\n") + "```");
|
2020-07-05 05:00:54 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 15:03:51 +03:00
|
|
|
},
|
|
|
|
});
|