Skip to content
Merged
Changes from 1 commit
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
69 changes: 60 additions & 9 deletions src/events/messageCreate.event.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import BotClient from "../struct/BotClient.js"
import BotGuildMember from "../struct/discord/BotGuildMember.js"
import BotGuild from "../struct/discord/BotGuild.js"
import { hexToNum, noop } from "@buildtheearth/bot-utils"
import crypto from "crypto"
import { Message, MessageType, TextChannel } from "discord.js"
import typeorm from "typeorm"
Comment thread
Nudelsuppe42 marked this conversation as resolved.
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")
import { noop } from "@buildtheearth/bot-utils"
import { Message, MessageType } from "discord.js"
import typeorm from "typeorm"
import ModerationMenu from "../entities/ModerationMenu.entity.js"
import { runBtCommand } from "../commands/team.command.js"

const ATTACHMENT_DUPLICATE_WINDOW = 30_000
const recentAttachmentHashes: Map<string, number> = new Map()

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

function clearOldAttachmentHashes(now: number): void {
for (const [hash, timestamp] of recentAttachmentHashes) {
if (now - timestamp > 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 +178,38 @@ 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

if (attachmentCount == 2 || attachmentCount == 4) {
message.attachments.forEach(attachment => {
getAttachmentHash(attachment.url).then(async hash => {
if (!hash) return
const now = Date.now()
clearOldAttachmentHashes(now)

const previous = recentAttachmentHashes.get(hash)
recentAttachmentHashes.set(hash, now)
if (previous && now - previous <= ATTACHMENT_DUPLICATE_WINDOW) {
message.delete().catch(noop)
const bannedWords = this.filter.findBannedWord("SUS_ATTACHMENT")

if (bannedWords.length >= 1) {
return ModerationMenu.createMenu(message, bannedWords, this)
} else {
Comment thread
Nudelsuppe42 marked this conversation as resolved.
Outdated
await client.log({
color: hexToNum(client.config.colors.info),
author: { name: "Attachment Delete" },
description: `Message with ${attachmentCount} attachments by ${message.member} in ${message.channel} deleted. No moderation action found.`
})
}
}
})
})
Comment thread
Nudelsuppe42 marked this conversation as resolved.
Outdated
}
}
}
Loading