Add awaitMessages

This commit is contained in:
Amish Shah
2016-08-31 21:13:33 +01:00
parent 91b1fa8359
commit ff3148ddd4
5 changed files with 44 additions and 1 deletions

File diff suppressed because one or more lines are too long

View File

@@ -74,6 +74,10 @@ class DMChannel extends Channel {
createCollector() {
return;
}
awaitMessages() {
return;
}
}
TextBasedChannel.applyToClass(DMChannel, true);

View File

@@ -151,6 +151,10 @@ class GroupDMChannel extends Channel {
createCollector() {
return;
}
awaitMessages() {
return;
}
}
TextBasedChannel.applyToClass(GroupDMChannel, true);

View File

@@ -59,6 +59,10 @@ class TextChannel extends GuildChannel {
createCollector() {
return;
}
awaitMessages() {
return;
}
}
TextBasedChannel.applyToClass(TextChannel, true);

View File

@@ -288,6 +288,36 @@ class TextBasedChannel {
return collector;
}
/**
* An object containing the same properties as CollectorOptions, but a few more:
* ```js
* {
* errors: [], // an array of stop/end reasons that cause the promise to reject.
* }
* ```
* @typedef {Object} AwaitMessagesOptions
*/
/**
* Similar to createCollector but in Promise form. Resolves with a Collection of messages that pass the specified
* filter.
* @param {CollectorFilterFunction} filter the filter function to use
* @param {AwaitMessagesOptions} [options={}] optional options to pass to the internal collector
* @returns {Promise<Collection<String, Message>>}
*/
awaitMessages(filter, options = {}) {
return new Promise((resolve, reject) => {
const collector = this.createCollector(filter, options);
collector.on('end', (collection, reason) => {
if (options.errors && options.errors.includes(reason)) {
reject(collection);
} else {
resolve(collection);
}
});
});
}
_cacheMessage(message) {
const maxSize = this.client.options.max_message_cache;
if (maxSize === 0) {
@@ -338,6 +368,7 @@ exports.applyToClass = (structure, full = false) => {
props.push('setTyping');
props.push('fetchPinnedMessages');
props.push('createCollector');
props.push('awaitMessages');
}
for (const prop of props) {
applyProp(structure, prop);