Fetch target user from audit log

This commit is contained in:
Lukas | AstroGD 2022-11-29 02:36:46 +01:00
parent 1c6c79dacf
commit 5eb56efb79
Signed by: AstroGD
GPG Key ID: 82A5E6C236C535AA

View File

@ -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<null | GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate, "Update", "Channel", AuditLogEvent.ChannelUpdate>>((unknown, curr) => {
if (!unknown) return curr;
const prev = unknown as GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate, "Update", "Channel", AuditLogEvent.ChannelUpdate>
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({