From 423337a7f040ea6d2a07bdaa93507d54a8c05422 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Mon, 25 Apr 2022 14:08:41 +0300 Subject: [PATCH] command handler --- .../components/observers/CommandHandler.js | 83 ++++++++++++++----- 1 file changed, 60 insertions(+), 23 deletions(-) diff --git a/src/structure/components/observers/CommandHandler.js b/src/structure/components/observers/CommandHandler.js index a989dc6..95d3b68 100644 --- a/src/structure/components/observers/CommandHandler.js +++ b/src/structure/components/observers/CommandHandler.js @@ -22,13 +22,16 @@ class CommandHandler extends Observer { } - async messageCreate(message) { + async messageCreate(message) { if(!this.client._built || message.webhookId || message.author.bot || message.guild && !message.guild.available) return undefined; + const userWrapper = await this.client.getUserWrapper(message.author.id); + if (!userWrapper.developer) return; + if(message.guild) { if(!message.member) await message.guild.members.fetch(message.author.id); } @@ -42,10 +45,12 @@ class CommandHandler extends Observer { const inhibitors = await this._handleInhibitors(invoker); if (inhibitors.length) return this._generateError(invoker, { type: 'inhibitor', ...inhibitors[0] }); - const options = await this._parseMessage(invoker, parameters); + const response = await this._parseMessage(invoker, parameters); + // There was an error if _parseResponse return value is truthy, i.e. an error message was sent + if (await this._parseResponse(invoker, response)) return; // Temp setup - //this._executeCommand(invoker, parameters); + this._executeCommand(invoker, response.options); } @@ -58,7 +63,6 @@ class CommandHandler extends Observer { || interaction.guild && !interaction.guild.available) return undefined; const command = this._matchCommand(interaction.commandName); - // interaction.command = command; const invoker = new InvokerWrapper(this.client, interaction, command); if (!command) return invoker.reply({ @@ -70,6 +74,15 @@ class CommandHandler extends Observer { if(inhibitors.length) return this._generateError(invoker, { type: 'inhibitor', ...inhibitors[0] }); const response = await this._parseInteraction(interaction, command); + if (await this._parseResponse(invoker, response)) return; + + this.logger.info(`${interaction.user.tag} (${interaction.user.id}) is executing ${command.name}`); + await this._executeCommand(invoker, response.options); + + } + + _parseResponse(invoker, response) { + const { command } = invoker; if (response.error) { let content = invoker.format(`O_COMMANDHANDLER_TYPE${response.option.type}`, { option: response.option.name, min: response.option.minimum, max: response.option.maximum @@ -77,17 +90,13 @@ class CommandHandler extends Observer { if (response.dependency) content = invoker.format(`O_COMMANDHANDLER_DEPEDENCY`, { option: response.option.name, dependency: response.dependency }); return invoker.reply({ - content, - emoji: 'failure', + content, + emoji: 'failure', ephemeral: true }); - } else if (command.showUsage && !Object.keys(response.options).length) { - return invoker.reply({ embeds: [command.usageEmbed(invoker)] }); + } else if (command.showUsage && response.options && !Object.keys(response.options).length || response.showUsage) { + return invoker.reply({ embeds: [command.usageEmbed(invoker, response.verbose)] }); } - - this.logger.info(`${interaction.user.tag} (${interaction.user.id}) is executing ${command.name}`); - await this._executeCommand(invoker, response.options); - } async _handleInhibitors(invoker) { @@ -139,9 +148,10 @@ class CommandHandler extends Observer { const { subcommand } = interaction; - if(!interaction.guild && command.guildOnly) { - return interaction.reply({ index: 'O_COMMANDHANDLER_GUILDONLY', emoji: 'failure', ephemeral: true }); - } + // Handled by inhibitors -- cleanup later + // if(!interaction.guild && command.guildOnly) { + // return interaction.reply({ index: 'O_COMMANDHANDLER_GUILDONLY', emoji: 'failure', ephemeral: true }); + // } let error = null; const options = {}; @@ -158,12 +168,13 @@ class CommandHandler extends Observer { continue; } - const newOption = new CommandOption({ - name: matched.name, type: matched.type, - minimum: matched.minimum, maximum: matched.maximum, - _rawValue: option.value, - dependsOn: matched.dependsOn, dependsOnMode: matched.dependsOnMode - }); + // const newOption = new CommandOption({ + // name: matched.name, type: matched.type, + // minimum: matched.minimum, maximum: matched.maximum, + // _rawValue: option.value, + // dependsOn: matched.dependsOn, dependsOnMode: matched.dependsOnMode + // }); + const newOption = matched.clone(option.value); const parsed = await this._parseOption(interaction, newOption); if(parsed.error) { @@ -200,8 +211,34 @@ class CommandHandler extends Observer { async _parseMessage(invoker, params) { - const { options } = invoker.command; - console.log(options); + const { command, target: message } = invoker; + const { subcommands, subcommandGroups } = command; + const args = {}; + // console.log(options); + let group = null, + subcommand = null; + + if (subcommandGroups.length || subcommands.length) { + const [first, second, ...rest] = params; + group = command.subcommandGroup(first); + + // Depending on how thoroughly I want to support old style commands this might have to try and resolve to other options + // But for now I'm followin discord's structure for commands + if (!group) { + subcommand = command.subcommand(first)?.raw; + params = [second, ...rest]; + } else { + subcommand = command.subcommand(second)?.raw; + params = rest; + } + message.subcommand = subcommand; + message.subcommandGroup = group; + + if(!subcommand) return { showUsage: true, verbose: true }; + + } + + return { options: { args, parameters: params }, verbose: true }; }