2020-04-17 17:23:13 +02:00
|
|
|
const { Inhibitor } = require('../../../interfaces/');
|
2020-04-16 14:37:04 +02:00
|
|
|
|
2020-06-02 12:09:28 +02:00
|
|
|
const safeCommands = ['command:settings', 'command:grant', 'command:revoke'];
|
|
|
|
|
|
|
|
class Permissions extends Inhibitor {
|
2020-04-16 14:37:04 +02:00
|
|
|
|
|
|
|
constructor(client) {
|
|
|
|
|
|
|
|
super(client, {
|
2020-06-02 12:09:28 +02:00
|
|
|
name: 'permissions',
|
2020-04-16 14:37:04 +02:00
|
|
|
priority: 10,
|
|
|
|
guarded: true,
|
|
|
|
guild: true
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-16 09:27:49 +02:00
|
|
|
async execute(message, command, override = null) {
|
2020-07-29 21:01:39 +02:00
|
|
|
const { permissionType } = message.guild._settings;
|
2020-07-28 22:40:15 +02:00
|
|
|
|
2020-07-29 21:01:39 +02:00
|
|
|
if(message.author.developer) return super._succeed();
|
2020-08-16 09:27:49 +02:00
|
|
|
const permissions = override ? override : command.memberPermissions;
|
2020-07-29 21:01:39 +02:00
|
|
|
|
|
|
|
let missing = [];
|
|
|
|
if(permissionType === 'discord') {
|
2020-08-16 09:27:49 +02:00
|
|
|
missing = this._checkDiscord(message, permissions);
|
2021-05-03 16:55:46 +02:00
|
|
|
} else if(permissionType === 'grant' && command.permissionRequired) {
|
2020-07-29 21:01:39 +02:00
|
|
|
// 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 && grants.length) return super._fail({ missing: [...disc, ...grants].join(', ') });
|
2020-07-29 21:01:39 +02:00
|
|
|
}
|
2020-06-02 12:09:28 +02:00
|
|
|
|
2020-04-16 14:37:04 +02:00
|
|
|
if(missing.length > 0) {
|
2020-05-25 13:13:34 +02:00
|
|
|
return super._fail({ missing: missing.join(', ') });
|
2020-06-02 12:09:28 +02:00
|
|
|
}
|
2020-05-21 12:47:58 +02:00
|
|
|
return super._succeed();
|
2020-06-02 12:09:28 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_checkDiscord(message, permissions) {
|
|
|
|
return message.channel.permissionsFor(message.member).missing(permissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _checkGrants(message, permission) { //async just incase
|
|
|
|
const _permissions = await message.guild.permissions();
|
2020-07-29 21:01:39 +02:00
|
|
|
const roles = message.member.roles.cache.keyArray();
|
|
|
|
const channelId = message.channel.id;
|
|
|
|
const userId = message.author.id;
|
2020-06-02 12:09:28 +02:00
|
|
|
|
|
|
|
let allowed = false;
|
|
|
|
for(const [ id, permissions ] of Object.entries(_permissions)) {
|
2020-07-29 21:01:39 +02:00
|
|
|
// console.log(id, permissions);
|
2020-06-02 12:09:28 +02:00
|
|
|
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];
|
2020-04-16 14:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:09:28 +02:00
|
|
|
module.exports = Permissions;
|