diff --git a/src/events/channelUpdate.ts b/src/events/channelUpdate.ts index f51e729..9e297b2 100644 --- a/src/events/channelUpdate.ts +++ b/src/events/channelUpdate.ts @@ -1,5 +1,5 @@ import client from "../client"; -import { Events } from "discord.js"; +import { AuditLogEvent, Events, GuildAuditLogsEntry, PermissionFlagsBits, User } from "discord.js"; import { getGuildSetting } from "../tools/data"; import { Badword, database } from "../data"; import { IsNull } from "typeorm"; @@ -27,7 +27,7 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => { const blocklist = [...globalBlocklist, ...localBlocklist]; let found: string | null = null; - + for (let i = 0; i < blocklist.length; i++) { const word = blocklist[i]; if (!word) continue; @@ -39,6 +39,33 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => { if (found === null) return; + let responsibleUser: User | null; + + try { + const clientMember = await guild.members.fetchMe(); + const canSeeAuditLog = clientMember.permissions.has(PermissionFlagsBits.ViewAuditLog); + const auditLogs = canSeeAuditLog ? await guild.fetchAuditLogs({ + type: AuditLogEvent.ChannelUpdate, + limit: 50 + }) : undefined; + + const change = auditLogs?.entries.filter((entry) => { + if (entry.target.id !== newChannel.id) return false; + + return entry.changes.filter((change) => { + return change.key === "name" && change.new === newChannel.name; + }).length > 0; + }).reduce>((unknown, curr) => { + if (!unknown) return curr; + const prev = unknown as GuildAuditLogsEntry + return curr.createdTimestamp > prev.createdTimestamp ? curr : prev; + }, null); + + responsibleUser = change?.executor || null; + } catch (error) { + responsibleUser = null; + } + const logChannel = settings.notificationChannelID ? await getGuildChannel(guild.id, settings.notificationChannelID) : null; try { @@ -51,6 +78,9 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => { embed.addFields({ name: "Detected banned word:", value: `||${found}||` + }, { + name: "Channel renamed by:", + value: responsibleUser ? `${responsibleUser.tag} (${responsibleUser.id})` : "`Couldn't detect responsible user :(`" }); logChannel.send({ @@ -68,6 +98,9 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => { name: "Detected banned word:", value: `||${found}||`, inline: true + }, { + name: "Channel renamed by:", + value: responsibleUser ? `${responsibleUser.tag} (${responsibleUser.id})` : "`Couldn't detect responsible user :(`" }); logChannel.send({