automod: tweaks/fixes to spam detection
This commit is contained in:
parent
4931c95872
commit
80fb9d7b6b
7 changed files with 18 additions and 11 deletions
|
@ -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(),
|
||||
});
|
||||
|
|
|
@ -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));
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -36,7 +36,7 @@ export const MemberJoinSpamTrigger = automodTrigger<unknown>()({
|
|||
type: RecentActionType.MemberJoin,
|
||||
timestamp: Date.now(),
|
||||
archiveId: null,
|
||||
userIds: [],
|
||||
identifiers: [],
|
||||
});
|
||||
|
||||
return {
|
||||
|
|
|
@ -97,6 +97,6 @@ export interface RecentAction {
|
|||
export interface RecentSpam {
|
||||
archiveId: string;
|
||||
type: RecentActionType;
|
||||
userIds: string[];
|
||||
identifiers: string[];
|
||||
timestamp: number;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue