Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 0 additions & 97 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"patch-package": "^6.4.7",
"prom-client": "^15.1.0",
"reflect-metadata": "0.1.13",
"run-my-sql-file": "^1.0.0",
"sentiment": "^5.0.2",
"sharp": "^0.30.7",
"sprintf-js": "^1.1.2",
Expand Down
82 changes: 72 additions & 10 deletions src/events/messageCreate.event.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import BotClient from "../struct/BotClient.js"
import BotGuildMember from "../struct/discord/BotGuildMember.js"
import BotGuild from "../struct/discord/BotGuild.js"
import Snippet from "../entities/Snippet.entity.js"
import languages from "../struct/client/iso6391.js"

import chalk = require("chalk")
import { noop } from "@buildtheearth/bot-utils"
import { hexToNum, noop } from "@buildtheearth/bot-utils"
import crypto from "crypto"
import { Message, MessageType } from "discord.js"
import fetch from "node-fetch"
import typeorm from "typeorm"
import ModerationMenu from "../entities/ModerationMenu.entity.js"
import { runBtCommand } from "../commands/team.command.js"
import ModerationMenu from "../entities/ModerationMenu.entity.js"
import Snippet from "../entities/Snippet.entity.js"
import BotClient from "../struct/BotClient.js"
import languages from "../struct/client/iso6391.js"
import BotGuild from "../struct/discord/BotGuild.js"
import BotGuildMember from "../struct/discord/BotGuildMember.js"
import chalk = require("chalk")

const ATTACHMENT_DUPLICATE_WINDOW = 30_000
const recentAttachmentHashes: Map<string, { time: number; messages: Message[] }> =
new Map()

function consumeCommand(client: BotClient, message: Message): string {
const content = message.content
Expand Down Expand Up @@ -50,6 +55,22 @@ function consumeTeam(client: BotClient, message: Message): string {
return commandSplit.join(" ").trim() || ""
}

function clearOldAttachmentHashes(now: number): void {
for (const [hash, { time }] of recentAttachmentHashes) {
if (now - time > ATTACHMENT_DUPLICATE_WINDOW) {
recentAttachmentHashes.delete(hash)
}
}
}

async function getAttachmentHash(url: string): Promise<string | null> {
const attachmentFetch = await fetch(url).catch(noop)
if (!attachmentFetch?.ok) return null
const arrayBuffer = await attachmentFetch.arrayBuffer().catch(() => null)
if (!arrayBuffer) return null
return crypto.createHash("sha256").update(new Uint8Array(arrayBuffer)).digest("hex")
}

export default async function (this: BotClient, message: Message): Promise<unknown> {
if (message.partial) await message.fetch().catch(noop)
if (message.author.bot) return
Expand Down Expand Up @@ -158,7 +179,48 @@ export default async function (this: BotClient, message: Message): Promise<unkno
return await message.channel.send("hay un server!")
if (message.content) {
const bannedWords = this.filter.findBannedWord(message.content)
if (bannedWords.length >= 1 && message.guild?.id === this.config.guilds.main)
if (bannedWords.length >= 1 && message.guild?.id === this.config.guilds.main) {
return ModerationMenu.createMenu(message, bannedWords, this)
}
}
if (main && message.attachments.size > 0) {
const attachmentCount = message.attachments.size

const messageHashes = new Set<string>()

for (const attachment of message.attachments.values()) {
const hash = await getAttachmentHash(attachment.url)
if (!hash) continue
if (messageHashes.has(hash)) continue

messageHashes.add(hash)
const now = Date.now()
clearOldAttachmentHashes(now)

const previous = recentAttachmentHashes.get(hash)
recentAttachmentHashes.set(hash, {
time: now,
messages: [...(previous?.messages || []), message]
})
if (previous && now - previous.time <= ATTACHMENT_DUPLICATE_WINDOW) {
for (const prevMessage of previous.messages) {
if (prevMessage.id === message.id) continue
await prevMessage.delete().catch(noop)
}

const bannedWords = this.filter.findBannedWord("SUS_ATTACHMENT")
if (bannedWords.length >= 1) {
await ModerationMenu.createMenu(message, bannedWords, this)
} else {
await message.delete().catch(noop)
await this.log({
color: hexToNum(this.config.colors.info),
author: { name: "Attachment Delete" },
description: `Message with ${attachmentCount} attachments by ${message.member} in ${message.channel} deleted. No moderation action found.`
})
break
}
}
}
}
}
Loading