galactic-bot/structure/client/components/commands/administration/Revoke.js
2020-07-28 13:40:15 -07:00

148 lines
5.6 KiB
JavaScript

const { User, GuildMember } = require('discord.js');
const { Command } = require('../../../../interfaces/');
class RevokeCommand extends Command {
constructor(client) {
super(client, {
name: 'revoke',
module: 'administration',
usage: "<role..|user..> <permissions..>",
examples: [
"\"Server Moderators\" module:moderation",
"@nolan#2887 command:kick",
"132620781791346688 moderation"
],
memberPermissions: ['ADMINISTRATOR', 'MANAGE_GUILD'],
showUsage: true,
guildOnly: true,
arguments: [
{
name: 'channel',
aliases: [
'channels'
],
type: 'TEXTCHANNEL',
types: ['FLAG', 'VERBAL'],
infinite: true
}
]
});
}
async execute(message, { params, args }) {
const _permissions = await message.guild.permissions();
let { parsed, parameters } = await this.client.resolver.infinite(params, [ //eslint-disable-line prefer-const
this.client.resolver.resolveMember.bind(this.client.resolver),
this.client.resolver.resolveUser.bind(this.client.resolver),
this.client.resolver.resolveRole.bind(this.client.resolver)
], true, message.guild);
parsed = parsed.filter((p) => _permissions[p.id]);
if(parsed.length === 0) {
return message.respond(message.format('C_REVOKE_MISSINGRESOLVEABLES'), {
emoji: 'failure'
});
}
const permissions = this.client.registry.components.filter((channel) => channel.type === 'command' || channel.type === 'module');
let parsedPermissions = [];
if(parameters.join(' ') === 'all') {
parsedPermissions = this.client.registry.components.filter((c) => c.type === 'command').map((c) => c.resolveable);
} else {
for(const perm of parameters) {
const search = permissions.filter(filterInexact(perm)).first(); //eslint-disable-line no-use-before-define
if(!perm) continue;
if(search.type === 'module') {
for(const component of search.components.values()) {
if(component.type === 'command') parsedPermissions.push(component.resolveable);
}
} else if (search.type === 'command') {
parsedPermissions.push(search.resolveable);
} else {
parsedPermissions.push(perm);
}
}
}
for(const resolveable of parsed) {
const permission = _permissions[resolveable.id];
if(args.channel) {
for(const channel of args.channel.value) {
const existingChannel = permission.channels[channel.id];
if(existingChannel) {
for(const parse of parsedPermissions) {
const index = existingChannel.indexOf(parse);
if(index > -1) {
permission.channels[channel.id].splice(index, 1);
}
}
if(existingChannel.length === 0) delete permission.channels[channel.id];
} else {
continue;
}
}
} else {
for(const parse of parsedPermissions) {
const index = permission.global.indexOf(parse);
if(index > -1) {
permission.global.splice(index, 1);
}
}
}
}
delete _permissions._id; //some bullshit..
try {
await this.client.transactionHandler.send({
provider: 'mongodb',
request: {
type: 'updateOne',
collection: 'permissions',
query: {
guildId: message.guild.id
},
data: _permissions
}
});
} catch(error) {
this.client.logger.error(error);
await message.respond(message.format('C_REVOKE_DATABASEERROR'), { emoji: 'failure' });
return undefined;
}
const name = (resolveable) => {
if(resolveable instanceof GuildMember) {
return resolveable.user.tag || resolveable.id;
} else if(resolveable instanceof User) {
return resolveable.tag || resolveable.id;
}
return resolveable.name || '[MISSING_INDEX]';
};
return message.respond(message.format('C_REVOKE_SUCCESS', {
targets: parsed.map((p) => `**${name(p)}**`).join(' '),
permissions: parsedPermissions.map((p) => `\`${p}\``).join(', '),
channel: args.channel ? ` ${message.format('C_REVOKE_SUCCESSCHANNELS', { channels: args.channel.value.map((c) => `**#${c.name}**`).join(', '), plural: args.channel.value.length === 1 ? '' : 's' })}` : ''
}), { emoji: 'success' });
}
}
module.exports = RevokeCommand;
const filterInexact = (search) => {
return (comp) => comp.id.toLowerCase().includes(search) ||
comp.resolveable.toLowerCase().includes(search) ||
comp.aliases && (comp.aliases.some((ali) => `${comp.type}:${ali}`.toLowerCase().includes(search)) ||
comp.aliases.some((ali) => ali.toLowerCase().includes(search)));
};