fixing up cmd handler to work with subcommands

This commit is contained in:
Erik 2022-01-15 17:01:46 +02:00
parent 9ea73581c0
commit ed62fbfc43
No known key found for this signature in database
GPG Key ID: FEFF4B220DDF5589

View File

@ -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) { 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; let error = null;
const options = {}; 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 // 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 // unless we want to add support for custom stuff
for(const option of interaction.options._hoistedOptions) { for (const option of interaction.optionsWithoutSubcommands) { // iterate through the received options
const matched = command.options.find((o) => o.name === option.name);
if (!matched) continue; 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 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) { if(parsed.error) {
error = { error = {
option: newOption, option: newOption,
@ -192,9 +201,7 @@ class CommandHandler extends Observer {
} }
}; };
const result = types[option.type](option._rawValue); return types[option.type](option._rawValue);
if(result instanceof Promise) await result;
return result;
} }
async _getCommand(message) { async _getCommand(message) {