From 0ee942728c6a353c5f9dff63c09e31bda6e43716 Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Tue, 28 Jan 2020 23:17:09 +0100 Subject: [PATCH] Updated Warn Threshold to work with latest release --- backend/src/plugins/Cases.ts | 15 ++++++++++ backend/src/plugins/ModActions.ts | 49 ++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/backend/src/plugins/Cases.ts b/backend/src/plugins/Cases.ts index ac5da024..bc44d22c 100644 --- a/backend/src/plugins/Cases.ts +++ b/backend/src/plugins/Cases.ts @@ -244,6 +244,21 @@ export class CasesPlugin extends ZeppelinPlugin { return { embed }; } + public async getCaseTypeAmountForUserId(userID: string, type: CaseTypes): Promise { + const cases = (await this.cases.getByUserId(userID)).filter(c => !c.is_hidden); + let typeAmount = 0; + + if (cases.length > 0) { + cases.forEach(singleCase => { + if (singleCase.type === type.valueOf()) { + typeAmount++; + } + }); + } + + return typeAmount; + } + /** * A helper for posting to the case log channel. * Returns silently if the case log channel isn't specified or is invalid. diff --git a/backend/src/plugins/ModActions.ts b/backend/src/plugins/ModActions.ts index 74042b9f..18226b33 100644 --- a/backend/src/plugins/ModActions.ts +++ b/backend/src/plugins/ModActions.ts @@ -46,6 +46,8 @@ const ConfigSchema = t.type({ ban_message: tNullable(t.string), alert_on_rejoin: t.boolean, alert_channel: tNullable(t.string), + warn_notify_threshold: t.number, + warn_notify_message: t.string, can_note: t.boolean, can_warn: t.boolean, can_mute: t.boolean, @@ -146,6 +148,9 @@ export class ModActionsPlugin extends ZeppelinPlugin { ban_message: "You have been banned from the {guildName} server. Reason given: {reason}", alert_on_rejoin: false, alert_channel: null, + warn_notify_threshold: 1, + warn_notify_message: + "The user already has **{priorWarnings}** warnings!\n Please check their prior cases and assess whether or not to warn anyways.\n Proceed with the warning?", can_note: false, can_warn: false, @@ -197,10 +202,7 @@ export class ModActionsPlugin extends ZeppelinPlugin { } clearIgnoredEvent(type: IgnoredEventType, userId: any) { - this.ignoredEvents.splice( - this.ignoredEvents.findIndex(info => type === info.type && userId === info.userId), - 1, - ); + this.ignoredEvents.splice(this.ignoredEvents.findIndex(info => type === info.type && userId === info.userId), 1); } formatReasonWithAttachments(reason: string, attachments: Attachment[]) { @@ -328,7 +330,9 @@ export class ModActionsPlugin extends ZeppelinPlugin { if (actions.length) { const alertChannel: any = this.guild.channels.get(alertChannelId); alertChannel.send( - `<@!${member.id}> (${member.user.username}#${member.user.discriminator} \`${member.id}\`) joined with ${actions.length} prior record(s)`, + `<@!${member.id}> (${member.user.username}#${member.user.discriminator} \`${member.id}\`) joined with ${ + actions.length + } prior record(s)`, ); } } @@ -349,7 +353,9 @@ export class ModActionsPlugin extends ZeppelinPlugin { const existingCaseForThisEntry = await this.cases.findByAuditLogId(kickAuditLogEntry.id); if (existingCaseForThisEntry) { logger.warn( - `Tried to create duplicate case for audit log entry ${kickAuditLogEntry.id}, existing case id ${existingCaseForThisEntry.id}`, + `Tried to create duplicate case for audit log entry ${kickAuditLogEntry.id}, existing case id ${ + existingCaseForThisEntry.id + }`, ); } else { const casesPlugin = this.getPlugin("cases"); @@ -605,6 +611,21 @@ export class ModActionsPlugin extends ZeppelinPlugin { const config = this.getConfig(); const reason = this.formatReasonWithAttachments(args.reason, msg.attachments); + const casesPlugin = this.getPlugin("cases"); + const priorWarnAmount = await casesPlugin.getCaseTypeAmountForUserId(memberToWarn.id, CaseTypes.Warn); + if (priorWarnAmount >= config.warn_notify_threshold) { + const tooManyWarningsMsg = await msg.channel.createMessage( + config.warn_notify_message.replace("{priorWarnings}", `${priorWarnAmount}`), + ); + + const reply = await waitForReaction(this.bot, tooManyWarningsMsg, ["✅", "❌"]); + tooManyWarningsMsg.delete(); + if (!reply || reply.name === "❌") { + msg.channel.createMessage(errorMessage("Warn cancelled by moderator")); + return; + } + } + const warnMessage = config.warn_message.replace("{guildName}", this.guild.name).replace("{reason}", reason); const warnResult = await this.warnMember( memberToWarn, @@ -626,7 +647,9 @@ export class ModActionsPlugin extends ZeppelinPlugin { this.sendSuccessMessage( msg.channel, - `Warned **${memberToWarn.user.username}#${memberToWarn.user.discriminator}** (Case #${warnResult.case.case_number})${messageResultText}`, + `Warned **${memberToWarn.user.username}#${memberToWarn.user.discriminator}** (Case #${ + warnResult.case.case_number + })${messageResultText}`, ); } @@ -1013,7 +1036,9 @@ export class ModActionsPlugin extends ZeppelinPlugin { } // Confirm the action to the moderator - let response = `Kicked **${memberToKick.user.username}#${memberToKick.user.discriminator}** (Case #${kickResult.case.case_number})`; + let response = `Kicked **${memberToKick.user.username}#${memberToKick.user.discriminator}** (Case #${ + kickResult.case.case_number + })`; if (kickResult.notifyResult.text) response += ` (${kickResult.notifyResult.text})`; this.sendSuccessMessage(msg.channel, response); @@ -1074,7 +1099,9 @@ export class ModActionsPlugin extends ZeppelinPlugin { } // Confirm the action to the moderator - let response = `Banned **${memberToBan.user.username}#${memberToBan.user.discriminator}** (Case #${banResult.case.case_number})`; + let response = `Banned **${memberToBan.user.username}#${memberToBan.user.discriminator}** (Case #${ + banResult.case.case_number + })`; if (banResult.notifyResult.text) response += ` (${banResult.notifyResult.text})`; this.sendSuccessMessage(msg.channel, response); @@ -1159,7 +1186,9 @@ export class ModActionsPlugin extends ZeppelinPlugin { // Confirm the action to the moderator this.sendSuccessMessage( msg.channel, - `Softbanned **${memberToSoftban.user.username}#${memberToSoftban.user.discriminator}** (Case #${createdCase.case_number})`, + `Softbanned **${memberToSoftban.user.username}#${memberToSoftban.user.discriminator}** (Case #${ + createdCase.case_number + })`, ); // Log the action