eu.astrogd.white-leopard/src/events/channelUpdate.ts

85 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-11-24 17:27:56 +01:00
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(() => {});
2022-11-24 17:27:56 +01:00
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(() => {});
2022-11-24 17:27:56 +01:00
});