Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions backend/src/data/GuildCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ export class GuildCases extends BaseGuildRepository {
});
}

async getByModId(
modId: string,
filters: Omit<FindOptionsWhere<Case>, "guild_id" | "mod_id"> = {},
): Promise<Case[]> {
const where: FindOptionsWhere<Case> = {
guild_id: this.guildId,
mod_id: modId,
is_hidden: false,
...filters,
};

if (where.is_hidden === true) {
delete where.is_hidden;
}

return this.cases.find({
relations: this.getRelations(),
where,
order: {
case_number: "DESC",
},
});
}

async getMinCaseNumber(): Promise<number> {
const result = await this.cases
.createQueryBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const opts = {
mod: ct.userId({ option: true }),
expand: ct.bool({ option: true, isSwitch: true, shortcut: "e" }),
hidden: ct.bool({ option: true, isSwitch: true, shortcut: "h" }),
reverseFilters: ct.switchOption({ def: false, shortcut: "r" }),
reverseFilters: ct.switchOption({ def: false, shortcut: "rf" }),
reason: ct.string({ option: true, shortcut: "r" }),
notes: ct.switchOption({ def: false, shortcut: "n" }),
warns: ct.switchOption({ def: false, shortcut: "w" }),
mutes: ct.switchOption({ def: false, shortcut: "m" }),
Expand Down Expand Up @@ -45,6 +46,7 @@ export const CasesModMsgCmd = modActionsMsgCmd({
args.bans,
args.unbans,
args.reverseFilters,
args.reason ?? null,
args.hidden,
args.expand,
args.show,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const opts = [
description: "To treat case type filters as exclusive instead of inclusive",
required: false,
}),
slashOptions.string({
name: "reason",
description: "Filter cases containing this text in the case reason",
required: false,
}),
slashOptions.boolean({ name: "notes", description: "To filter notes", required: false }),
slashOptions.boolean({ name: "warns", description: "To filter warns", required: false }),
slashOptions.boolean({ name: "mutes", description: "To filter mutes", required: false }),
Expand Down Expand Up @@ -48,6 +53,7 @@ export const CasesSlashCmd = modActionsSlashCmd({
options.bans,
options.unbans,
options["reverse-filters"],
options.reason ?? null,
options.hidden,
options.expand,
options.show,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const opts = {
mod: ct.userId({ option: true }),
expand: ct.bool({ option: true, isSwitch: true, shortcut: "e" }),
hidden: ct.bool({ option: true, isSwitch: true, shortcut: "h" }),
reverseFilters: ct.switchOption({ def: false, shortcut: "r" }),
reverseFilters: ct.switchOption({ def: false, shortcut: "rf" }),
reason: ct.string({ option: true, shortcut: "r" }),
notes: ct.switchOption({ def: false, shortcut: "n" }),
warns: ct.switchOption({ def: false, shortcut: "w" }),
mutes: ct.switchOption({ def: false, shortcut: "m" }),
Expand Down Expand Up @@ -58,6 +59,7 @@ export const CasesUserMsgCmd = modActionsMsgCmd({
args.bans,
args.unbans,
args.reverseFilters,
args.reason ?? null,
args.hidden,
args.expand,
args.show,
Expand Down
64 changes: 49 additions & 15 deletions backend/src/plugins/ModActions/commands/cases/actualCasesCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ import { ModActionsPluginType } from "../../types.js";
const casesPerPage = 5;
const maxExpandedCases = 8;

function filterCasesByReason(cases: Case[], reason: string): Case[] {
const normalizedReason = reason.toLowerCase();

return cases.filter((theCase) =>
theCase.notes?.some((note) => note.body.toLowerCase().includes(normalizedReason)),
);
}

async function sendExpandedCases(
pluginData: GuildPluginData<ModActionsPluginType>,
context: Message | ChatInputCommandInteraction,
Expand Down Expand Up @@ -54,6 +62,7 @@ async function casesUserCmd(
user: GuildMember | User | UnknownUser,
modName: string,
typesToShow: CaseTypes[],
reason: string | null,
hidden: boolean | null,
expand: boolean | null,
show: boolean | null,
Expand All @@ -66,26 +75,29 @@ async function casesUserCmd(
}

const cases = await pluginData.state.cases.with("notes").getByUserId(user.id, casesFilters);
const normalCases = cases.filter((c) => !c.is_hidden);
const hiddenCases = cases.filter((c) => c.is_hidden);
const matchingCases = reason ? filterCasesByReason(cases, reason) : cases;
const normalCases = matchingCases.filter((c) => !c.is_hidden);
const hiddenCases = matchingCases.filter((c) => c.is_hidden);

const userName =
user instanceof UnknownUser && cases.length ? cases[cases.length - 1].user_name : renderUsername(user);

if (cases.length === 0) {
if (matchingCases.length === 0) {
await sendContextResponse(context, {
content: `No cases found for **${userName}**${modId ? ` by ${modName}` : ""}.`,
content: `No cases found for **${userName}**${modId ? ` by ${modName}` : ""}${
reason ? ` with reason matching "${reason}"` : ""
}.`,
ephemeral: !show,
});

return;
}

const casesToDisplay = hidden ? cases : normalCases;
const casesToDisplay = hidden ? matchingCases : normalCases;

if (!casesToDisplay.length) {
await sendContextResponse(context, {
content: `No normal cases found for **${userName}**. Use "-hidden" to show ${cases.length} hidden cases.`,
content: `No normal cases found for **${userName}**. Use "-hidden" to show ${matchingCases.length} hidden cases.`,
ephemeral: !show,
});

Expand Down Expand Up @@ -148,17 +160,31 @@ async function casesModCmd(
mod: GuildMember | User | UnknownUser,
modName: string,
typesToShow: CaseTypes[],
reason: string | null,
hidden: boolean | null,
expand: boolean | null,
show: boolean | null,
) {
const casesPlugin = pluginData.getPlugin(CasesPlugin);
const casesFilters = { type: In(typesToShow), is_hidden: !!hidden };

const totalCases = await casesPlugin.getTotalCasesByMod(modId ?? author.id, casesFilters);
const filteredCases = reason
? filterCasesByReason(
await pluginData.state.cases.with("notes").getByModId(modId ?? author.id, casesFilters),
reason,
)
: null;

const totalCases = filteredCases?.length ?? (await casesPlugin.getTotalCasesByMod(modId ?? author.id, casesFilters));

if (totalCases === 0) {
pluginData.state.common.sendErrorMessage(context, `No cases by **${modName}**`, undefined, undefined, !show);
pluginData.state.common.sendErrorMessage(
context,
`No cases by **${modName}**${reason ? ` with reason matching "${reason}"` : ""}`,
undefined,
undefined,
!show,
);

return;
}
Expand All @@ -168,7 +194,10 @@ async function casesModCmd(

if (expand) {
// Expanded view (= individual case embeds)
const cases = totalCases > 8 ? [] : await casesPlugin.getRecentCasesByMod(modId ?? author.id, 8, 0, casesFilters);
const cases =
filteredCases && totalCases > maxExpandedCases
? filteredCases.slice(0, maxExpandedCases)
: filteredCases ?? (await casesPlugin.getRecentCasesByMod(modId ?? author.id, 8, 0, casesFilters));

await sendExpandedCases(pluginData, context, totalCases, cases, show);
return;
Expand All @@ -179,12 +208,14 @@ async function casesModCmd(
context,
totalPages,
async (page) => {
const cases = await casesPlugin.getRecentCasesByMod(
modId ?? author.id,
casesPerPage,
(page - 1) * casesPerPage,
casesFilters,
);
const cases = filteredCases
? filteredCases.slice((page - 1) * casesPerPage, (page - 1) * casesPerPage + casesPerPage)
: await casesPlugin.getRecentCasesByMod(
modId ?? author.id,
casesPerPage,
(page - 1) * casesPerPage,
casesFilters,
);

const lines = await asyncMap(cases, (c) => casesPlugin.getCaseSummary(c, true, author.id));
const firstCaseNum = (page - 1) * casesPerPage + 1;
Expand Down Expand Up @@ -230,6 +261,7 @@ export async function actualCasesCmd(
bans: boolean | null,
unbans: boolean | null,
reverseFilters: boolean | null,
reason: string | null,
hidden: boolean | null,
expand: boolean | null,
show: boolean | null,
Expand Down Expand Up @@ -275,6 +307,7 @@ export async function actualCasesCmd(
user,
modName,
typesToShow,
reason,
hidden,
expand,
show === true,
Expand All @@ -287,6 +320,7 @@ export async function actualCasesCmd(
mod ?? author,
modName,
typesToShow,
reason,
hidden,
expand,
show === true,
Expand Down
Loading