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 \` to see more information about an individual case Use \`${prefix}cases \` to see a specific user's cases `), }, ], } satisfies APIEmbed; return { embeds: [embed] }; }, { limitToUserId: msg.author.id, }, ); }, });