all repos — stealth-developers @ 9aac44c48a95c933580c3cd29f769740e6505332

src/automod/index.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
import { rest } from "@/discord";
import { hasManagerPermissions } from "@/utils/discord/permissions";
import {
	type Client,
	type Message,
	type RESTGetAPIGuildMessagesSearchQuery,
	type RESTGetAPIGuildMessagesSearchResult,
	Routes,
	type Snowflake,
} from "discord.js";

import { loggers } from "@/utils/logging";
const logger = loggers.automod;

type Pending = {
	channels: Set<Snowflake>;
	timer: NodeJS.Timeout;
};

const pendingReports = new Map<Snowflake, Pending>();
const cooldowns = new Map<Snowflake, NodeJS.Timeout>();

async function getLogChannel(client: Client) {
	return client.channels.fetch("1478940790112915486");
}

type AutomodResult = { delete: true } | null;
type AutomodSignals = {
	messageCount?: number;
	lastMessage?: Message;
};
type AutomodFunction = (
	client: Client,
	message: Message,
) => Promise<AutomodResult>;

async function fetchSignals(
	client: Client,
	message: Message,
): Promise<AutomodSignals> {
	const signals: AutomodSignals = {
		messageCount: undefined,
	};

	const messages = (await rest.get(
		`/guilds/${message.guildId}/messages/search`,
		{
			body: {
				author_id: [message.author.id],
			} as RESTGetAPIGuildMessagesSearchQuery,
		},
	)) as RESTGetAPIGuildMessagesSearchResult;

	if ("total_results" in messages) {
		signals.messageCount = messages.total_results;
		const [lastApiMessage] = messages.messages[1];

		const channel = await client.channels.fetch(lastApiMessage.channel_id);
		if (channel?.isTextBased()) {
			const lastMessage = await channel.messages
				.fetch(lastApiMessage.id)
				.catch(() => null);
			if (lastMessage) {
				signals.lastMessage = lastMessage;
			}
		}
	}

	return signals;
}

export async function handleMessage(client: Client, message: Message) {
	if (message.author.bot || !message.guildId || !message.member) return;
	const automodFunctions: AutomodFunction[] = [handleSpamMarker];

	if (await hasManagerPermissions(message.member)) return null;
	if (message.channel.isThread()) return null;

	for (const func of automodFunctions) {
		const result = await func(client, message);

		if (result?.delete) {
			await message.delete().catch(() => null);
			break;
		}
	}
}

async function handleSpamMarker(
	client: Client,
	message: Message,
): Promise<AutomodResult> {
	const { content } = message;

	/*
  	a message will contain either no markers, a link marker or a word maker,
  	likely not two at once.

  	if a messsage contains a link marker, then we delete it.
  	if a message contains a word marker, we first check if the last message
       is older than an hour, if it is, we delete the message.
	*/
	const linkMarkers = [
		"discord.gg/",
		"discordapp.com/invite/",
		"discord.com/invite/",
	];
	const wordMarkers = ["check my bio"];

	const hasLinkMarker = linkMarkers.some((marker) =>
		content.toLowerCase().includes(marker),
	);
	const hasWordMarker = wordMarkers.some((marker) =>
		content.toLowerCase().includes(marker),
	);

	if (!hasLinkMarker && !hasWordMarker) return null;
	const signals = await fetchSignals(client, message);
	const ONE_HOUR = 60 * 60 * 1000;

	logger.debug(
		{
			signals,
			message,
			markers: {
				hasLinkMarker,
				hasWordMarker,
			},
		},
		"evaluating message for potential phishing markers",
	);

	if (
		hasWordMarker &&
		signals.lastMessage &&
		Date.now() - signals.lastMessage.createdTimestamp < ONE_HOUR
	) {
		logger.debug(
			{
				messageId: message.id,
				authorId: message.author.id,
				lastMessageId: signals.lastMessage.id,
				lastMessageAge: Date.now() - signals.lastMessage.createdTimestamp,
			},
			"skipping deletion due to recent message",
		);
		return null;
	}

	if (cooldowns.has(message.author.id)) {
		return { delete: true };
	}

	const existing = pendingReports.get(message.author.id);
	if (!existing) {
		const content = [
			`<@${message.author.id}>, your message contained something indicative of a compromised account or phishing attempt, so we removed it.`,
			"Keep in mind, we don't allow advertising other servers here.",
		];
		await message.reply(content.join(" "));

		const logChannel = await getLogChannel(client);
		if (logChannel?.isSendable()) {
			await message.forward(logChannel);
			logChannel.send(
				`Deleted a message from <@${message.author.id}> in <#${message.channelId}> containing a potential phishing marker.`,
			);
		}

		const timer = setTimeout(
			() => flushReport(client, message.author.id),
			30_000,
		);
		pendingReports.set(message.author.id, {
			channels: new Set([message.channelId]),
			timer,
		});

		return { delete: true };
	}

	existing.channels.add(message.channelId);

	clearTimeout(existing.timer);
	existing.timer = setTimeout(
		() => flushReport(client, message.author.id),
		30_000,
	);

	return { delete: true };
}

async function flushReport(client: Client, authorId: Snowflake) {
	const pending = pendingReports.get(authorId);
	if (!pending) return;

	pendingReports.delete(authorId);
	clearTimeout(pending.timer);

	const logChannel = await getLogChannel(client);
	if (!logChannel?.isSendable()) return;

	const channels = Array.from(pending.channels);
	const channelMentions = channels.map((c) => `<#${c}>`).join(", ");

	logChannel.send(
		`<@${authorId}> posted message in channels: ${channelMentions}`,
	);

	const cooldownTimer = setTimeout(
		() => cooldowns.delete(authorId),
		5 * 60 * 1000,
	);
	cooldowns.set(authorId, cooldownTimer);
}