Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automod: Spam detection #653

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
9 changes: 8 additions & 1 deletion modules/automod/automod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ESTABLISHED_THRESHOLD, getLevelForXp } from "../xp/misc.js";
import { xpDatabase } from "../xp/util.js";
import tryCensor, { badWordRegexps, badWordsAllowed } from "./misc.js";
import { ignoredDeletions } from "../logging/messages.js";
import { handleMessage } from "./spam.js";

const { threads } = (await config.channels.servers?.threads.fetchActive()) ?? {};
const whitelistedInvites = await Promise.all(
Expand Down Expand Up @@ -61,10 +62,16 @@ const BLACKLISTED_DOMAINS = [

export default async function automodMessage(message: Message): Promise<boolean> {
const baseChannel = getBaseChannel(message.channel);

let needsDelete = false;
const deletionMessages: string[] = [];

const spam = handleMessage(message.author, message.content);
if (spam) {
await warn(message.author, "Spamming", 1, message.content);
needsDelete = true;
deletionMessages.push("Please don’t spam!");
}

const animatedEmojis =
baseChannel?.id !== config.channels.bots?.id && message.content.match(GlobalAnimatedEmoji);
const badAnimatedEmojis =
Expand Down
53 changes: 53 additions & 0 deletions modules/automod/spam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { User } from "discord.js";
import { xpDatabase } from "../xp/util.js";
import { getLevelForXp } from "../xp/misc.js";

const timeWindow = 20;

type Message = {
message: string;
timestamp: number;
};
const userMessages: Record<string, Message[]> = {};
function logMessage(userId: string, message: string): void {
const timestamp: number = Math.floor(Date.now() / 1000);
if (!userMessages[userId]) {
userMessages[userId] = [];
}

userMessages[userId]?.push({ message, timestamp });

userMessages[userId] =
userMessages[userId]?.filter((m) => timestamp - m.timestamp <= timeWindow) ?? [];
}

export function isSpam(user: User, message: string): boolean {
const xp = xpDatabase.data.find((entry) => entry.user === user.id)?.xp ?? 0;
const level = getLevelForXp(xp);
const levelFactor = Math.min(10 * Math.log10(1 + level), 15);

const x = Math.max(2, Math.round((5 + levelFactor) * (1 - message.length / 2000)));

const timestamp: number = Math.floor(Date.now() / 1000);
console.log(message.length, x, message.length / 400);
if ((userMessages[user.id]?.length || 0) < x) {
return false;
}

const recentMessages = userMessages[user.id]?.slice(-x);

return (
recentMessages?.every(
(m) => m.message === message && timestamp - m.timestamp <= timeWindow,
) || false
);
}

export function handleMessage(user: User, message: string): boolean {
if (isSpam(user, message)) {
return true;
} else {
logMessage(user.id, message);
return false;
}
}
Loading