Weird PermissionOverwrite triggers ChannelUpdate for every guildChannel

Hello. I've setup a listener to the event ChannelUpdate that should handle permissionOverwrites modifications. But I some weird trigger that concerns every channels. I've node v16.14.2, djs 13.9.0 Here's the code :
client.on("channelUpdate", onChannelUpdate);

//in another file
export const onChannelUpdate = async (oldChannel, newChannel) => {
//handle channel update event
...
//basic operations
const logChannel = await getLogChannel(commons, newChannel); //get logChannelId
const embed = setupEmbed("DARK_AQUA", chnUp, newChannel, "tag"); //setup embed
const chnLog = await fetchAuditLog(oldChannel.guild, "CHANNEL_UPDATE", 1); //get auditLog

//check for permission overwrite
const oldOverwrite = oldChannel.permissionOverwrites.cache;
const newOverwrite = newChannel.permissionOverwrites.cache;
const diffOverwrite = oldOverwrite.difference(newOverwrite);

if (diffOverwrite.size !== 0) {
//add/removed permission orverwrite
...
}
if (!oldOverwrite.equals(newOverwrite)) {
//if permissionOverwrite changed without add/remove role/user
//sort by id
oldOverwrite.sort((a, b) => a.id - b.id);
newOverwrite.sort((a, b) => a.id - b.id);

//find PO difference by couple
const diff = oldOverwrite.reduce((acc, cur) => {
const newPO = newOverwrite.get(cur.id);
if (
cur.deny.bitfield !== newPO.deny.bitfield ||
cur.allow.bitfield !== newPO.allow.bitfield
)
return [...acc, [cur, newPO]];
else return acc;
}, []);

//get bit diff, write it along channel.toString()
const modifs = await diff.reduce(async (acc, cur) => {
...
}, "");

if (modifs.length !== 0) {
embed.addField(chnUp.text, modifs); //add modifs in embed
finishEmbed(chnUp, null, embed, logChannel); //send log message
}
else console.log("channelUpdate permOverwrite noModifs", new Date(), newChannel.name, diff, [modifs]);
return;
}
client.on("channelUpdate", onChannelUpdate);

//in another file
export const onChannelUpdate = async (oldChannel, newChannel) => {
//handle channel update event
...
//basic operations
const logChannel = await getLogChannel(commons, newChannel); //get logChannelId
const embed = setupEmbed("DARK_AQUA", chnUp, newChannel, "tag"); //setup embed
const chnLog = await fetchAuditLog(oldChannel.guild, "CHANNEL_UPDATE", 1); //get auditLog

//check for permission overwrite
const oldOverwrite = oldChannel.permissionOverwrites.cache;
const newOverwrite = newChannel.permissionOverwrites.cache;
const diffOverwrite = oldOverwrite.difference(newOverwrite);

if (diffOverwrite.size !== 0) {
//add/removed permission orverwrite
...
}
if (!oldOverwrite.equals(newOverwrite)) {
//if permissionOverwrite changed without add/remove role/user
//sort by id
oldOverwrite.sort((a, b) => a.id - b.id);
newOverwrite.sort((a, b) => a.id - b.id);

//find PO difference by couple
const diff = oldOverwrite.reduce((acc, cur) => {
const newPO = newOverwrite.get(cur.id);
if (
cur.deny.bitfield !== newPO.deny.bitfield ||
cur.allow.bitfield !== newPO.allow.bitfield
)
return [...acc, [cur, newPO]];
else return acc;
}, []);

//get bit diff, write it along channel.toString()
const modifs = await diff.reduce(async (acc, cur) => {
...
}, "");

if (modifs.length !== 0) {
embed.addField(chnUp.text, modifs); //add modifs in embed
finishEmbed(chnUp, null, embed, logChannel); //send log message
}
else console.log("channelUpdate permOverwrite noModifs", new Date(), newChannel.name, diff, [modifs]);
return;
}
2 Replies
d.js docs
d.js docs3y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Eccléria
EcclériaOP3y ago
I am wondering why I have all theses logs with no apparent reason. Early, somebody told me that there should be an AuditLog about it, but when fetching I'm seeing that it refers to an older modification that I did precedently. To be sure I'm fetching correctly, here is the AuditLog fetching part
const chnLog = await fetchAuditLog(oldChannel.guild, "CHANNEL_UPDATE", 1); //get auditLog

//in another file
export const fetchAuditLog = async (guild, auditType, limit, type) => {
try {
const fetchedLogs = await guild.fetchAuditLogs({
limit: limit,
type: auditType,
}); //fetch logs
if (type === "list") return fetchedLogs.entries; //return the first
return fetchedLogs.entries.first();
} catch (e) {
//if no permission => crash
console.log("AuditLog Fetch Error", e);
return null;
}
};
const chnLog = await fetchAuditLog(oldChannel.guild, "CHANNEL_UPDATE", 1); //get auditLog

//in another file
export const fetchAuditLog = async (guild, auditType, limit, type) => {
try {
const fetchedLogs = await guild.fetchAuditLogs({
limit: limit,
type: auditType,
}); //fetch logs
if (type === "list") return fetchedLogs.entries; //return the first
return fetchedLogs.entries.first();
} catch (e) {
//if no permission => crash
console.log("AuditLog Fetch Error", e);
return null;
}
};
But if there is a difference in values, why isn't there any AuditLog ? Oh, this way So this massive trigger could be a channel reorganisation, that update every rawPosition ? Firing the channelUpdate listener for every channels Okay thanks 🙂

Did you find this page helpful?