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

107 lines
3.5 KiB
JavaScript

const { Command } = require('../../../../interfaces/');
class DisableCommand extends Command {
constructor(client) {
super(client, {
name: 'enable',
module: 'administration',
usage: "<command..>",
examples: [
"strike",
"command:strike 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 }) {
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_ENABLE_MISSINGCOMMANDS'), {
emoji: 'failure'
});
}
if(args.global) {
if(!message.author.developer) return message.respond(message.format('C_ENABLE_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.enable())`);
const succeeded = results.filter((r) => r.some((r) => !r.error));
if(succeeded.length > 0) {
return message.respond(message.format('C_ENABLE_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_ENABLE_FAILGLOBAL', {
plural: parsed.length === 1 ? '' : 's',
commands: parsed.map((p) => `\`${p.resolveable}\``).join(', '),
total: results.length
}), { emoji: 'failure' });
}
const { disabledCommands } = message.guild._settings;
const removed = [];
for(const command of parsed) {
if(disabledCommands.includes(command.resolveable)) {
const index = disabledCommands.indexOf(command.resolveable);
if(index > -1) {
removed.push(command.resolveable);
disabledCommands.splice(index, 1);
}
}
}
await message.guild._updateSettings({
disabledCommands
});
if(removed.length === 0) {
message.respond(message.format('C_ENABLE_FAIL', {
plural: parsed.length === 1 ? '' : 's',
commands: parsed.map((p) => `\`${p.resolveable}\``).join(', ')
}), { emoji: 'failure' });
} else {
message.respond(message.format('C_ENABLE_SUCCESS', {
plural: parsed.length === 1 ? '' : 's',
commands: parsed.map((p) => `\`${p.resolveable}\``).join(', ')
}), { emoji: 'success' });
}
return undefined;
}
}
module.exports = DisableCommand;