Search code examples
discord.js

log when a channel permissions got changed discord.js


I am trying to log when a permissions from a channel got updated. This is what i have now: but i don't come further at this point. Hope some one could help me here.

const { MessageEmbed, GuildChannel } = require("discord.js");
const DB = require('../../Structures/Schemas/loggingDB');


module.exports = {
    name: "channelUpdate",
    /**
       * 
       * @param {GuildChannel} oldChannel
       * @param {GuildChannel} newChannel
       */
    async execute(oldChannel, newChannel) {

        const { guild } = oldChannel;

        const data = await DB.findOne({ GuildID: guild.id });
        if (!data) {
            return
        }

        const LogChannel = await guild.channels.fetch(data.ChannelID)

        if (!LogChannel.guild) return false; 

        const AuditLogFetch = await oldChannel.guild.fetchAuditLogs({ limit: 1, type: "CHANNEL_UPDATE" }); 

        if (!LogChannel) return console.error(`Invalid channel.`); 

        if (!AuditLogFetch.entries.first()) return console.error(`No entries found.`);

        const Entry = AuditLogFetch.entries.first(); 

        //console.log(Entry)
        //console.log(oldChannel)
        //console.log(newChannel)
        //console.log(oldChannel)

        const embed = new MessageEmbed()
            .setColor("#b827ba")
            .setTitle("Channel Updated")
            .setDescription("An channel has been updated!")
            .addFields(
                { name: "Updated By:", value: `${Entry.executor.tag || "Someone"}` },
                { name: "Channel Name:", value: `${oldChannel.name}` },
                { name: "Action:", value: `${Entry.action}` },
                { name: "Created At:", value: `<t:${parseInt(oldChannel.createdTimestamp / 1000)}:f>` },
            )
            .setTimestamp()

        if (oldChannel.name != newChannel.name) {
            embed.addFields(
                { name: "Old Channel Name:", value: `${oldChannel.name}` },
                { name: "New Channel Name:", value: `${newChannel.name}` },
            )
        }
        console.log("Oldchannel:", oldChannel.permissionOverwrites.cache)
        console.log("newchannel:", newChannel.permissionOverwrites.cache)

        LogChannel.send({ embeds: [embed] })
    }
}

This is the output that i get from console.log("newchannel:", newChannel.permissionOverwrites.cache. From this point i don't know how i get the names etc.

Oldchannel: Collection(2) [Map] {
      '973305365540266055' => PermissionOverwrites {
        id: '973305365540266055',
        type: 'role',
        deny: Permissions { bitfield: 0n },
        allow: Permissions { bitfield: 1024n }
      },
      '976422603227013130' => PermissionOverwrites {
        id: '976422603227013130',
        type: 'role',
        deny: Permissions { bitfield: 1024n },
        allow: Permissions { bitfield: 0n }
      }
    }
    newchannel: Collection(2) [Map] {
      '973305365540266055' => PermissionOverwrites {
        id: '973305365540266055',
        type: 'role',
        deny: Permissions { bitfield: 0n },
        allow: Permissions { bitfield: 0n }
      },
      '976422603227013130' => PermissionOverwrites {
        id: '976422603227013130',
        type: 'role',
        deny: Permissions { bitfield: 1024n },
        allow: Permissions { bitfield: 0n }
      }
    }

Solution

  • fixed by using code as below -

    
    const { MessageEmbed, GuildChannel } = require("discord.js");
    const DB = require('../../Structures/Schemas/loggingDB');
    const { ColorYom } = require("../../Structures/botConfig.json") 
    
    
    module.exports = {
        name: "channelUpdate",
        /**
           * 
           * @param {GuildChannel} oldChannel
           * @param {GuildChannel} newChannel
           */
        async execute(oldChannel, newChannel) {
    
            const { guild } = oldChannel;
    
            const data = await DB.findOne({ GuildID: guild.id });
            if (!data) {
                return
            }
    
            const LogChannel = await guild.channels.fetch(data.ChannelID)
    
            if (!LogChannel.guild) return false; 
    
            const AuditLogFetch = await oldChannel.guild.fetchAuditLogs({ limit: 1, type: "CHANNEL_UPDATE" }); 
    
            if (!LogChannel) return console.error(`Invalid channel.`); 
    
            if (!AuditLogFetch.entries.first()) return console.error(`No entries found.`);
    
            const Entry = AuditLogFetch.entries.first(); 
    
            const embed = new MessageEmbed()
                .setColor(`${ColorYom}`)
                .setTitle("Channel Updated")
                .setDescription("An channel has been updated!")
                .addFields(
                    { name: "Updated By:", value: `${Entry.executor.tag || "Someone"}` },
                    { name: "Channel Name:", value: `${oldChannel.name}` },
                    { name: "Action:", value: `${Entry.action}` },
                )
                .setTimestamp()
    
            if (oldChannel.name != newChannel.name) {
                embed.addFields(
                    { name: "Old Channel Name:", value: `${oldChannel.name}` },
                    { name: "New Channel Name:", value: `${newChannel.name}` },
                )
            }
    
            LogChannel.send({ embeds: [embed] })
        }
    }