refactor(events/channel): now using the new log system

This commit is contained in:
Raphael 2025-10-26 17:50:23 +01:00 committed by Raphaël
parent c579048b33
commit afb9b13647
3 changed files with 73 additions and 24 deletions

View file

@ -1,7 +1,14 @@
import { Events, AuditLogEvent, TextChannel, EmbedBuilder, Channel } from 'discord.js'; import {
Events,
AuditLogEvent,
TextChannel,
EmbedBuilder,
Channel,
} from 'discord.js';
import { prisma } from '../../lib/prisma'; import { prisma } from '../../lib/prisma';
import { Guild as GuildPrisma } from '@prisma/client'; import { Guild as GuildPrisma } from '@prisma/client';
import { isWhitelisted } from '@lib/perm'; import { isWhitelisted } from '@lib/perm';
import { log } from '@lib/log';
export default { export default {
name: Events.ChannelCreate, name: Events.ChannelCreate,
@ -22,16 +29,27 @@ export default {
}, },
}); });
if (!(await isWhitelisted(executor.id, channel.guild.id))) { if (!(await isWhitelisted(executor.id, channel.guild.id))) {
await channel.delete(`Unauthorized channel creation by ${executor.tag}`); await channel.delete(
const member = await channel.guild.members.fetch(executor.id).catch(() => null); `Unauthorized channel creation by ${executor.tag}`,
);
const member = await channel.guild.members
.fetch(executor.id)
.catch(() => null);
if (member) { if (member) {
const rolesToRemove = member.roles.cache.filter(r => r.id !== channel.guild.id); const rolesToRemove = member.roles.cache.filter(
(r) => r.id !== channel.guild.id,
);
for (const [id] of rolesToRemove) { for (const [id] of rolesToRemove) {
await member.roles.remove(id, 'Unauthorized channel creation [TTY AntiRaid]'); await member.roles.remove(
id,
'Unauthorized channel creation [TTY AntiRaid]',
);
} }
} }
if (guildData.logMod) { if (guildData.logMod) {
const logChannel = await channel.guild.channels.fetch(guildData.logMod).catch(() => null); const logChannel = await channel.guild.channels
.fetch(guildData.logMod)
.catch(() => null);
if (logChannel?.isTextBased()) { if (logChannel?.isTextBased()) {
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setTitle('⚠️ | Anti-Channel Protection') .setTitle('⚠️ | Anti-Channel Protection')
@ -51,11 +69,15 @@ export default {
return; return;
} }
if (guildData.logChannel) { if (guildData.logChannel) {
const logChannel = await channel.guild.channels.fetch(guildData.logChannel).catch(() => null); const logChannel = await channel.guild.channels
.fetch(guildData.logChannel)
.catch(() => null);
if (logChannel?.isTextBased()) { if (logChannel?.isTextBased()) {
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setTitle('📢 Channel Created') .setTitle('📢 Channel Created')
.setDescription(`Channel **${channel.name}** has been created by <@${executor.id}>.`) .setDescription(
`Channel **${channel.name}** has been created by <@${executor.id}>.`,
)
.setColor(guildData.color) .setColor(guildData.color)
.setTimestamp() .setTimestamp()
.setFooter({ .setFooter({
@ -68,7 +90,7 @@ export default {
} }
} }
catch (err) { catch (err) {
console.error(`⚠️ | ChannelCreate protection error: ${err as Error}`); log.error(err, 'ChannelCreate protection error');
} }
}, },
}; };

View file

@ -1,7 +1,14 @@
import { Events, AuditLogEvent, TextChannel, EmbedBuilder, Channel } from 'discord.js'; import {
Events,
AuditLogEvent,
TextChannel,
EmbedBuilder,
Channel,
} from 'discord.js';
import { prisma } from '../../lib/prisma'; import { prisma } from '../../lib/prisma';
import { Guild as GuildPrisma } from '@prisma/client'; import { Guild as GuildPrisma } from '@prisma/client';
import { isWhitelisted } from '@lib/perm'; import { isWhitelisted } from '@lib/perm';
import { log } from '@lib/log';
export default { export default {
name: Events.ChannelDelete, name: Events.ChannelDelete,
@ -22,18 +29,27 @@ export default {
}, },
}); });
if (!(await isWhitelisted(executor.id, channel.guild.id))) { if (!(await isWhitelisted(executor.id, channel.guild.id))) {
const member = await channel.guild.members.fetch(executor.id).catch(() => null); const member = await channel.guild.members
.fetch(executor.id)
.catch(() => null);
if (member) { if (member) {
const rolesToRemove = member.roles.cache.filter(r => r.id !== channel.guild.id); const rolesToRemove = member.roles.cache.filter(
(r) => r.id !== channel.guild.id,
);
for (const [id] of rolesToRemove) { for (const [id] of rolesToRemove) {
await member.roles.remove(id, 'Unauthorized channel deletion [TTY AntiRaid]'); await member.roles.remove(
id,
'Unauthorized channel deletion [TTY AntiRaid]',
);
} }
} }
channel.clone().then((newchannel) => { channel.clone().then((newchannel) => {
newchannel.setPosition(channel.position); newchannel.setPosition(channel.position);
}); });
if (guildData.logMod) { if (guildData.logMod) {
const logChannel = await channel.guild.channels.fetch(guildData.logMod).catch(() => null); const logChannel = await channel.guild.channels
.fetch(guildData.logMod)
.catch(() => null);
if (logChannel instanceof TextChannel) { if (logChannel instanceof TextChannel) {
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setTitle('⚠️ | Anti-Channel Protection') .setTitle('⚠️ | Anti-Channel Protection')
@ -45,7 +61,7 @@ export default {
.setFooter({ .setFooter({
text: guildData.footer, text: guildData.footer,
}); });
await (logChannel).send({ await logChannel.send({
embeds: [embed], embeds: [embed],
}); });
} }
@ -53,7 +69,9 @@ export default {
return; return;
} }
if (guildData.logChannels) { if (guildData.logChannels) {
const logChannel = await channel.guild.channels.fetch(guildData.logChannel).catch(() => null); const logChannel = await channel.guild.channels
.fetch(guildData.logChannel)
.catch(() => null);
if (logChannel instanceof TextChannel) { if (logChannel instanceof TextChannel) {
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setTitle('🗑️ | Channel Deleted') .setTitle('🗑️ | Channel Deleted')
@ -63,14 +81,14 @@ export default {
.setFooter({ .setFooter({
text: guildData.footer, text: guildData.footer,
}); });
await (logChannel).send({ await logChannel.send({
embeds: [embed], embeds: [embed],
}); });
} }
} }
} }
catch (err) { catch (err) {
console.error(`⚠️ | ChannelDelete protection error: ${err as Error}`); log.error(err, 'ChannelDelete protection error');
} }
}, },
}; };

View file

@ -9,6 +9,7 @@ import {
import { prisma } from '../../lib/prisma'; import { prisma } from '../../lib/prisma';
import { Guild as GuildPrisma } from '@prisma/client'; import { Guild as GuildPrisma } from '@prisma/client';
import { getCorrectMention } from '../../lib/mention'; import { getCorrectMention } from '../../lib/mention';
import { log } from '@lib/log';
export default { export default {
name: Events.ChannelUpdate, name: Events.ChannelUpdate,
@ -16,11 +17,15 @@ export default {
if (!newChannel.guild) return; if (!newChannel.guild) return;
try { try {
const logs = await newChannel.guild.fetchAuditLogs({ const logs = await newChannel.guild.fetchAuditLogs({
type: AuditLogEvent.ChannelUpdate | AuditLogEvent.ChannelOverwriteCreate | AuditLogEvent.ChannelOverwriteDelete | AuditLogEvent.ChannelOverwriteUpdate, type:
AuditLogEvent.ChannelUpdate |
AuditLogEvent.ChannelOverwriteCreate |
AuditLogEvent.ChannelOverwriteDelete |
AuditLogEvent.ChannelOverwriteUpdate,
limit: 5, limit: 5,
}); });
const entry = [...logs.entries.values()] const entry = [...logs.entries.values()]
.filter(e => (e.target as GuildChannel).id === newChannel.id) .filter((e) => (e.target as GuildChannel).id === newChannel.id)
.sort((a, b) => b.createdTimestamp - a.createdTimestamp)[0]; .sort((a, b) => b.createdTimestamp - a.createdTimestamp)[0];
const executor = entry?.executor; const executor = entry?.executor;
const guildData: GuildPrisma | null = await prisma.guild.findUnique({ const guildData: GuildPrisma | null = await prisma.guild.findUnique({
@ -29,7 +34,9 @@ export default {
if (!guildData) return; if (!guildData) return;
const changes: string[] = []; const changes: string[] = [];
if (oldChannel.name !== newChannel.name) { if (oldChannel.name !== newChannel.name) {
changes.push(`**Name:** \`${oldChannel.name}\`\`${newChannel.name}\``); changes.push(
`**Name:** \`${oldChannel.name}\`\`${newChannel.name}\``,
);
} }
if ('topic' in oldChannel && 'topic' in newChannel) { if ('topic' in oldChannel && 'topic' in newChannel) {
if (oldChannel.topic !== newChannel.topic) { if (oldChannel.topic !== newChannel.topic) {
@ -43,7 +50,9 @@ export default {
newPerms.forEach((overwrite, id) => { newPerms.forEach((overwrite, id) => {
const old = oldPerms.get(id); const old = oldPerms.get(id);
if (!old) { if (!old) {
changes.push(`New overwrite added for ${getCorrectMention(oldChannel.guild, id)}`); changes.push(
`New overwrite added for ${getCorrectMention(oldChannel.guild, id)}`,
);
return; return;
} }
if ( if (
@ -78,7 +87,7 @@ export default {
} }
} }
catch (err) { catch (err) {
console.error(`⚠️ | ChannelUpdate log error: ${err as Error}`); log.error(err, 'ChannelUpdate log error');
} }
}, },
}; };