3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00
zeppelin/backend/src/plugins/ModActions/commands/CasesModCmd.ts
Tiago R 10bb0b67bc some more patches thanks to ruby
Signed-off-by: GitHub <noreply@github.com>
2023-12-27 20:20:17 +00:00

83 lines
2.9 KiB
TypeScript

import { APIEmbed, GuildMember, User } from "discord.js";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendErrorMessage } from "../../../pluginUtils";
import { emptyEmbedValue, renderUsername, resolveMember, resolveUser, trimLines } from "../../../utils";
import { asyncMap } from "../../../utils/async";
import { createPaginatedMessage } from "../../../utils/createPaginatedMessage";
import { getChunkedEmbedFields } from "../../../utils/getChunkedEmbedFields";
import { getGuildPrefix } from "../../../utils/getGuildPrefix";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { modActionsCmd } from "../types";
const opts = {
mod: ct.userId({ option: true }),
};
const casesPerPage = 5;
export const CasesModCmd = modActionsCmd({
trigger: ["cases", "modlogs", "infractions"],
permission: "can_view",
description: "Show the most recent 5 cases by the specified -mod",
signature: [
{
...opts,
},
],
async run({ pluginData, message: msg, args }) {
const modId = args.mod || msg.author.id;
const mod =
(await resolveMember(pluginData.client, pluginData.guild, modId)) ||
(await resolveUser(pluginData.client, modId));
const modName = mod instanceof User ? renderUsername(mod) : modId;
const casesPlugin = pluginData.getPlugin(CasesPlugin);
const totalCases = await casesPlugin.getTotalCasesByMod(modId);
if (totalCases === 0) {
sendErrorMessage(pluginData, msg.channel, `No cases by **${modName}**`);
return;
}
const totalPages = Math.max(Math.ceil(totalCases / casesPerPage), 1);
const prefix = getGuildPrefix(pluginData);
createPaginatedMessage(
pluginData.client,
msg.channel,
totalPages,
async (page) => {
const cases = await casesPlugin.getRecentCasesByMod(modId, casesPerPage, (page - 1) * casesPerPage);
const lines = await asyncMap(cases, (c) => casesPlugin.getCaseSummary(c, true, msg.author.id));
const firstCaseNum = (page - 1) * casesPerPage + 1;
const lastCaseNum = page * casesPerPage;
const title = `Most recent cases ${firstCaseNum}-${lastCaseNum} of ${totalCases} by ${modName}`;
const embed = {
author: {
name: title,
icon_url: mod instanceof User || mod instanceof GuildMember ? mod.displayAvatarURL() : undefined,
},
fields: [
...getChunkedEmbedFields(emptyEmbedValue, lines.join("\n")),
{
name: emptyEmbedValue,
value: trimLines(`
Use \`${prefix}case <num>\` to see more information about an individual case
Use \`${prefix}cases <user>\` to see a specific user's cases
`),
},
],
} satisfies APIEmbed;
return { embeds: [embed] };
},
{
limitToUserId: msg.author.id,
},
);
},
});