import client from "../client"; import { Events } from "discord.js"; import { getGuildSetting } 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(); const guild = oldChannel.guild; const settings = await getGuildSetting(guild.id); 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 { if (!newChannel.deletable) throw new Error("Missing permissions to delete channel"); await newChannel.delete("[Automated] Detected blocked word in channel name"); } catch (error) { if (!logChannel || !logChannel.isTextBased()) return; const embed = getFailedEmbed(); embed.setDescription(`Couldn't delete <#${newChannel.id}> (${newChannel.id}): ${error instanceof Error ? error.message : error}`); embed.addFields({ name: "Detected banned word:", value: `||${found}||` }); 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.name}|| (${newChannel.id}) has been deleted because its name contained a blocked word.`); embed.setColor(Color.WARNING_YELLOW); embed.addFields({ name: "Detected banned word:", value: `||${found}||`, inline: true }); logChannel.send({ embeds: [embed] }).catch(() => {}); });