galactic-bot/structure/client/components/inhibitors/Permissions.js

78 lines
2.9 KiB
JavaScript
Raw Normal View History

const { Inhibitor } = require('../../../interfaces/');
const safeCommands = ['command:settings', 'command:grant', 'command:revoke'];
class Permissions extends Inhibitor {
constructor(client) {
super(client, {
name: 'permissions',
priority: 10,
guarded: true,
guild: true
});
}
async execute(message, command, override = null) {
const { permissionType } = message.guild._settings;
2020-07-28 22:40:15 +02:00
if(message.author.developer) return super._succeed();
const permissions = override ? override : command.memberPermissions;
let missing = [];
if(permissionType === 'discord') {
missing = this._checkDiscord(message, permissions);
//NOTE: Rewrite this entire fucking file :)
if(missing.length === permissions.length && permissions.length > 0) return super._fail({ missing: missing.join(', ') });
return super._succeed();
} else if(permissionType === 'grant' && command.permissionRequired) {
// vv Prevent Softlock vv
if(safeCommands.includes(command.resolveable) && (this._checkDiscord(message, ['ADMINISTRATOR']).length === 0 || this._checkDiscord(message, ['MANAGE_GUILD']).length === 0)) {
return super._succeed();
}
missing = await this._checkGrants(message, command.resolveable);
2021-06-11 03:07:27 +02:00
} else if(permissionType === 'both') {
2021-06-09 22:44:53 +02:00
const disc = this._checkDiscord(message, permissions);
const grants = await this._checkGrants(message, command.resolveable);
if (disc.length === permissions.length && permissions.length > 0 && grants.length) return super._fail({ missing: [...disc, ...grants].join(', ') });
return super._succeed();
}
if(missing.length > 0) {
return super._fail({ missing: missing.join(', ') });
}
return super._succeed();
}
_checkDiscord(message, permissions) {
return message.channel.permissionsFor(message.member).missing(permissions);
}
async _checkGrants(message, permission) { //async just incase
const _permissions = await message.guild.permissions();
const roles = message.member.roles.cache.keyArray();
const channelId = message.channel.id;
const userId = message.author.id;
let allowed = false;
for(const [ id, permissions ] of Object.entries(_permissions)) {
// console.log(id, permissions);
if(id === userId || roles.includes(id)) {
if(permissions.global.includes(permission)) {
allowed = true; break;
}
const channel = permissions.channels[channelId];
if(!channel) continue;
if(channel.includes(permission)) allowed = true; break;
}
continue;
}
return allowed ? [] : [permission];
}
}
module.exports = Permissions;