diff --git a/src/structure/components/commands/administration/Commands.js b/src/structure/components/commands/administration/Commands.js deleted file mode 100644 index 987b7f5..0000000 --- a/src/structure/components/commands/administration/Commands.js +++ /dev/null @@ -1,106 +0,0 @@ -const { Emojis } = require("../../../../constants"); -const { SlashCommand, CommandError } = require("../../../interfaces"); - -class Commands extends SlashCommand { - - // Used to disable and enable commands, possibly list them? - constructor(client) { - super(client, { - name: 'commands', - 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'], - guildOnly: true - }); - } - - 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 diff --git a/src/structure/components/commands/information/Commands.js b/src/structure/components/commands/information/Commands.js new file mode 100644 index 0000000..da7b15f --- /dev/null +++ b/src/structure/components/commands/information/Commands.js @@ -0,0 +1,61 @@ +const { Emojis } = require("../../../../constants"); +const { SlashCommand } = require("../../../interfaces"); + +class Commands extends SlashCommand { + + // Used to disable and enable commands, possibly list them? + constructor(client) { + super(client, { + name: 'commands', + module: 'information', + description: 'List commands', + options: [{ + name: ['module'], + type: 'MODULE', + description: ['List commands from a specific module'] + }], + memberPermissions: ['MANAGE_GUILD'], + guildOnly: true + }); + } + + async execute(invoker, { module }) { + + const { guild } = invoker; + + const settings = await guild.settings(); + const { commands: { disabled } } = settings; + + let commands = null; + if (module) commands = module.value.components.filter((c) => c._type === 'command'); + // eslint-disable-next-line prefer-destructuring + else commands = this.client.registry.commands; + + const modules = {}; + for (const command of commands.values()) { + if (command.restricted) continue; + const _module = command.module.name; + if (!modules[_module]) modules[_module] = []; + const emoji = disabled.includes(command.resolveable) ? Emojis.failure : Emojis.success; + modules[_module].push(`${emoji} ${command.name}`); + } + + const embed = { + title: guild.format('COMMAND_COMMANDS_TITLE'), + fields: [] + }; + for (const [module, commands] of Object.entries(modules).sort(([, a], [, b]) => a.length - b.length)) { + embed.fields.push({ + name: module, + value: commands.join('\n'), + inline: true + }); + } + + return invoker.reply({ embed }); + + } + +} + +module.exports = Commands; \ No newline at end of file diff --git a/src/structure/components/settings/administration/Commands.js b/src/structure/components/settings/administration/Commands.js new file mode 100644 index 0000000..17b5eb8 --- /dev/null +++ b/src/structure/components/settings/administration/Commands.js @@ -0,0 +1,69 @@ +const { Setting } = require("../../../interfaces"); + +class CommandsSetting extends Setting { + + constructor(client) { + super(client, { + name: 'commands', + module: 'administration', + description: 'Manage commands', + display: 'Commands', + default: { + disabled: [], + custom: {} + }, + definitions: { + disabled: { ARRAY: 'COMMAND' }, + custom: { OBJECT: 'CUSTOM_COMMAND' } + }, + commandType: 'SUB_COMMAND_GROUP', + commandOptions: [{ + name: ['enable', 'disable'], + description: ['Enable commands', 'Disable commands'], + type: 'SUB_COMMAND', + options: [{ + name: 'commands', + description: 'The command to enable/disable', + type: 'COMMANDS', + required: true + }] + }, { + name: 'list', + description: 'List disabled commands', + type: 'SUB_COMMAND' + }] + }); + } + + async execute(invoker, { commands }, setting) { + + const { subcommand, guild } = invoker; + let warning = null; + if (subcommand.name === 'enable') { + if (!setting.disabled.length) return { error: true, index: 'SETTING_COMMANDS_NONE' }; + for (const command of commands.value) { + const index = setting.disabled.indexOf(command.resolveable); + if(index >= 0) setting.disabled.splice(index, 1); + } + } else if (subcommand.name === 'disable') { + for (const command of commands.value) { + if (command.module.name === 'administration') { + warning = guild.format('SETTING_COMMAND_WARNING'); + continue; + } + if(!setting.disabled.includes(command.resolveable)) setting.disabled.push(command.resolveable); + } + if(warning) return { content: warning }; + } else if (subcommand.name === 'list') { + let content = null; + if (setting.disabled.length) content = setting.disabled.join(', '); + else content = guild.format('SETTING_COMMANDS_NONE'); + return { content }; + } + return { index: 'SETTING_SUCCESS_ALT' }; + + } + +} + +module.exports = CommandsSetting; \ No newline at end of file