Rework bot function #13
@@ -1,5 +1,5 @@
 | 
				
			|||||||
import client from "../client";
 | 
					import client from "../client";
 | 
				
			||||||
import { Events } from "discord.js";
 | 
					import { AuditLogEvent, Events, GuildAuditLogsEntry, PermissionFlagsBits, User } from "discord.js";
 | 
				
			||||||
import { getGuildSetting } from "../tools/data";
 | 
					import { getGuildSetting } from "../tools/data";
 | 
				
			||||||
import { Badword, database } from "../data";
 | 
					import { Badword, database } from "../data";
 | 
				
			||||||
import { IsNull } from "typeorm";
 | 
					import { IsNull } from "typeorm";
 | 
				
			||||||
@@ -27,7 +27,7 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const blocklist = [...globalBlocklist, ...localBlocklist];
 | 
					    const blocklist = [...globalBlocklist, ...localBlocklist];
 | 
				
			||||||
    let found: string | null = null;
 | 
					    let found: string | null = null;
 | 
				
			||||||
    
 | 
					
 | 
				
			||||||
    for (let i = 0; i < blocklist.length; i++) {
 | 
					    for (let i = 0; i < blocklist.length; i++) {
 | 
				
			||||||
        const word = blocklist[i];
 | 
					        const word = blocklist[i];
 | 
				
			||||||
        if (!word) continue;
 | 
					        if (!word) continue;
 | 
				
			||||||
@@ -39,6 +39,33 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if (found === null) return;
 | 
					    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;
 | 
					    const logChannel = settings.notificationChannelID ? await getGuildChannel(guild.id, settings.notificationChannelID) : null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
@@ -51,6 +78,9 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => {
 | 
				
			|||||||
        embed.addFields({
 | 
					        embed.addFields({
 | 
				
			||||||
            name: "Detected banned word:",
 | 
					            name: "Detected banned word:",
 | 
				
			||||||
            value: `||${found}||`
 | 
					            value: `||${found}||`
 | 
				
			||||||
 | 
					        }, {
 | 
				
			||||||
 | 
					            name: "Channel renamed by:",
 | 
				
			||||||
 | 
					            value: responsibleUser ? `${responsibleUser.tag} (${responsibleUser.id})` : "`Couldn't detect responsible user :(`"
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        logChannel.send({
 | 
					        logChannel.send({
 | 
				
			||||||
@@ -68,6 +98,9 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => {
 | 
				
			|||||||
        name: "Detected banned word:",
 | 
					        name: "Detected banned word:",
 | 
				
			||||||
        value: `||${found}||`,
 | 
					        value: `||${found}||`,
 | 
				
			||||||
        inline: true
 | 
					        inline: true
 | 
				
			||||||
 | 
					    }, {
 | 
				
			||||||
 | 
					        name: "Channel renamed by:",
 | 
				
			||||||
 | 
					        value: responsibleUser ? `${responsibleUser.tag} (${responsibleUser.id})` : "`Couldn't detect responsible user :(`"
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    logChannel.send({
 | 
					    logChannel.send({
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user