From b64335c901dcd66ef4421fb580eb780dc6e4aaa5 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Thu, 4 Jun 2020 03:20:31 +0300 Subject: [PATCH] automod: clear old message spam data when re-evaluating an edited message --- backend/src/plugins/Automod/Automod.ts | 42 +++++++++++++++++++------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/backend/src/plugins/Automod/Automod.ts b/backend/src/plugins/Automod/Automod.ts index 780dab9f..cb777e72 100644 --- a/backend/src/plugins/Automod/Automod.ts +++ b/backend/src/plugins/Automod/Automod.ts @@ -232,6 +232,7 @@ export class AutomodPlugin extends ZeppelinPlugin this.onMessageCreate(msg); + this.onMessageCreateFn = msg => this.runAutomodOnMessage(msg, false); this.savedMessages.events.on("create", this.onMessageCreateFn); - this.savedMessages.events.on("update", this.onMessageCreateFn); + + this.onMessageUpdateFn = msg => this.runAutomodOnMessage(msg, true); + this.savedMessages.events.on("update", this.onMessageUpdateFn); + this.actionedMessageIds = []; } @@ -368,7 +372,7 @@ export class AutomodPlugin extends ZeppelinPlugin { const since = moment.utc(msg.posted_at).valueOf() - convertDelayStringToMS(trigger.within); + const to = moment.utc(msg.posted_at).valueOf(); const identifier = trigger.per_channel ? `${msg.channel_id}-${msg.user_id}` : msg.user_id; - const recentActions = this.getMatchingRecentActions(recentActionType, identifier, since); + const recentActions = this.getMatchingRecentActions(recentActionType, identifier, since, to); const totalCount = recentActions.reduce((total, action) => { return total + action.count; }, 0); @@ -591,7 +596,8 @@ export class AutomodPlugin extends ZeppelinPlugin { const since = moment.utc().valueOf() - convertDelayStringToMS(trigger.within); - const recentActions = this.getMatchingRecentActions(recentActionType, identifier, since) as OtherRecentAction[]; + const to = moment.utc().valueOf(); + const recentActions = this.getMatchingRecentActions(recentActionType, identifier, since, to) as OtherRecentAction[]; const totalCount = recentActions.reduce((total, action) => { return total + action.count; }, 0); @@ -840,7 +846,7 @@ export class AutomodPlugin extends ZeppelinPlugin { - return action.type === type && (!identifier || action.identifier === identifier) && action.timestamp >= since; + return ( + action.type === type && + (!identifier || action.identifier === identifier) && + action.timestamp >= since && + action.timestamp <= to + ); + }); + } + + protected clearRecentActionsForMessage(messageId: string) { + this.recentActions = this.recentActions.filter(info => { + return !((info as TextRecentAction).messageInfo?.messageId === messageId); }); } @@ -1526,13 +1543,16 @@ export class AutomodPlugin extends ZeppelinPlugin { if (this.unloaded) return; - await this.logRecentActionsForMessage(msg); + if (isEdit) { + this.clearRecentActionsForMessage(msg.id); + } + this.logRecentActionsForMessage(msg); const member = this.guild.members.get(msg.user_id); const config = this.getMatchingConfig({