galactic-bot/structure/client/components/commands/administration/Grant.js

148 lines
5.4 KiB
JavaScript
Raw Normal View History

const { Command } = require('../../../../interfaces/');
const { GuildMember } = require('../../../../extensions/');
class GrantCommand extends Command {
constructor(client) {
super(client, {
name: 'grant',
module: 'administration',
2020-07-25 08:02:22 +02:00
usage: "<role..|user..> <permissions..>",
examples: [
"\"Server Moderators\" module:moderation",
"@nolan#2887 command:kick"
],
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();
const { parsed, parameters } = await this.client.resolver.infinite(params, [
this.client.resolver.resolveMember.bind(this.client.resolver),
this.client.resolver.resolveRole.bind(this.client.resolver)
], true, message.guild);
if(parsed.length === 0) {
return message.respond(message.format('C_GRANT_MISSINGRESOLVEABLES'), {
emoji: 'failure'
});
}
const permissions = this.client.registry.components.filter((c) => c.type === 'command' ||
c.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(!search) continue;
if(search.type === 'module') {
for(const component of search.components.values()) {
if(component.type === 'command') parsedPermissions.push(component.resolveable);
}
} else {
parsedPermissions.push(search.resolveable);
}
}
}
if(parsedPermissions.length === 0) {
return message.respond(message.format('C_GRANT_MISSINGPERMISSIONNODES'), {
emoji: 'failure'
});
}
for(const resolveable of parsed) {
if(!_permissions[resolveable.id]) {
_permissions[resolveable.id] = {
global: [],
channels: {}
};
}
const existing = _permissions[resolveable.id];
if(args.channel) {
for(const channel of args.channel.value) {
const existingChannel = existing.channels[channel.id];
if(existingChannel) {
for(const perm of parsedPermissions) {
if(existingChannel.includes(perm)) {
continue;
} else {
existingChannel.push(perm);
}
}
} else {
existing.channels[channel.id] = parsedPermissions;
}
}
} else {
for(const perm of parsedPermissions) {
if(existing.global.includes(perm)) {
continue;
} else {
existing.global.push(perm);
}
}
}
}
if(_permissions._id) 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_GRANT_DATABASEERROR'), { emoji: 'failure' });
return undefined;
}
return message.respond(message.format('C_GRANT_SUCCESS', {
targets: parsed.map((p) => p instanceof GuildMember ? `**${p.user.tag}**` : `**${p.name}**`).join(' '),
permissions: parsedPermissions.map((p) => `\`${p}\``).join(', '),
channel: args.channel ? ` ${message.format('C_GRANT_SUCCESSCHANNELS', { channels: args.channel.value.map((c) => `**#${c.name}**`).join(', '), plural: args.channel.value.length === 1 ? '' : 's' })}` : ''
}), { emoji: 'success' });
}
}
module.exports = GrantCommand;
const filterInexact = (search) => (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)));