feat(Slash): Optional value

This commit is contained in:
March 7th 2022-08-01 12:31:54 +07:00
parent 4be5656f3d
commit 8946f9ab35

View File

@ -592,6 +592,7 @@ class ApplicationCommand extends Base {
* @returns {Promise<InteractionResponseBody>} * @returns {Promise<InteractionResponseBody>}
*/ */
async sendSlashCommand(message, options = []) { async sendSlashCommand(message, options = []) {
// Todo: Refactor
// Check Options // Check Options
if (!(message instanceof Message())) { if (!(message instanceof Message())) {
throw new TypeError('The message must be a Discord.Message'); throw new TypeError('The message must be a Discord.Message');
@ -599,7 +600,7 @@ class ApplicationCommand extends Base {
if (!Array.isArray(options)) { if (!Array.isArray(options)) {
throw new TypeError('The options must be an array of strings'); 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 optionFormat = [];
const attachments = []; const attachments = [];
const attachmentsBuffer = []; const attachmentsBuffer = [];
@ -627,10 +628,10 @@ class ApplicationCommand extends Base {
option_[0] = { option_[0] = {
type: ApplicationCommandOptionTypes[subCommand.type], type: ApplicationCommandOptionTypes[subCommand.type],
name: subCommand.name, name: subCommand.name,
options: optionFormat, options: optionFormat.filter(obj => obj.value),
}; };
} else { } else {
option_ = optionFormat; option_ = optionFormat.filter(obj => obj.value);
} }
// Autoresponse // Autoresponse
const getAutoResponse = async (options_, type, name, value) => { 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) { if (this.options[i].choices && this.options[i].choices.length > 0) {
choice = choice =
this.options[i].choices.find(c => c.name == value) || this.options[i].choices.find(c => c.value == value); 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( throw new Error(
`Invalid option: ${value} is not a valid choice for this option\nList of choices:\n${this.options[ `Invalid option: ${value} is not a valid choice for this option\nList of choices:\n${this.options[
i i
@ -716,7 +717,7 @@ class ApplicationCommand extends Base {
type: ApplicationCommandOptionTypes[this.options[i].type], type: ApplicationCommandOptionTypes[this.options[i].type],
name: this.options[i].name, name: this.options[i].name,
value: value:
choice?.value || choice?.value || value == undefined ? undefined :
(this.options[i].type == 'ATTACHMENT' (this.options[i].type == 'ATTACHMENT'
? await addDataFromAttachment(value) ? await addDataFromAttachment(value)
: this.options[i].type == 'INTEGER' : this.options[i].type == 'INTEGER'
@ -742,7 +743,7 @@ class ApplicationCommand extends Base {
choice = choice =
subCommand.options[i].choices.find(c => c.name == value) || subCommand.options[i].choices.find(c => c.name == value) ||
subCommand.options[i].choices.find(c => c.value == value); subCommand.options[i].choices.find(c => c.value == value);
if (!choice) { if (!choice && value) {
throw new Error( throw new Error(
`Invalid option: ${value} is not a valid choice for this option\nList of choices: \n${subCommand.options[ `Invalid option: ${value} is not a valid choice for this option\nList of choices: \n${subCommand.options[
i i
@ -756,7 +757,7 @@ class ApplicationCommand extends Base {
type: ApplicationCommandOptionTypes[subCommand.options[i].type], type: ApplicationCommandOptionTypes[subCommand.options[i].type],
name: subCommand.options[i].name, name: subCommand.options[i].name,
value: value:
choice?.value || choice?.value || value == undefined ? undefined :
(subCommand.options[i].type == 'ATTACHMENT' (subCommand.options[i].type == 'ATTACHMENT'
? await addDataFromAttachment(value) ? await addDataFromAttachment(value)
: subCommand.options[i].type == 'INTEGER' : subCommand.options[i].type == 'INTEGER'