From 8946f9ab35b91c2dee234082d0df55b27683cb4b Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Mon, 1 Aug 2022 12:31:54 +0700 Subject: [PATCH] feat(Slash): Optional value --- src/structures/ApplicationCommand.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/structures/ApplicationCommand.js b/src/structures/ApplicationCommand.js index f147df6..aa87056 100644 --- a/src/structures/ApplicationCommand.js +++ b/src/structures/ApplicationCommand.js @@ -592,6 +592,7 @@ class ApplicationCommand extends Base { * @returns {Promise} */ async sendSlashCommand(message, options = []) { + // Todo: Refactor // Check Options if (!(message instanceof Message())) { throw new TypeError('The message must be a Discord.Message'); @@ -599,7 +600,7 @@ class ApplicationCommand extends Base { if (!Array.isArray(options)) { throw new TypeError('The options must be an array of strings'); } - if (this.type !== 'CHAT_INPUT') return false; + if (this.type !== 'CHAT_INPUT') throw new Error('This command is not a chat input [/]'); const optionFormat = []; const attachments = []; const attachmentsBuffer = []; @@ -627,10 +628,10 @@ class ApplicationCommand extends Base { option_[0] = { type: ApplicationCommandOptionTypes[subCommand.type], name: subCommand.name, - options: optionFormat, + options: optionFormat.filter(obj => obj.value), }; } else { - option_ = optionFormat; + option_ = optionFormat.filter(obj => obj.value); } // Autoresponse const getAutoResponse = async (options_, type, name, value) => { @@ -702,7 +703,7 @@ class ApplicationCommand extends Base { if (this.options[i].choices && this.options[i].choices.length > 0) { choice = this.options[i].choices.find(c => c.name == value) || this.options[i].choices.find(c => c.value == value); - if (!choice) { + if (!choice && value) { throw new Error( `Invalid option: ${value} is not a valid choice for this option\nList of choices:\n${this.options[ i @@ -716,7 +717,7 @@ class ApplicationCommand extends Base { type: ApplicationCommandOptionTypes[this.options[i].type], name: this.options[i].name, value: - choice?.value || + choice?.value || value == undefined ? undefined : (this.options[i].type == 'ATTACHMENT' ? await addDataFromAttachment(value) : this.options[i].type == 'INTEGER' @@ -742,7 +743,7 @@ class ApplicationCommand extends Base { choice = subCommand.options[i].choices.find(c => c.name == value) || subCommand.options[i].choices.find(c => c.value == value); - if (!choice) { + if (!choice && value) { throw new Error( `Invalid option: ${value} is not a valid choice for this option\nList of choices: \n${subCommand.options[ i @@ -756,7 +757,7 @@ class ApplicationCommand extends Base { type: ApplicationCommandOptionTypes[subCommand.options[i].type], name: subCommand.options[i].name, value: - choice?.value || + choice?.value || value == undefined ? undefined : (subCommand.options[i].type == 'ATTACHMENT' ? await addDataFromAttachment(value) : subCommand.options[i].type == 'INTEGER'