2020-05-08 08:50:54 +02:00
|
|
|
const { Command } = require('../../../../interfaces/');
|
2020-07-23 22:40:20 +02:00
|
|
|
const { GuildMember } = require('../../../../extensions/');
|
2020-05-08 08:50:54 +02:00
|
|
|
|
|
|
|
class GrantCommand extends Command {
|
|
|
|
|
|
|
|
constructor(client) {
|
|
|
|
|
|
|
|
super(client, {
|
|
|
|
name: 'grant',
|
|
|
|
module: 'administration',
|
2020-07-25 08:02:22 +02:00
|
|
|
usage: "<role..|user..> <permissions..>",
|
2020-05-08 08:50:54 +02:00
|
|
|
examples: [
|
|
|
|
"\"Server Moderators\" module:moderation",
|
|
|
|
"@nolan#2887 command:kick"
|
|
|
|
],
|
2020-07-23 22:40:20 +02:00
|
|
|
memberPermissions: ['ADMINISTRATOR', 'MANAGE_GUILD'],
|
2020-05-08 08:50:54 +02:00
|
|
|
showUsage: true,
|
|
|
|
guildOnly: true,
|
|
|
|
arguments: [
|
|
|
|
{
|
|
|
|
name: 'channel',
|
2020-05-21 12:47:58 +02:00
|
|
|
aliases: ['channels'],
|
2020-07-23 22:40:20 +02:00
|
|
|
type: 'TEXTCHANNEL',
|
2020-05-21 12:47:58 +02:00
|
|
|
types: [
|
|
|
|
'FLAG',
|
|
|
|
'VERBAL'
|
|
|
|
],
|
2020-05-08 08:50:54 +02:00
|
|
|
infinite: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async execute(message, { params, args }) {
|
|
|
|
|
|
|
|
const _permissions = await message.guild.permissions();
|
|
|
|
|
2020-07-23 22:40:20 +02:00
|
|
|
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'
|
|
|
|
});
|
2020-05-08 08:50:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
const permissions = this.client.registry.components.filter((c) => c.type === 'command' ||
|
|
|
|
c.type === 'module');
|
2020-05-08 08:50:54 +02:00
|
|
|
|
2020-07-23 22:40:20 +02:00
|
|
|
let parsedPermissions = [];
|
|
|
|
if(parameters.join(' ') === 'all') {
|
|
|
|
parsedPermissions = this.client.registry.components.filter((c) => c.type === 'command').map((c) => c.resolveable);
|
2020-05-08 08:50:54 +02:00
|
|
|
} else {
|
2020-07-23 22:40:20 +02:00
|
|
|
for(const perm of parameters) {
|
2020-05-21 12:47:58 +02:00
|
|
|
const search = permissions.filter(filterInexact(perm)).first(); //eslint-disable-line no-use-before-define
|
2020-05-17 15:07:39 +02:00
|
|
|
if(!search) continue;
|
2020-05-08 08:50:54 +02:00
|
|
|
if(search.type === 'module') {
|
|
|
|
for(const component of search.components.values()) {
|
2020-07-23 22:40:20 +02:00
|
|
|
if(component.type === 'command') parsedPermissions.push(component.resolveable);
|
2020-05-08 08:50:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-23 22:40:20 +02:00
|
|
|
parsedPermissions.push(search.resolveable);
|
2020-05-08 08:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 22:40:20 +02:00
|
|
|
if(parsedPermissions.length === 0) {
|
|
|
|
return message.respond(message.format('C_GRANT_MISSINGPERMISSIONNODES'), {
|
|
|
|
emoji: 'failure'
|
|
|
|
});
|
2020-05-08 08:50:54 +02:00
|
|
|
}
|
2020-07-23 22:40:20 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2020-05-08 08:50:54 +02:00
|
|
|
}
|
2020-07-23 22:40:20 +02:00
|
|
|
} else {
|
|
|
|
existing.channels[channel.id] = parsedPermissions;
|
2020-05-08 08:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-23 22:40:20 +02:00
|
|
|
} else {
|
|
|
|
for(const perm of parsedPermissions) {
|
|
|
|
if(existing.global.includes(perm)) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
existing.global.push(perm);
|
|
|
|
}
|
2020-05-08 08:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 22:40:20 +02:00
|
|
|
if(_permissions._id) delete _permissions._id; //some bullshit..
|
2020-05-08 08:50:54 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.client.transactionHandler.send({
|
|
|
|
provider: 'mongodb',
|
|
|
|
request: {
|
|
|
|
type: 'updateOne',
|
|
|
|
collection: 'permissions',
|
|
|
|
query: {
|
|
|
|
guildId: message.guild.id
|
|
|
|
},
|
|
|
|
data: _permissions
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch(error) {
|
2020-07-23 22:40:20 +02:00
|
|
|
this.client.logger.error(error);
|
2020-05-17 15:07:39 +02:00
|
|
|
await message.respond(message.format('C_GRANT_DATABASEERROR'), { emoji: 'failure' });
|
|
|
|
return undefined;
|
2020-05-08 08:50:54 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 22:40:20 +02:00
|
|
|
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' });
|
2020-05-08 08:50:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GrantCommand;
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
const filterInexact = (search) => (comp) => comp.id.toLowerCase().includes(search) ||
|
2020-05-08 08:50:54 +02:00
|
|
|
comp.resolveable.toLowerCase().includes(search) ||
|
2020-05-21 12:47:58 +02:00
|
|
|
comp.aliases && (comp.aliases.some((ali) => `${comp.type}:${ali}`.toLowerCase().includes(search)) ||
|
|
|
|
comp.aliases.some((ali) => ali.toLowerCase().includes(search)));
|