File size: 3,063 Bytes
3c7e34b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | const { ChannelType, PermissionFlagsBits } = require('discord.js');
const { createEmbed } = require('../utils/embeds');
const { Colors } = require('../config');
module.exports = {
name: 'permission audit',
async execute(client, message) {
const guildId = process.env.GUILD_ID;
const guild = await client.guilds.fetch(guildId);
await guild.channels.fetch();
const issues = [];
const report = [];
const categories = guild.channels.cache
.filter(c => c.type === ChannelType.GuildCategory)
.sort((a, b) => a.position - b.position);
for (const [, category] of categories) {
const catReport = [`π **${category.name}**`];
const children = guild.channels.cache
.filter(c => c.parentId === category.id)
.sort((a, b) => a.position - b.position);
for (const [, channel] of children) {
const everyone = channel.permissionOverwrites.cache.get(guild.roles.everyone.id);
const everyoneView = everyone?.deny?.has(PermissionFlagsBits.ViewChannel) ? 'π' : 'ποΈ';
const everyoneSend = everyone?.deny?.has(PermissionFlagsBits.SendMessages) ? 'π' : 'π¬';
catReport.push(` β ${channel.name} β ${everyoneView} ${everyoneSend}`);
// Check for potential issues
if (!everyone) {
issues.push(`β οΈ ${channel.name} β no @everyone override set`);
}
}
report.push(catReport.join('\n'));
}
// Legend
const legend = '```\nποΈ = @everyone can view\nπ = @everyone cannot view\nπ¬ = @everyone can send\nπ = @everyone cannot send\n```';
const embed = createEmbed({
title: 'π‘οΈ Permission Audit',
description: legend + '\n\n' + report.join('\n\n'),
color: Colors.INFO,
});
const parts = [];
const embedStr = legend + '\n\n' + report.join('\n\n');
// Discord embed limit is 4096 characters
if (embedStr.length > 4000) {
// Send as file instead
const buffer = Buffer.from(report.join('\n\n'), 'utf-8');
await message.reply({
embeds: [createEmbed({
title: 'π‘οΈ Permission Audit',
description: `${legend}\n\nFull report attached (too long for embed).${issues.length ? '\n\n**Issues Found:**\n' + issues.join('\n') : '\n\nβ
No issues found.'}`,
color: Colors.INFO,
})],
files: [{ attachment: buffer, name: 'permission-audit.txt' }],
});
} else {
const desc = embedStr + (issues.length ? '\n\n**Issues Found:**\n' + issues.join('\n') : '\n\nβ
No issues found.');
await message.reply({
embeds: [createEmbed({ title: 'π‘οΈ Permission Audit', description: desc, color: Colors.INFO })],
});
}
},
};
|