From ed62fbfc431b89ad27c8d264fd86db788ba830e0 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sat, 15 Jan 2022 17:01:46 +0200 Subject: [PATCH] fixing up cmd handler to work with subcommands --- .../components/observers/CommandHandler.js | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/structure/components/observers/CommandHandler.js b/src/structure/components/observers/CommandHandler.js index 9b78c87..372d6ec 100644 --- a/src/structure/components/observers/CommandHandler.js +++ b/src/structure/components/observers/CommandHandler.js @@ -81,25 +81,34 @@ class CommandHandler extends Observer { } - async _parseInteraction(thing) { + /** + * @param {InteractionWrapper} interaction + */ + async _parseInteraction(interaction) { - const { command, interaction } = thing; + const { command, subcommand } = interaction; if(!interaction.guild && command.guildOnly) { - return thing.reply({ template: { index: 'O_COMMANDHANDLER_GUILDONLY' }, emoji: 'failure', ephemeral: true }); + return interaction.reply({ template: { index: 'O_COMMANDHANDLER_GUILDONLY' }, emoji: 'failure', ephemeral: true }); } let error = null; const options = {}; + // Find the option through the subcommand incase the subcommands have options with the same name + const _subcommand = command.subcommand(subcommand.name); // btw d.js already takes care of this, we don't need to do our own value parsing - // unless we want to add support for custom stuff like time, but that sounds a bit unnecessary - for(const option of interaction.options._hoistedOptions) { - const matched = command.options.find((o) => o.name === option.name); - if (!matched) continue; + // unless we want to add support for custom stuff + for (const option of interaction.optionsWithoutSubcommands) { // iterate through the received options + + const matched = _subcommand.options.find((o) => o.name === option.name); + if (!matched) { // Shouldn't occur but just in case + this.client.logger.warn(`Command interaction option mismatch:\nCommand: ${command.name}\nReceived subcommand ${subcommand.name}`); + continue; + } const newOption = new CommandOption({ name: matched.name, type: matched.type, minimum: matched.minimum, maximum: matched.maximum, _rawValue: option.value }); - const parsed = await this._parseOption(thing, newOption); + const parsed = await this._parseOption(interaction, newOption); if(parsed.error) { error = { option: newOption, @@ -192,9 +201,7 @@ class CommandHandler extends Observer { } }; - const result = types[option.type](option._rawValue); - if(result instanceof Promise) await result; - return result; + return types[option.type](option._rawValue); } async _getCommand(message) {