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