const { Command } = require('../../../../interfaces/'); const { GuildMember } = require('../../../../extensions/'); const { Emojis } = require('../../../../../util/'); class GrantCommand extends Command { constructor(client) { super(client, { name: 'grant', module: 'administration', usage: " ", 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 warning = false; let parsedPermissions = []; if(parameters.join(' ').toLowerCase() === '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') { if(component.module.id === 'administration') warning = true; parsedPermissions.push(component.resolveable); } } } else { if(search.module.id === 'administration') warning = true; 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.storageManager.mongodb.permissions.updateOne( { guildId: message.guild.id }, _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', embed: warning ? { author: { name: `${Emojis.warning} Warning` }, description: message.format('C_GRANT_WARNING'), color: 0xffe15c } : null }); } } 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)));