Allow hidecase, unhidecase and deletecase to take multiple case numbers

This commit is contained in:
Dark 2020-10-12 14:48:54 +02:00
parent dda4313b26
commit 52ee007770
3 changed files with 116 additions and 52 deletions

View file

@ -19,18 +19,32 @@ export const DeleteCaseCmd = modActionsCmd({
`), `),
signature: { signature: {
caseNumber: ct.number(), caseNumber: ct.number({ rest: true }),
force: ct.switchOption({ shortcut: "f" }), force: ct.switchOption({ shortcut: "f" }),
}, },
async run({ pluginData, message, args }) { async run({ pluginData, message, args }) {
const theCase = await pluginData.state.cases.findByCaseNumber(args.caseNumber); const failed = [];
const validCases = [];
let cancelled = 0;
for (const num of args.caseNumber) {
const theCase = await pluginData.state.cases.findByCaseNumber(num);
if (!theCase) { if (!theCase) {
sendErrorMessage(pluginData, message.channel, "Case not found"); failed.push(num);
continue;
}
validCases.push(theCase);
}
if (failed.length === args.caseNumber.length) {
sendErrorMessage(pluginData, message.channel, "None of the cases were found!");
return; return;
} }
for (const theCase of validCases) {
if (!args.force) { if (!args.force) {
const cases = pluginData.getPlugin(CasesPlugin); const cases = pluginData.getPlugin(CasesPlugin);
const embedContent = await cases.getCaseEmbed(theCase); const embedContent = await cases.getCaseEmbed(theCase);
@ -48,7 +62,8 @@ export const DeleteCaseCmd = modActionsCmd({
const normalizedReply = (reply?.content || "").toLowerCase().trim(); const normalizedReply = (reply?.content || "").toLowerCase().trim();
if (normalizedReply !== "yes" && normalizedReply !== "y") { if (normalizedReply !== "yes" && normalizedReply !== "y") {
message.channel.createMessage("Cancelled. Case was not deleted."); message.channel.createMessage("Cancelled. Case was not deleted.");
return; cancelled++;
continue;
} }
} }
@ -69,7 +84,22 @@ export const DeleteCaseCmd = modActionsCmd({
mod: stripObjectToScalars(message.member, ["user", "roles"]), mod: stripObjectToScalars(message.member, ["user", "roles"]),
case: stripObjectToScalars(theCase), case: stripObjectToScalars(theCase),
}); });
}
sendSuccessMessage(pluginData, message.channel, `Case #${theCase.case_number} deleted!`); 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;
}
sendSuccessMessage(
pluginData,
message.channel,
`${amt} case${amt === 1 ? " was" : "s were"} deleted!${failedAddendum}`,
);
}, },
}); });

View file

@ -9,22 +9,37 @@ export const HideCaseCmd = modActionsCmd({
signature: [ signature: [
{ {
caseNum: ct.number(), caseNum: ct.number({ rest: true }),
}, },
], ],
async run({ pluginData, message: msg, args }) { async run({ pluginData, message: msg, args }) {
const theCase = await pluginData.state.cases.findByCaseNumber(args.caseNum); const failed = [];
for (const num of args.caseNum) {
const theCase = await pluginData.state.cases.findByCaseNumber(num);
if (!theCase) { if (!theCase) {
sendErrorMessage(pluginData, msg.channel, "Case not found!"); failed.push(num);
return; 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( sendSuccessMessage(
pluginData, pluginData,
msg.channel, 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}`,
); );
}, },
}); });

View file

@ -9,18 +9,37 @@ export const UnhideCaseCmd = modActionsCmd({
signature: [ signature: [
{ {
caseNum: ct.number(), caseNum: ct.number({ rest: true }),
}, },
], ],
async run({ pluginData, message: msg, args }) { async run({ pluginData, message: msg, args }) {
const theCase = await pluginData.state.cases.findByCaseNumber(args.caseNum); const failed = [];
for (const num of args.caseNum) {
const theCase = await pluginData.state.cases.findByCaseNumber(num);
if (!theCase) { if (!theCase) {
sendErrorMessage(pluginData, msg.channel, "Case not found!"); failed.push(num);
return; 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}`,
);
}, },
}); });