automod: tweaks/fixes to spam detection

This commit is contained in:
Dragory 2020-07-30 22:47:33 +03:00
parent 4931c95872
commit 80fb9d7b6b
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
7 changed files with 18 additions and 11 deletions

View file

@ -6,6 +6,7 @@ import { humanizeDurationShort } from "../../../humanizeDurationShort";
import { findRecentSpam } from "./findRecentSpam";
import { getMatchingMessageRecentActions } from "./getMatchingMessageRecentActions";
import * as t from "io-ts";
import { getMessageSpamIdentifier } from "./getSpamIdentifier";
const MessageSpamTriggerConfig = t.type({
amount: t.number,
@ -28,7 +29,9 @@ export function createMessageSpamTrigger(spamType: RecentActionType, prettyName:
return;
}
const recentSpam = findRecentSpam(pluginData, spamType, context.message.user_id);
const spamIdentifier = getMessageSpamIdentifier(context.message, triggerConfig.per_channel);
const recentSpam = findRecentSpam(pluginData, spamType, spamIdentifier);
if (recentSpam) {
await pluginData.state.archives.addSavedMessagesToArchive(
recentSpam.archiveId,
@ -46,9 +49,9 @@ export function createMessageSpamTrigger(spamType: RecentActionType, prettyName:
pluginData,
context.message,
spamType,
spamIdentifier,
triggerConfig.amount,
within,
triggerConfig.per_channel,
);
if (matchedSpam) {
@ -61,7 +64,7 @@ export function createMessageSpamTrigger(spamType: RecentActionType, prettyName:
pluginData.state.recentSpam.push({
type: spamType,
userIds: [context.message.user_id],
identifiers: [spamIdentifier],
archiveId,
timestamp: Date.now(),
});

View file

@ -2,8 +2,8 @@ import { PluginData } from "knub";
import { AutomodPluginType } from "../types";
import { RecentActionType } from "../constants";
export function findRecentSpam(pluginData: PluginData<AutomodPluginType>, type: RecentActionType, userId?: string) {
export function findRecentSpam(pluginData: PluginData<AutomodPluginType>, type: RecentActionType, identifier?: string) {
return pluginData.state.recentSpam.find(spam => {
return spam.type === type && (!userId || spam.userIds.includes(userId));
return spam.type === type && (!identifier || spam.identifiers.includes(identifier));
});
}

View file

@ -9,19 +9,17 @@ export function getMatchingMessageRecentActions(
pluginData: PluginData<AutomodPluginType>,
message: SavedMessage,
type: RecentActionType,
identifier: string,
count: number,
within: number,
perChannel: boolean,
) {
const since = moment.utc(message.posted_at).valueOf() - within;
const to = moment.utc(message.posted_at).valueOf();
const identifier = perChannel ? `${message.channel_id}-${message.user_id}` : message.user_id;
const recentActions = getMatchingRecentActions(pluginData, type, identifier, since, to);
const totalCount = recentActions.reduce((total, action) => total + action.count, 0);
if (totalCount >= count) {
return {
identifier,
recentActions,
};
}

View file

@ -16,7 +16,8 @@ export function getMatchingRecentActions(
action.type === type &&
(!identifier || action.identifier === identifier) &&
action.context.timestamp >= since &&
action.context.timestamp <= to
action.context.timestamp <= to &&
!action.context.actioned
);
});
}

View file

@ -0,0 +1,5 @@
import { SavedMessage } from "../../../data/entities/SavedMessage";
export function getMessageSpamIdentifier(message: SavedMessage, perChannel: boolean) {
return perChannel ? `${message.channel_id}-${message.user_id}` : message.user_id;
}

View file

@ -36,7 +36,7 @@ export const MemberJoinSpamTrigger = automodTrigger<unknown>()({
type: RecentActionType.MemberJoin,
timestamp: Date.now(),
archiveId: null,
userIds: [],
identifiers: [],
});
return {

View file

@ -97,6 +97,6 @@ export interface RecentAction {
export interface RecentSpam {
archiveId: string;
type: RecentActionType;
userIds: string[];
identifiers: string[];
timestamp: number;
}