From dc6755eb198fa521e253ffc16d6e9b06ecae8761 Mon Sep 17 00:00:00 2001 From: Lukas | AstroGD Date: Thu, 24 Nov 2022 17:27:56 +0100 Subject: [PATCH] censoring --- index.ts | 1 + package.json | 2 +- src/events/channelUpdate.ts | 85 +++++++++++++++++++++++++++++++++++++ src/events/index.ts | 1 + 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/events/channelUpdate.ts create mode 100644 src/events/index.ts diff --git a/index.ts b/index.ts index ce68056..1775a40 100644 --- a/index.ts +++ b/index.ts @@ -6,6 +6,7 @@ swapConsole(); import "./src/client/init"; import "./src/commands"; +import "./src/events"; import client from "./src/client"; function shutdown() { diff --git a/package.json b/package.json index 8c25db0..a6811c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eu.astrogd.white-leopard", - "version": "1.0.0-alpha.3", + "version": "1.0.0-alpha.4", "description": "A Discord bot that checks channel names for blacklisted words and reverts the changes if necessary", "main": "build/index.js", "scripts": { diff --git a/src/events/channelUpdate.ts b/src/events/channelUpdate.ts new file mode 100644 index 0000000..9d41d2b --- /dev/null +++ b/src/events/channelUpdate.ts @@ -0,0 +1,85 @@ +import client from "../client"; +import { Events } from "discord.js"; +import { getGuildSetting, isPremiumActive } from "../tools/data"; +import { Badword, database } from "../data"; +import { IsNull } from "typeorm"; +import getDefaultEmbed, { getFailedEmbed } from "../tools/defaultEmbeds"; +import { getGuildChannel } from "../tools/discord"; +import { Color, Emoji } from "../tools/design"; + +client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => { + if (oldChannel.isDMBased() || newChannel.isDMBased()) return; + const name = newChannel.name.toLowerCase(); + if (name === "censored") return; + + const guild = oldChannel.guild; + const settings = await getGuildSetting(guild.id); + const isPremium = isPremiumActive(settings.isPremiumUntil); + + const globalBlocklist = await database.getRepository(Badword).find({ + where: { + guildID: IsNull() + } + }); + const localBlocklist = await database.getRepository(Badword).find({ + where: { + guildID: guild.id + } + }); + + const blocklist = [...globalBlocklist, ...localBlocklist]; + let found: string | null = null; + + for (let i = 0; i < blocklist.length; i++) { + const word = blocklist[i]; + if (!word) continue; + + if (!name.includes(word.value)) continue; + found = word.value; + break; + } + + if (found === null) return; + + const logChannel = settings.notificationChannelID ? await getGuildChannel(guild.id, settings.notificationChannelID) : null; + + try { + await newChannel.setName("CENSORED", `[Automated] Detected blocked word in channel name. Name has been censored`); + } catch (error) { + if (!logChannel || !logChannel.isTextBased()) return; + const embed = getFailedEmbed(); + embed.setDescription(`Couldn't censor <#${newChannel.id}> (${newChannel.id}): ${error instanceof Error ? error.message : error}`); + if (isPremium) embed.addFields({ + name: "Detected banned word:", + value: `||${found}||` + },{ + name: "Old channel name:", + value: `||${name}||`, + inline: true + }); + + logChannel.send({ + embeds: [embed] + }).catch(); + return; + } + + if (!logChannel || !logChannel.isTextBased()) return; + const embed = getDefaultEmbed(); + embed.setTitle(`${Emoji.SECURITY_CHALLENGE_FAILED} Blocked word detected`); + embed.setDescription(`<#${newChannel.id}> (${newChannel.id}) has been renamed because its name contained a blocked word.`); + embed.setColor(Color.WARNING_YELLOW); + if (isPremium) embed.addFields({ + name: "Detected banned word:", + value: `||${found}||`, + inline: true + },{ + name: "Old channel name:", + value: `||${name}||`, + inline: true + }); + + logChannel.send({ + embeds: [embed] + }).catch(); +}); \ No newline at end of file diff --git a/src/events/index.ts b/src/events/index.ts new file mode 100644 index 0000000..f427567 --- /dev/null +++ b/src/events/index.ts @@ -0,0 +1 @@ +import "./channelUpdate"; \ No newline at end of file