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 { Guild as GuildPrisma } from '@prisma/client';
import { isWhitelisted } from '@lib/perm';
import { log } from '@lib/log';
export default {
name: Events.ChannelCreate,
@ -22,16 +29,27 @@ export default {
},
});
if (!(await isWhitelisted(executor.id, channel.guild.id))) {
await channel.delete(`Unauthorized channel creation by ${executor.tag}`);
const member = await channel.guild.members.fetch(executor.id).catch(() => null);
await channel.delete(
`Unauthorized channel creation by ${executor.tag}`,
);
const member = await channel.guild.members
.fetch(executor.id)
.catch(() => null);
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) {
await member.roles.remove(id, 'Unauthorized channel creation [TTY AntiRaid]');
await member.roles.remove(
id,
'Unauthorized channel creation [TTY AntiRaid]',
);
}
}
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()) {
const embed = new EmbedBuilder()
.setTitle('⚠️ | Anti-Channel Protection')
@ -51,11 +69,15 @@ export default {
return;
}
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()) {
const embed = new EmbedBuilder()
.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)
.setTimestamp()
.setFooter({
@ -68,7 +90,7 @@ export default {
}
}
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 { Guild as GuildPrisma } from '@prisma/client';
import { isWhitelisted } from '@lib/perm';
import { log } from '@lib/log';
export default {
name: Events.ChannelDelete,
@ -22,18 +29,27 @@ export default {
},
});
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) {
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) {
await member.roles.remove(id, 'Unauthorized channel deletion [TTY AntiRaid]');
await member.roles.remove(
id,
'Unauthorized channel deletion [TTY AntiRaid]',
);
}
}
channel.clone().then((newchannel) => {
newchannel.setPosition(channel.position);
});
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) {
const embed = new EmbedBuilder()
.setTitle('⚠️ | Anti-Channel Protection')
@ -45,7 +61,7 @@ export default {
.setFooter({
text: guildData.footer,
});
await (logChannel).send({
await logChannel.send({
embeds: [embed],
});
}
@ -53,7 +69,9 @@ export default {
return;
}
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) {
const embed = new EmbedBuilder()
.setTitle('🗑️ | Channel Deleted')
@ -63,14 +81,14 @@ export default {
.setFooter({
text: guildData.footer,
});
await (logChannel).send({
await logChannel.send({
embeds: [embed],
});
}
}
}
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 { Guild as GuildPrisma } from '@prisma/client';
import { getCorrectMention } from '../../lib/mention';
import { log } from '@lib/log';
export default {
name: Events.ChannelUpdate,
@ -16,11 +17,15 @@ export default {
if (!newChannel.guild) return;
try {
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,
});
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];
const executor = entry?.executor;
const guildData: GuildPrisma | null = await prisma.guild.findUnique({
@ -29,7 +34,9 @@ export default {
if (!guildData) return;
const changes: string[] = [];
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 (oldChannel.topic !== newChannel.topic) {
@ -43,12 +50,14 @@ export default {
newPerms.forEach((overwrite, id) => {
const old = oldPerms.get(id);
if (!old) {
changes.push(`New overwrite added for ${getCorrectMention(oldChannel.guild, id)}`);
changes.push(
`New overwrite added for ${getCorrectMention(oldChannel.guild, id)}`,
);
return;
}
if (
overwrite.allow.bitfield !== old.allow.bitfield ||
overwrite.deny.bitfield !== old.deny.bitfield
overwrite.deny.bitfield !== old.deny.bitfield
) {
changes.push(`Overwrite changed for <@&${id}> / <@${id}>`);
}
@ -78,7 +87,7 @@ export default {
}
}
catch (err) {
console.error(`⚠️ | ChannelUpdate log error: ${err as Error}`);
log.error(err, 'ChannelUpdate log error');
}
},
};