Skip to content
This repository has been archived by the owner on Dec 24, 2021. It is now read-only.

Instead of testing trigger message, test latest response #367

Merged
merged 4 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/commands/argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ class Argument {
};
}

empty = this.isEmpty(val, msg);
valid = await this.validate(val, msg);
empty = this.isEmpty(val, msg, responses.first());
valid = await this.validate(val, msg, responses.first());
/* eslint-enable no-await-in-loop */
}

return {
value: await this.parse(val, msg),
value: await this.parse(val, msg, answers.length ? answers[answers.length - 1] : msg),
cancelled: null,
prompts,
answers
Expand Down Expand Up @@ -325,10 +325,10 @@ class Argument {
};
}

valid = await this.validate(val, msg);
valid = await this.validate(val, msg, responses.first());
}

results.push(await this.parse(val, msg));
results.push(await this.parse(val, msg, answers.length ? answers[answers.length - 1] : msg));

if(vals) {
currentVal++;
Expand All @@ -348,11 +348,14 @@ class Argument {
/**
* Checks if a value is valid for the argument
* @param {string} val - Value to check
* @param {CommandoMessage} msg - Message that triggered the command
* @param {CommandoMessage} originalMsg - Message that triggered the command
* @param {?CommandoMessage} [currentMsg=originalMsg] - Current response message
* @return {boolean|string|Promise<boolean|string>}
*/
validate(val, msg) {
const valid = this.validator ? this.validator(val, msg, this) : this.type.validate(val, msg, this);
validate(val, originalMsg, currentMsg = originalMsg) {
const valid = this.validator ?
this.validator(val, originalMsg, this, currentMsg) :
this.type.validate(val, originalMsg, this, currentMsg);
if(!valid || typeof valid === 'string') return this.error || valid;
if(isPromise(valid)) return valid.then(vld => !vld || typeof vld === 'string' ? this.error || vld : vld);
return valid;
Expand All @@ -361,23 +364,25 @@ class Argument {
/**
* Parses a value string into a proper value for the argument
* @param {string} val - Value to parse
* @param {CommandoMessage} msg - Message that triggered the command
* @param {CommandoMessage} originalMsg - Message that triggered the command
* @param {?CommandoMessage} [currentMsg=originalMsg] - Current response message
* @return {*|Promise<*>}
*/
parse(val, msg) {
if(this.parser) return this.parser(val, msg, this);
return this.type.parse(val, msg, this);
parse(val, originalMsg, currentMsg = originalMsg) {
if(this.parser) return this.parser(val, originalMsg, this, currentMsg);
return this.type.parse(val, originalMsg, this, currentMsg);
}

/**
* Checks whether a value for the argument is considered to be empty
* @param {string} val - Value to check for emptiness
* @param {CommandoMessage} msg - Message that triggered the command
* @param {CommandoMessage} originalMsg - Message that triggered the command
* @param {?CommandoMessage} [currentMsg=originalMsg] - Current response message
* @return {boolean}
*/
isEmpty(val, msg) {
if(this.emptyChecker) return this.emptyChecker(val, msg, this);
if(this.type) return this.type.isEmpty(val, msg, this);
isEmpty(val, originalMsg, currentMsg = originalMsg) {
if(this.emptyChecker) return this.emptyChecker(val, originalMsg, this, currentMsg);
if(this.type) return this.type.isEmpty(val, originalMsg, this, currentMsg);
if(Array.isArray(val)) return val.length === 0;
return !val;
}
Expand Down
15 changes: 9 additions & 6 deletions src/types/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,40 @@ class ArgumentType {
/**
* Validates a value string against the type
* @param {string} val - Value to validate
* @param {CommandoMessage} msg - Message the value was obtained from
* @param {CommandoMessage} originalMsg - Message that triggered the command
* @param {Argument} arg - Argument the value was obtained from
* @param {?CommandoMessage} [currentMsg=originalMsg] - Current response message
* @return {boolean|string|Promise<boolean|string>} Whether the value is valid, or an error message
* @abstract
*/
validate(val, msg, arg) { // eslint-disable-line no-unused-vars
validate(val, originalMsg, arg, currentMsg = originalMsg) { // eslint-disable-line no-unused-vars
throw new Error(`${this.constructor.name} doesn't have a validate() method.`);
}

// eslint-disable-next-line valid-jsdoc
/**
* Parses the raw value string into a usable value
* @param {string} val - Value to parse
* @param {CommandoMessage} msg - Message the value was obtained from
* @param {CommandoMessage} originalMsg - Message that triggered the command
* @param {Argument} arg - Argument the value was obtained from
* @param {?CommandoMessage} [currentMsg=originalMsg] - Current response message
* @return {*|Promise<*>} Usable value
* @abstract
*/
parse(val, msg, arg) { // eslint-disable-line no-unused-vars
parse(val, originalMsg, arg, currentMsg = originalMsg) { // eslint-disable-line no-unused-vars
throw new Error(`${this.constructor.name} doesn't have a parse() method.`);
}

/**
* Checks whether a value is considered to be empty. This determines whether the default value for an argument
* should be used and changes the response to the user under certain circumstances.
* @param {string} val - Value to check for emptiness
* @param {CommandoMessage} msg - Message the value was obtained from
* @param {CommandoMessage} originalMsg - Message that triggered the command
* @param {Argument} arg - Argument the value was obtained from
* @param {?CommandoMessage} [currentMsg=originalMsg] - Current response message
* @return {boolean} Whether the value is empty
*/
isEmpty(val, msg, arg) { // eslint-disable-line no-unused-vars
isEmpty(val, originalMsg, arg, currentMsg = originalMsg) { // eslint-disable-line no-unused-vars
if(Array.isArray(val)) return val.length === 0;
return !val;
}
Expand Down