wsb-bot / src /commands /permissionAudit.js
APRK01
Initial commit: WSB Discord Bot
3c7e34b
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 })],
});
}
},
};