Skip to content

Commit

Permalink
Merge pull request #5 from ahnolds/isPromise
Browse files Browse the repository at this point in the history
Use is-promise to check if objects are Promises
  • Loading branch information
danbulant authored Mar 23, 2022
2 parents 2b85d78 + 863a3a1 commit a4d6c28
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@discordjs/rest": "^0.1.0-canary.0",
"common-tags": "^1.8.0",
"discord-api-types": "^0.24.0",
"is-promise": "4.0.0",
"require-all": "^3.0.0"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/argument.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { escapeMarkdown } = require('../util');
const { oneLine, stripIndents } = require('common-tags');
const isPromise = require('is-promise');
const ArgumentUnionType = require('../types/union');

/** A fancy argument */
Expand Down Expand Up @@ -392,7 +393,7 @@ class Argument {
validate(val, msg, vmsg) {
const valid = this.validator ? this.validator(val, msg, this, vmsg) : this.type.validate(val, msg, this, vmsg);
if(!valid || typeof valid === 'string') return this.error || valid;
if(valid instanceof Promise) return valid.then(vld => !vld || typeof vld === 'string' ? this.error || vld : vld);
if(isPromise(valid)) return valid.then(vld => !vld || typeof vld === 'string' ? this.error || vld : vld);
return valid;
}

Expand Down
3 changes: 2 additions & 1 deletion src/dispatcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Context = require('./extensions/context');
const { Message } = require('discord.js');
const { escapeRegex } = require('./util');
const isPromise = require('is-promise');

/** Handles parsing messages and running commands from them */
class CommandDispatcher {
Expand Down Expand Up @@ -203,7 +204,7 @@ class CommandDispatcher {
const valid = typeof inhibit.reason === 'string' && (
typeof inhibit.response === 'undefined' ||
inhibit.response === null ||
inhibit.response instanceof Promise ||
isPromise(inhibit.response) ||
inhibit.response instanceof Message
);
if(!valid) {
Expand Down

0 comments on commit a4d6c28

Please sign in to comment.