all repos — stealth-developers @ 94d6817ede30336125cbd29cdb3234467fe65b54

src/events/messageCreate.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
 216
 217
 218
 219
 220
import config from "@/config";
import { GuildModel, UserModel } from "@/database/schemas";
import lily from "@/utils/logging";
import vision from "@google-cloud/vision";
import {
	ActionRowBuilder,
	type Attachment,
	ButtonBuilder,
	ButtonStyle,
	ChannelType,
	type Client,
	Events,
	type Message,
} from "discord.js";

type ImageResult = {
	image: Attachment;
	isCat: boolean;
};

const MS_IN_DAY = 1000 * 60 * 60 * 24;
const REPORT_COOLDOWN_DAYS = 28;
const REPORT_COOLDOWN = MS_IN_DAY * REPORT_COOLDOWN_DAYS;

const logger = lily.child(["google-cloud", "vision"]);

type ImageResults = Record<string, ImageResult>;
const client = new vision.ImageAnnotatorClient({
	projectId: config.data.googleCloud?.projectId,
	credentials: {
		client_email: config.data.googleCloud?.credentials.clientEmail,
		private_key: config.data.googleCloud?.credentials.privateKey,
	},
});

async function processImage(
	message: Message,
): Promise<ImageResults | undefined> {
	if (!config.data.googleCloud) {
		logger.warn("gcp vision api not configured");
		return;
	}

	const imageAttachments = message.attachments.filter((attachment) =>
		attachment.contentType?.startsWith("image/"),
	);

	if (imageAttachments.size === 0) return;

	const results: ImageResults = {};
	for (const [id, attachment] of imageAttachments) {
		try {
			const response = await fetch(attachment.url);
			if (!response.ok) {
				logger.error(`failed to download image ${attachment.url}`);
				results[id] = { image: attachment, isCat: false };
				continue;
			}

			const buffer = await response.arrayBuffer();

			if (client.objectLocalization === undefined) {
				logger.warn(
					"objectLocalization method not available in Google Cloud Vision client",
				);
				return;
			}

			const [result] = await client.objectLocalization({
				image: { content: Buffer.from(buffer) },
			});

			const objects = result.localizedObjectAnnotations || [];
			const hasCat = objects.some(
				(obj) =>
					obj.name?.toLowerCase().includes("cat") && (obj.score || 0) > 0.5,
			);

			results[id] = { image: attachment, isCat: hasCat };
			logger.debug(`image ${attachment.name}: cat detected = ${hasCat}`);
		} catch (error) {
			logger.error(`error processing image ${attachment.name}:`, error);
			results[id] = { image: attachment, isCat: true };
		}
	}

	return results;
}

async function handleReportChannel(message: Message) {
	if (!message.guild) return;
	const guildData = await GuildModel.findOne({ guild_id: message.guild.id });
	if (
		!guildData?.report_channel ||
		message.channel.id !== guildData.report_channel
	) {
		return;
	}

	let user = await UserModel.findOne({
		user_id: message.author.id,
		guild_id: message.guild.id,
	});

	if (!user) {
		user = new UserModel({
			user_id: message.author.id,
			guild_id: message.guild.id,
		});
	} else {
		const lastReport = user.last_report;
		if (lastReport) {
			const timeSinceLastReport = Date.now() - lastReport.getTime();
			if (timeSinceLastReport < REPORT_COOLDOWN) return;
		}
	}

	user.last_report = new Date();
	await user.save();

	if (guildData.report_message) {
		const agreeButton = new ActionRowBuilder<ButtonBuilder>().addComponents(
			new ButtonBuilder()
				.setCustomId("report:agree")
				.setLabel("I confirm that my report follows the rules")
				.setStyle(ButtonStyle.Primary),
		);

		await message.reply({
			content: guildData.report_message.replace(
				"{user}",
				`<@${message.author.id}>`,
			),
			allowedMentions: { users: [message.author.id] },
			components: [agreeButton],
		});
	}
}

export default {
	event: Events.MessageCreate,
	async execute(client: Client, message: Message) {
		if (message.author.bot) return;

		if (message.content.includes("grok is this true")) {
			if (message.channel.isSendable()) await message.channel.send("yeh");
		}

		await handleReportChannel(message);

		if (
			!message.attachments.some((attachment) =>
				attachment.contentType?.startsWith("image/"),
			) ||
			!config.data.catChannel?.channelId
		)
			return;

		if (message.channel.id !== config.data.catChannel.channelId) return;
		const channel = client.channels.cache.get(config.data.catChannel.channelId);
		if (
			!channel ||
			!channel.isTextBased() ||
			channel.type !== ChannelType.GuildText
		)
			return;

		if (!config.data.googleCloud) {
			await message.react("😻");
			await awardCatPoints(
				message.author.id,
				message.guildId,
				message.attachments.size,
			);
			return;
		}

		const results = await processImage(message);
		if (!results) return;

		const allResults = Object.values(results);
		const catImages = allResults.filter((r) => r.isCat);
		const nonCatImages = allResults.filter((r) => !r.isCat);

		if (nonCatImages.length > 0) {
			const replyContent = [
				"this channel is only for cat images!",
				allResults.length > 1
					? `i found ${nonCatImages.length} non-cat images:`
					: "",
			].join(" ");
			await message.reply({
				content: replyContent,
			});
		} else if (catImages.length > 0) {
			await message.react("😻");
			await awardCatPoints(
				message.author.id,
				message.guildId,
				message.attachments.size,
			);
		}
	},
};

async function awardCatPoints(
	userId: string,
	guildId: string | null,
	points: number,
) {
	if (!guildId) return;
	const user = await UserModel.findOneAndUpdate(
		{ user_id: userId, guild_id: guildId },
		{ $inc: { cat_points: points } },
		{ upsert: true, new: true },
	);
	logger.info(
		`awarded ${points} cat points to user ${userId}, total: ${user.cat_points}`,
	);
}