From 17d3fb5e227adeca6f448aa615562fa8c5ec87a7 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Wed, 20 Apr 2022 15:59:10 +0300 Subject: [PATCH] enable/disable commands --- .../commands/administration/Commands.js | 94 ++++++++++++++++++- 1 file changed, 90 insertions(+), 4 deletions(-) diff --git a/src/structure/components/commands/administration/Commands.js b/src/structure/components/commands/administration/Commands.js index 79adc37..1f38646 100644 --- a/src/structure/components/commands/administration/Commands.js +++ b/src/structure/components/commands/administration/Commands.js @@ -1,4 +1,5 @@ -const { SlashCommand } = require("../../../interfaces"); +const { Emojis } = require("../../../../constants"); +const { SlashCommand, CommandError } = require("../../../interfaces"); class Commands extends SlashCommand { @@ -6,14 +7,99 @@ class Commands extends SlashCommand { constructor(client) { super(client, { name: 'commands', - module: 'administration' + module: 'administration', + description: 'Manage commands', + options: [{ + name: ['disable', 'enable'], + type: 'SUB_COMMAND', + description: ['Disable commands', 'Enable commands'], + options: [{ + name: 'commands', + type: 'STRING', + description: '' + } ] + }, { + name: ['list'], + type: 'SUB_COMMAND', + description: ['List disabled commands'], + options: [] + }], + memberPermissions: ['MANAGE_GUILD'] }); } - execute(interaction, opts) { + async execute(interaction, { commands }) { + + const { subcommand, guild } = interaction; + + const settings = await guild.settings(); + const { disabledCommands = [] } = settings; + + if (subcommand.name === 'list') return this._listDisabledCommands(interaction, disabledCommands); + + if (!commands) throw new CommandError(interaction, { index: 'COMMAND_COMMANDS_MISSING_ARG' }); + + const resolved = [], + invalid = [], + alreadyDisabled = []; + let warning = false; + for (const _cmd of commands.value.split(' ')) { + const [cmd] = this.client.resolver.components(_cmd, 'command'); + if (!cmd) { + invalid.push(_cmd); + continue; + } + + if (resolved.includes(cmd.resolveable) || cmd.resolveable === 'command:commands') continue; + if (disabledCommands.includes(cmd.resolveable) && subcommand.name === 'disable') { + alreadyDisabled.push(cmd.resolveable); + continue; + } + + if (cmd.module.name === 'administration' && subcommand.name === 'disable') warning = true; + + resolved.push(cmd.resolveable); + } + + if (!resolved.length) { + let response = null; + if (alreadyDisabled.length) response = interaction.format('COMMAND_COMMANDS_ALREADY_DISABLED', { alreadyDisabled: alreadyDisabled.join('`, `') }); + else response = interaction.format('COMMAND_COMMANDS_NO_RESOLVED'); + if (invalid.length) response += '\n\n' + interaction.format('COMMAND_COMMANDS_INVALID', { invalid: invalid.join('`, `') }); + return { content: response, emoji: 'failure' }; + } + + if (subcommand.name === 'disable') { + disabledCommands.push(...resolved); + } else if (subcommand.name === 'enable') { + for (const disabled of resolved) { + const index = disabledCommands.indexOf(disabled); + if(index > -1) disabledCommands.splice(index, 1); + } + } + + await guild.updateSettings({ disabledCommands }); + + let response = interaction.format(`COMMAND_COMMANDS_${subcommand.name.toUpperCase()}`, { resolved: resolved.join(`**, **`) }); + if (alreadyDisabled.length) response += '\n\n' + interaction.format('COMMAND_COMMANDS_ALREADY_DISABLED', { alreadyDisabled: alreadyDisabled.join('`, `') }); + if (invalid.length) response += '\n\n' + interaction.format('COMMAND_COMMANDS_INVALID', { invalid: invalid.join('`, `') }); + + return { + content: response, emoji: 'success', embeds: warning ? [{ + title: `${Emojis.warning} WARNING`, + color: 0xffe15c, + description: interaction.format('COMMAND_COMMANDS_DISABLE_WARN') + }] : undefined }; + + } + + async _listDisabledCommands(interaction, commands) { + + if (!commands?.length) return { index: 'COMMAND_COMMANDS_NONE_DISABLED' }; + return { content: interaction.format('COMMAND_COMMANDS_LIST', { commands: commands.join('`, `') }) }; } } -//module.exports = Commands; \ No newline at end of file +module.exports = Commands; \ No newline at end of file