From 52ee0077703c85df1af524c17c8ac30c6be310d3 Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Mon, 12 Oct 2020 14:48:54 +0200 Subject: [PATCH] Allow hidecase, unhidecase and deletecase to take multiple case numbers --- .../ModActions/commands/DeleteCaseCmd.ts | 106 +++++++++++------- .../ModActions/commands/HideCaseCmd.ts | 29 +++-- .../ModActions/commands/UnhideCaseCmd.ts | 33 ++++-- 3 files changed, 116 insertions(+), 52 deletions(-) diff --git a/backend/src/plugins/ModActions/commands/DeleteCaseCmd.ts b/backend/src/plugins/ModActions/commands/DeleteCaseCmd.ts index c8bdbd6d..2012cceb 100644 --- a/backend/src/plugins/ModActions/commands/DeleteCaseCmd.ts +++ b/backend/src/plugins/ModActions/commands/DeleteCaseCmd.ts @@ -19,57 +19,87 @@ export const DeleteCaseCmd = modActionsCmd({ `), signature: { - caseNumber: ct.number(), + caseNumber: ct.number({ rest: true }), force: ct.switchOption({ shortcut: "f" }), }, async run({ pluginData, message, args }) { - const theCase = await pluginData.state.cases.findByCaseNumber(args.caseNumber); - if (!theCase) { - sendErrorMessage(pluginData, message.channel, "Case not found"); + const failed = []; + const validCases = []; + let cancelled = 0; + + for (const num of args.caseNumber) { + const theCase = await pluginData.state.cases.findByCaseNumber(num); + if (!theCase) { + failed.push(num); + continue; + } + + validCases.push(theCase); + } + + if (failed.length === args.caseNumber.length) { + sendErrorMessage(pluginData, message.channel, "None of the cases were found!"); return; } - if (!args.force) { - const cases = pluginData.getPlugin(CasesPlugin); - const embedContent = await cases.getCaseEmbed(theCase); - message.channel.createMessage({ - content: "Delete the following case? Answer 'Yes' to continue, 'No' to cancel.", - embed: embedContent.embed, - }); + for (const theCase of validCases) { + if (!args.force) { + const cases = pluginData.getPlugin(CasesPlugin); + const embedContent = await cases.getCaseEmbed(theCase); + message.channel.createMessage({ + content: "Delete the following case? Answer 'Yes' to continue, 'No' to cancel.", + embed: embedContent.embed, + }); - const reply = await helpers.waitForReply( - pluginData.client, - message.channel as TextChannel, - message.author.id, - 15 * SECONDS, - ); - const normalizedReply = (reply?.content || "").toLowerCase().trim(); - if (normalizedReply !== "yes" && normalizedReply !== "y") { - message.channel.createMessage("Cancelled. Case was not deleted."); - return; + const reply = await helpers.waitForReply( + pluginData.client, + message.channel as TextChannel, + message.author.id, + 15 * SECONDS, + ); + const normalizedReply = (reply?.content || "").toLowerCase().trim(); + if (normalizedReply !== "yes" && normalizedReply !== "y") { + message.channel.createMessage("Cancelled. Case was not deleted."); + cancelled++; + continue; + } } + + const deletedByName = `${message.author.username}#${message.author.discriminator}`; + + const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin); + const deletedAt = timeAndDate.inGuildTz().format(timeAndDate.getDateFormat("pretty_datetime")); + + await pluginData.state.cases.softDelete( + theCase.id, + message.author.id, + deletedByName, + `Case deleted by **${deletedByName}** (\`${message.author.id}\`) on ${deletedAt}`, + ); + + const logs = pluginData.getPlugin(LogsPlugin); + logs.log(LogType.CASE_DELETE, { + mod: stripObjectToScalars(message.member, ["user", "roles"]), + case: stripObjectToScalars(theCase), + }); } - const deletedByName = `${message.author.username}#${message.author.discriminator}`; + const failedAddendum = + failed.length > 0 + ? `\nThe following cases were not found: ${failed.toString().replace(new RegExp(",", "g"), ", ")}` + : ""; + const amt = validCases.length - cancelled; + if (amt === 0) { + sendErrorMessage(pluginData, message.channel, "All deletions were cancelled, no cases were deleted."); + return; + } - const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin); - const deletedAt = timeAndDate.inGuildTz().format(timeAndDate.getDateFormat("pretty_datetime")); - - await pluginData.state.cases.softDelete( - theCase.id, - message.author.id, - deletedByName, - `Case deleted by **${deletedByName}** (\`${message.author.id}\`) on ${deletedAt}`, + sendSuccessMessage( + pluginData, + message.channel, + `${amt} case${amt === 1 ? " was" : "s were"} deleted!${failedAddendum}`, ); - - const logs = pluginData.getPlugin(LogsPlugin); - logs.log(LogType.CASE_DELETE, { - mod: stripObjectToScalars(message.member, ["user", "roles"]), - case: stripObjectToScalars(theCase), - }); - - sendSuccessMessage(pluginData, message.channel, `Case #${theCase.case_number} deleted!`); }, }); diff --git a/backend/src/plugins/ModActions/commands/HideCaseCmd.ts b/backend/src/plugins/ModActions/commands/HideCaseCmd.ts index b86a42c2..ac351c05 100644 --- a/backend/src/plugins/ModActions/commands/HideCaseCmd.ts +++ b/backend/src/plugins/ModActions/commands/HideCaseCmd.ts @@ -9,22 +9,37 @@ export const HideCaseCmd = modActionsCmd({ signature: [ { - caseNum: ct.number(), + caseNum: ct.number({ rest: true }), }, ], async run({ pluginData, message: msg, args }) { - const theCase = await pluginData.state.cases.findByCaseNumber(args.caseNum); - if (!theCase) { - sendErrorMessage(pluginData, msg.channel, "Case not found!"); - return; + const failed = []; + + for (const num of args.caseNum) { + const theCase = await pluginData.state.cases.findByCaseNumber(num); + if (!theCase) { + failed.push(num); + continue; + } + + await pluginData.state.cases.setHidden(theCase.id, true); } - await pluginData.state.cases.setHidden(theCase.id, true); + if (failed.length === args.caseNum.length) { + sendErrorMessage(pluginData, msg.channel, "None of the cases were found!"); + return; + } + const failedAddendum = + failed.length > 0 + ? `\nThe following cases were not found: ${failed.toString().replace(new RegExp(",", "g"), ", ")}` + : ""; + + const amt = args.caseNum.length - failed.length; sendSuccessMessage( pluginData, msg.channel, - `Case #${theCase.case_number} is now hidden! Use \`unhidecase\` to unhide it.`, + `${amt} case${amt === 1 ? " is" : "s are"} now hidden! Use \`unhidecase\` to unhide them.${failedAddendum}`, ); }, }); diff --git a/backend/src/plugins/ModActions/commands/UnhideCaseCmd.ts b/backend/src/plugins/ModActions/commands/UnhideCaseCmd.ts index 60e756b8..5cea8927 100644 --- a/backend/src/plugins/ModActions/commands/UnhideCaseCmd.ts +++ b/backend/src/plugins/ModActions/commands/UnhideCaseCmd.ts @@ -9,18 +9,37 @@ export const UnhideCaseCmd = modActionsCmd({ signature: [ { - caseNum: ct.number(), + caseNum: ct.number({ rest: true }), }, ], async run({ pluginData, message: msg, args }) { - const theCase = await pluginData.state.cases.findByCaseNumber(args.caseNum); - if (!theCase) { - sendErrorMessage(pluginData, msg.channel, "Case not found!"); - return; + const failed = []; + + for (const num of args.caseNum) { + const theCase = await pluginData.state.cases.findByCaseNumber(num); + if (!theCase) { + failed.push(num); + continue; + } + + await pluginData.state.cases.setHidden(theCase.id, false); } - await pluginData.state.cases.setHidden(theCase.id, false); - sendSuccessMessage(pluginData, msg.channel, `Case #${theCase.case_number} is no longer hidden!`); + if (failed.length === args.caseNum.length) { + sendErrorMessage(pluginData, msg.channel, "None of the cases were found!"); + return; + } + const failedAddendum = + failed.length > 0 + ? `\nThe following cases were not found: ${failed.toString().replace(new RegExp(",", "g"), ", ")}` + : ""; + + const amt = args.caseNum.length - failed.length; + sendSuccessMessage( + pluginData, + msg.channel, + `${amt} case${amt === 1 ? " is" : "s are"} no longer hidden!${failedAddendum}`, + ); }, });