From 34168eb832f265467620abe690ea0a902bf935f9 Mon Sep 17 00:00:00 2001 From: Programmix Date: Sat, 1 Oct 2016 12:29:15 -0700 Subject: [PATCH] Create MessageCollector.next (#761) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows using await with message collectors (ES7) Hydrabolt approved™ --- src/structures/MessageCollector.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/structures/MessageCollector.js b/src/structures/MessageCollector.js index 81956faa6..b7395dbb0 100644 --- a/src/structures/MessageCollector.js +++ b/src/structures/MessageCollector.js @@ -92,6 +92,34 @@ class MessageCollector extends EventEmitter { return false; } + /** + * Returns a promise that resolves when a valid message is sent. Rejects + * with collected messages if the Collector ends before receiving a message. + * @type {Promise} + * @readonly + */ + get next() { + return new Promise((resolve, reject) => { + const cleanup = () => { + this.removeListener(onMessage); + this.removeListener(onEnd); + }; + + const onMessage = (...args) => { + cleanup(); + resolve(...args); + }; + + const onEnd = (...args) => { + cleanup(); + reject(...args); + }; + + this.once('message', onMessage); + this.once('end', onEnd); + }); + } + /** * Stops the collector and emits `end`. * @param {string} [reason='user'] An optional reason for stopping the collector