galactic-bot/structure/client/components/commands/administration/Disable.js
2020-07-27 20:00:24 -07:00

131 lines
4.6 KiB
JavaScript

const { Command } = require('../../../../interfaces/');
const { Emojis } = require('../../../../../util/');
class DisableCommand extends Command {
constructor(client) {
super(client, {
name: 'disable',
module: 'administration',
usage: "<command..|'list'>",
examples: [
"snipe",
"command:snipe command:note"
],
memberPermissions: ['ADMINISTRATOR', 'MANAGE_GUILD'],
showUsage: true,
guildOnly: true,
arguments: [
{
name: 'global',
type: 'BOOLEAN',
types: [
'FLAG'
],
default: true,
archivable: false
}
]
});
}
async execute(message, { params, args }) {
if(params.join(' ').toLowerCase() === 'list') {
const disabled = message.guild._settings.disabledCommands;
if(disabled.length > 0) {
message.respond(message.format('C_DISABLE_LIST', {
plural: disabled.length === 1 ? '' : 's',
commands: disabled.map((d) => `\`${d}\``).join(', ')
}), { emoji: 'success' });
} else {
message.respond(message.format('C_DISABLE_LISTFAIL'), { emoji: 'success' });
}
return undefined;
}
const { parsed } = await this.client.resolver.infinite(params, [
this.client.resolver.resolveComponent.bind(this.client.resolver)
], true, 'command');
if(parsed.length === 0) {
return message.respond(message.format('C_DISABLE_MISSINGCOMMANDS'), {
emoji: 'failure'
});
}
if(args.global) {
if(!message.author.developer) return message.respond(message.format('C_DISABLE_PERMISSIONSREQUIRED'), {
emoji: 'failure'
});
const results = await this.client.shard.broadcastEval(`this.registry.components
.filter((c) => [${parsed.map((p) => `"${p.resolveable}"`).join(', ')}].includes(c.resolveable))
.map((c) => c.disable())`);
const succeeded = results.filter((r) => r.some((r) => !r.error));
if(succeeded.length > 0) {
return message.respond(message.format('C_DISABLE_SUCCESSGLOBAL', {
plural: parsed.length === 1 ? '' : 's',
commands: parsed.map((p) => `\`${p.resolveable}\``).join(', '),
amount: succeeded.length,
total: results.length
}), { emoji: 'success' });
}
return message.respond(message.format('C_DISABLE_FAILGLOBAL', {
plural: parsed.length === 1 ? '' : 's',
commands: parsed.map((p) => `\`${p.resolveable}\``).join(', '),
total: results.length
}), { emoji: 'failure' });
}
const { disabledCommands } = message.guild._settings;
let warning = false,
commands = []; //eslint-disable-line prefer-const
for(const command of parsed) {
if(command.module.id === 'administration') {
if(['command:enable', 'command:disable'].includes(command.resolveable)) continue;
warning = true;
}
commands.push(command.resolveable);
if(disabledCommands.includes(command.resolveable)) continue;
else disabledCommands.push(command.resolveable);
}
await message.guild._updateSettings({
disabledCommands
});
if(commands.length === 0) {
message.respond(message.format('C_DISABLE_FAIL', {
plural: parsed.length === 1 ? '' : 's',
commands: parsed.map((p) => `\`${p.resolveable}\``).join(', ')
}), { emoji: 'failure' });
} else {
message.respond(message.format('C_DISABLE_SUCCESS', {
plural: parsed.length === 1 ? '' : 's',
commands: parsed.map((p) => `\`${p.resolveable}\``).join(', ')
}), {
emoji: 'success',
embed: warning ? {
author: {
name: `${Emojis.warning} Warning`
},
description: message.format('C_DISABLE_WARNING'),
color: 0xffe15c
} : null
});
}
return undefined;
}
}
module.exports = DisableCommand;