2020-10-01 01:43:38 +03:00
|
|
|
|
import { modActionsCmd } from "../types";
|
2020-07-24 02:25:33 +02:00
|
|
|
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
|
|
|
|
import { sendErrorMessage } from "../../../pluginUtils";
|
2020-10-01 01:43:38 +03:00
|
|
|
|
import { CasesPlugin } from "../../../plugins/Cases/CasesPlugin";
|
2020-08-10 03:18:34 +03:00
|
|
|
|
import {
|
|
|
|
|
UnknownUser,
|
|
|
|
|
multiSorter,
|
|
|
|
|
trimLines,
|
|
|
|
|
createChunkedMessage,
|
|
|
|
|
resolveUser,
|
|
|
|
|
emptyEmbedValue,
|
|
|
|
|
chunkArray,
|
2020-10-01 01:43:38 +03:00
|
|
|
|
} from "../../../utils";
|
2020-08-10 03:18:34 +03:00
|
|
|
|
import { getGuildPrefix } from "../../../utils/getGuildPrefix";
|
|
|
|
|
import { EmbedOptions, User } from "eris";
|
|
|
|
|
import { getChunkedEmbedFields } from "../../../utils/getChunkedEmbedFields";
|
|
|
|
|
import { asyncMap } from "../../../utils/async";
|
2021-04-28 21:51:09 +02:00
|
|
|
|
import { CaseTypes } from "../../../data/CaseTypes";
|
2020-07-24 02:25:33 +02:00
|
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
|
expand: ct.bool({ option: true, isSwitch: true, shortcut: "e" }),
|
|
|
|
|
hidden: ct.bool({ option: true, isSwitch: true, shortcut: "h" }),
|
2021-04-28 21:51:09 +02:00
|
|
|
|
reverseFilters: ct.switchOption({ shortcut: "r" }),
|
|
|
|
|
notes: ct.switchOption({ shortcut: "n" }),
|
|
|
|
|
warns: ct.switchOption({ shortcut: "w" }),
|
|
|
|
|
mutes: ct.switchOption({ shortcut: "m" }),
|
|
|
|
|
unmutes: ct.switchOption({ shortcut: "um" }),
|
|
|
|
|
bans: ct.switchOption({ shortcut: "b" }),
|
|
|
|
|
unbans: ct.switchOption({ shortcut: "ub" }),
|
2020-07-24 02:25:33 +02:00
|
|
|
|
};
|
|
|
|
|
|
2020-10-01 01:43:38 +03:00
|
|
|
|
export const CasesUserCmd = modActionsCmd({
|
2020-12-12 15:22:31 -05:00
|
|
|
|
trigger: ["cases", "modlogs"],
|
2020-07-24 02:25:33 +02:00
|
|
|
|
permission: "can_view",
|
|
|
|
|
description: "Show a list of cases the specified user has",
|
|
|
|
|
|
|
|
|
|
signature: [
|
|
|
|
|
{
|
|
|
|
|
user: ct.string(),
|
|
|
|
|
|
|
|
|
|
...opts,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
async run({ pluginData, message: msg, args }) {
|
|
|
|
|
const user = await resolveUser(pluginData.client, args.user);
|
2020-12-17 04:12:49 +02:00
|
|
|
|
if (!user.id) {
|
2021-01-17 21:21:18 +02:00
|
|
|
|
sendErrorMessage(pluginData, msg.channel, `User not found`);
|
|
|
|
|
return;
|
2020-12-17 04:12:49 +02:00
|
|
|
|
}
|
2020-07-24 02:25:33 +02:00
|
|
|
|
|
2021-04-28 21:51:09 +02:00
|
|
|
|
let cases = await pluginData.state.cases.with("notes").getByUserId(user.id);
|
|
|
|
|
|
|
|
|
|
const typesToShow: CaseTypes[] = [];
|
|
|
|
|
if (args.notes) typesToShow.push(CaseTypes.Note);
|
|
|
|
|
if (args.warns) typesToShow.push(CaseTypes.Warn);
|
|
|
|
|
if (args.mutes) typesToShow.push(CaseTypes.Mute);
|
|
|
|
|
if (args.unmutes) typesToShow.push(CaseTypes.Unmute);
|
|
|
|
|
if (args.bans) typesToShow.push(CaseTypes.Ban);
|
|
|
|
|
if (args.unbans) typesToShow.push(CaseTypes.Unban);
|
|
|
|
|
|
|
|
|
|
if (typesToShow.length > 0) {
|
|
|
|
|
// Reversed: Hide specified types
|
|
|
|
|
if (args.reverseFilters) cases = cases.filter(c => !typesToShow.includes(c.type));
|
|
|
|
|
// Normal: Show only specified types
|
|
|
|
|
else cases = cases.filter(c => typesToShow.includes(c.type));
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 02:25:33 +02:00
|
|
|
|
const normalCases = cases.filter(c => !c.is_hidden);
|
|
|
|
|
const hiddenCases = cases.filter(c => c.is_hidden);
|
|
|
|
|
|
|
|
|
|
const userName =
|
|
|
|
|
user instanceof UnknownUser && cases.length
|
|
|
|
|
? cases[cases.length - 1].user_name
|
|
|
|
|
: `${user.username}#${user.discriminator}`;
|
|
|
|
|
|
|
|
|
|
if (cases.length === 0) {
|
|
|
|
|
msg.channel.createMessage(`No cases found for **${userName}**`);
|
|
|
|
|
} else {
|
|
|
|
|
const casesToDisplay = args.hidden ? cases : normalCases;
|
|
|
|
|
|
|
|
|
|
if (args.expand) {
|
|
|
|
|
if (casesToDisplay.length > 8) {
|
|
|
|
|
msg.channel.createMessage("Too many cases for expanded view. Please use compact view instead.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expanded view (= individual case embeds)
|
|
|
|
|
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
|
|
|
|
for (const theCase of casesToDisplay) {
|
|
|
|
|
const embed = await casesPlugin.getCaseEmbed(theCase.id);
|
|
|
|
|
msg.channel.createMessage(embed);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Compact view (= regular message with a preview of each case)
|
2020-08-10 03:18:34 +03:00
|
|
|
|
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
2020-08-19 00:19:12 +03:00
|
|
|
|
const lines = await asyncMap(casesToDisplay, c => casesPlugin.getCaseSummary(c, true, msg.author.id));
|
2020-07-24 02:25:33 +02:00
|
|
|
|
|
2020-08-10 03:18:34 +03:00
|
|
|
|
const prefix = getGuildPrefix(pluginData);
|
2020-10-13 19:26:16 +03:00
|
|
|
|
const linesPerChunk = 10;
|
2020-08-10 03:18:34 +03:00
|
|
|
|
const lineChunks = chunkArray(lines, linesPerChunk);
|
2020-07-24 02:25:33 +02:00
|
|
|
|
|
2020-08-10 03:18:34 +03:00
|
|
|
|
const footerField = {
|
|
|
|
|
name: emptyEmbedValue,
|
|
|
|
|
value: trimLines(`
|
|
|
|
|
Use \`${prefix}case <num>\` to see more information about an individual case
|
|
|
|
|
`),
|
|
|
|
|
};
|
2020-07-24 02:25:33 +02:00
|
|
|
|
|
2020-08-10 03:18:34 +03:00
|
|
|
|
for (const [i, linesInChunk] of lineChunks.entries()) {
|
|
|
|
|
const isLastChunk = i === lineChunks.length - 1;
|
2020-07-24 02:25:33 +02:00
|
|
|
|
|
2020-08-10 03:18:34 +03:00
|
|
|
|
if (isLastChunk && !args.hidden && hiddenCases.length) {
|
|
|
|
|
if (hiddenCases.length === 1) {
|
|
|
|
|
linesInChunk.push(`*+${hiddenCases.length} hidden case, use "-hidden" to show it*`);
|
|
|
|
|
} else {
|
|
|
|
|
linesInChunk.push(`*+${hiddenCases.length} hidden cases, use "-hidden" to show them*`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const chunkStart = i * linesPerChunk + 1;
|
|
|
|
|
const chunkEnd = Math.min((i + 1) * linesPerChunk, lines.length);
|
2020-07-24 02:25:33 +02:00
|
|
|
|
|
2020-08-10 03:18:34 +03:00
|
|
|
|
const embed: EmbedOptions = {
|
|
|
|
|
author: {
|
|
|
|
|
name:
|
|
|
|
|
lineChunks.length === 1
|
|
|
|
|
? `Cases for ${userName} (${lines.length} total)`
|
|
|
|
|
: `Cases ${chunkStart}–${chunkEnd} of ${lines.length} for ${userName}`,
|
|
|
|
|
icon_url: user instanceof User ? user.avatarURL || user.defaultAvatarURL : undefined,
|
|
|
|
|
},
|
|
|
|
|
fields: [
|
|
|
|
|
...getChunkedEmbedFields(emptyEmbedValue, linesInChunk.join("\n")),
|
|
|
|
|
...(isLastChunk ? [footerField] : []),
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
msg.channel.createMessage({ embed });
|
|
|
|
|
}
|
2020-07-24 02:25:33 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|