Add message reaction collectors & abstract collectors (#1335)

* message reaction collectors

* docs cleanup

* abstraction

* remove pointless method

* rename reaction collector creator method

* docs and stuff

* fix docs & build

* backwards compatibility, fix docs

* fix docs

* remove deprecated comments

* betterer docs again

* Fix documentation

* Fix Alias to not break depreciated code
This commit is contained in:
Will Nelson
2017-04-19 09:52:40 -07:00
committed by Crawl
parent 8475a4abee
commit ca34c43ba0
10 changed files with 345 additions and 135 deletions

View File

@@ -1,5 +1,5 @@
const Channel = require('./Channel'); const Channel = require('./Channel');
const TextBasedChannel = require('./interface/TextBasedChannel'); const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
/** /**

View File

@@ -1,5 +1,5 @@
const Channel = require('./Channel'); const Channel = require('./Channel');
const TextBasedChannel = require('./interface/TextBasedChannel'); const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
/* /*

View File

@@ -1,4 +1,4 @@
const TextBasedChannel = require('./interface/TextBasedChannel'); const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Role = require('./Role'); const Role = require('./Role');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');

View File

@@ -2,6 +2,7 @@ const Mentions = require('./MessageMentions');
const Attachment = require('./MessageAttachment'); const Attachment = require('./MessageAttachment');
const Embed = require('./MessageEmbed'); const Embed = require('./MessageEmbed');
const MessageReaction = require('./MessageReaction'); const MessageReaction = require('./MessageReaction');
const ReactionCollector = require('./ReactionCollector');
const Util = require('../util/Util'); const Util = require('../util/Util');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const Constants = require('../util/Constants'); const Constants = require('../util/Constants');
@@ -245,6 +246,47 @@ class Message {
}); });
} }
/**
* Creates a reaction collector.
* @param {CollectorFilter} filter The filter to apply.
* @param {ReactionCollectorOptions} [options={}] Options to send to the collector.
* @returns {ReactionCollector}
* @example
* // create a reaction collector
* const collector = message.createReactionCollector(
* (reaction, user) => reaction.emoji.id === '👌' && user.id === 'someID',
* { time: 15000 }
* );
* collector.on('collect', r => console.log(`Collected ${r.emoji.name}`));
* collector.on('end', collected => console.log(`Collected ${collected.size} items`));
*/
createReactionCollector(filter, options = {}) {
return new ReactionCollector(this, filter, options);
}
/**
* An object containing the same properties as CollectorOptions, but a few more:
* @typedef {ReactionCollectorOptions} AwaitReactionsOptions
* @property {string[]} [errors] Stop/end reasons that cause the promise to reject
*/
/**
* Similar to createCollector but in promise form. Resolves with a collection of reactions that pass the specified
* filter.
* @param {CollectorFilter} filter The filter function to use
* @param {AwaitReactionsOptions} [options={}] Optional options to pass to the internal collector
* @returns {Promise<Collection<string, MessageReaction>>}
*/
awaitReactions(filter, options = {}) {
return new Promise((resolve, reject) => {
const collector = this.createReactionCollector(filter, options);
collector.once('end', (reactions, reason) => {
if (options.errors && options.errors.includes(reason)) reject(reactions);
else resolve(reactions);
});
});
}
/** /**
* An array of cached versions of the message, including the current version. * An array of cached versions of the message, including the current version.
* Sorted from latest (first) to oldest (last). * Sorted from latest (first) to oldest (last).

View File

@@ -1,150 +1,73 @@
const EventEmitter = require('events').EventEmitter; const Collector = require('./interfaces/Collector');
const Collection = require('../util/Collection');
/** /**
* Collects messages based on a specified filter, then emits them. * @typedef {CollectorOptions} MessageCollectorOptions
* @extends {EventEmitter} * @property {number} max The maximum amount of messages to process.
* @property {number} maxMatches The maximum amount of messages to collect.
*/ */
class MessageCollector extends EventEmitter {
/** /**
* A function that takes a Message object and a MessageCollector and returns a boolean. * Collects messages on a channel.
* ```js * @implements {Collector}
* function(message, collector) { */
* if (message.content.includes('discord')) { class MessageCollector extends Collector {
* return true; // passed the filter test
* }
* return false; // failed the filter test
* }
* ```
* @typedef {Function} CollectorFilterFunction
*/
/** /**
* An object containing options used to configure a MessageCollector. All properties are optional. * @param {TextBasedChannel} channel The channel.
* @typedef {Object} CollectorOptions * @param {CollectorFilter} filter The filter to be applied to this collector.
* @property {number} [time] Duration for the collector in milliseconds * @param {MessageCollectorOptions} options The options to be applied to this collector.
* @property {number} [max] Maximum number of messages to handle * @emits MessageCollector#message
* @property {number} [maxMatches] Maximum number of successfully filtered messages to obtain
*/
/**
* @param {Channel} channel The channel to collect messages in
* @param {CollectorFilterFunction} filter The filter function
* @param {CollectorOptions} [options] Options for the collector
*/ */
constructor(channel, filter, options = {}) { constructor(channel, filter, options = {}) {
super(); super(channel.client, filter, options);
/** /**
* The channel this collector is operating on * @type {TextBasedChannel} channel The channel.
* @type {Channel}
*/ */
this.channel = channel; this.channel = channel;
/** /**
* A function used to filter messages that the collector collects. * @type {number} received Total number of messages that were received in the
* @type {CollectorFilterFunction} * channel during message collection.
*/ */
this.filter = filter; this.received = 0;
/** this.client.on('message', this.listener);
* Options for the collecor.
* @type {CollectorOptions}
*/
this.options = options;
/** // For backwards compatibility (remove in v12).
* Whether this collector has stopped collecting messages. if (this.options.max) this.options.maxProcessed = this.options.max;
* @type {boolean} if (this.options.maxMatches) this.options.max = this.options.maxMatches;
*/ this._reEmitter = message => {
this.ended = false;
/**
* A collection of collected messages, mapped by message ID.
* @type {Collection<Snowflake, Message>}
*/
this.collected = new Collection();
this.listener = message => this.verify(message);
this.channel.client.on('message', this.listener);
if (options.time) this.channel.client.setTimeout(() => this.stop('time'), options.time);
}
/**
* Verifies a message against the filter and options
* @private
* @param {Message} message The message
* @returns {boolean}
*/
verify(message) {
if (this.channel ? this.channel.id !== message.channel.id : false) return false;
if (this.filter(message, this)) {
this.collected.set(message.id, message);
/** /**
* Emitted whenever the collector receives a message that passes the filter test. * Emitted when the collector receives a message.
* @param {Message} message The received message
* @param {MessageCollector} collector The collector the message passed through
* @event MessageCollector#message * @event MessageCollector#message
* @param {Message} message The message.
* @deprecated
*/ */
this.emit('message', message, this); this.emit('message', message);
if (this.collected.size >= this.options.maxMatches) this.stop('matchesLimit'); };
else if (this.options.max && this.collected.size === this.options.max) this.stop('limit'); this.on('collect', this._reEmitter);
return true;
}
return false;
} }
/** handle(message) {
* Returns a promise that resolves when a valid message is sent. Rejects if (message.channel.id !== this.channel.id) return null;
* with collected messages if the Collector ends before receiving a message. this.received++;
* @type {Promise<Message>} return {
* @readonly key: message.id,
*/ value: message,
get next() { };
return new Promise((resolve, reject) => {
if (this.ended) {
reject(this.collected);
return;
}
const cleanup = () => {
this.removeListener('message', onMessage);
this.removeListener('end', onEnd);
};
const onMessage = (...args) => {
cleanup();
resolve(...args);
};
const onEnd = (...args) => {
cleanup();
reject(...args); // eslint-disable-line prefer-promise-reject-errors
};
this.once('message', onMessage);
this.once('end', onEnd);
});
} }
/** postCheck() {
* Stops the collector and emits `end`. // Consider changing the end reasons for v12
* @param {string} [reason='user'] An optional reason for stopping the collector if (this.options.maxMatches && this.collected.size >= this.options.max) return 'matchesLimit';
*/ if (this.options.max && this.received >= this.options.maxProcessed) return 'limit';
stop(reason = 'user') { return null;
if (this.ended) return; }
this.ended = true;
this.channel.client.removeListener('message', this.listener); cleanup() {
/** this.removeListener('collect', this._reEmitter);
* Emitted when the Collector stops collecting. this.client.removeListener('message', this.listener);
* @param {Collection<Snowflake, Message>} collection A collection of messages collected
* during the lifetime of the collector, mapped by the ID of the messages.
* @param {string} reason The reason for the end of the collector. If it ended because it reached the specified time
* limit, this would be `time`. If you invoke `.stop()` without specifying a reason, this would be `user`. If it
* ended because it reached its message limit, it will be `limit`.
* @event MessageCollector#end
*/
this.emit('end', this.collected, reason);
} }
} }

View File

@@ -0,0 +1,64 @@
const Collector = require('./interfaces/Collector');
const Collection = require('../util/Collection');
/**
* @typedef {CollectorOptions} ReactionCollectorOptions
* @property {number} max The maximum total amount of reactions to collect.
* @property {number} maxEmojis The maximum number of emojis to collect.
* @property {number} maxUsers The maximum number of users to react.
*/
/**
* Collects reactions on messages.
* @implements {Collector}
*/
class ReactionCollector extends Collector {
/**
* @param {Message} message The message upon which to collect reactions.
* @param {CollectorFilter} filter The filter to apply to this collector.
* @param {ReactionCollectorOptions} [options={}] The options to apply to this collector.
*/
constructor(message, filter, options = {}) {
super(message.client, filter, options);
/**
* @type {Message} message The message.
*/
this.message = message;
/**
* @type {Collection} users Users which have reacted.
*/
this.users = new Collection();
/**
* @type {number} total Total number of reactions collected.
*/
this.total = 0;
this.client.on('messageReactionAdd', this.listener);
}
handle(reaction) {
if (reaction.message.id !== this.message.id) return null;
return {
key: reaction.emoji.id || reaction.emoji.name,
value: reaction,
};
}
postCheck(reaction, user) {
this.users.set(user.id, user);
if (this.options.max && ++this.total >= this.options.max) return 'limit';
if (this.options.maxEmojis && this.collected.size >= this.options.maxEmojis) return 'emojiLimit';
if (this.options.maxUsers && this.users.size >= this.options.maxUsers) return 'userLimit';
return null;
}
cleanup() {
this.client.removeListener('messageReactionAdd', this.listener);
}
}
module.exports = ReactionCollector;

View File

@@ -1,5 +1,5 @@
const GuildChannel = require('./GuildChannel'); const GuildChannel = require('./GuildChannel');
const TextBasedChannel = require('./interface/TextBasedChannel'); const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
/** /**
@@ -89,6 +89,7 @@ class TextChannel extends GuildChannel {
get typing() {} get typing() {}
get typingCount() {} get typingCount() {}
createCollector() {} createCollector() {}
createMessageCollector() {}
awaitMessages() {} awaitMessages() {}
bulkDelete() {} bulkDelete() {}
acknowledge() {} acknowledge() {}

View File

@@ -1,4 +1,4 @@
const TextBasedChannel = require('./interface/TextBasedChannel'); const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Constants = require('../util/Constants'); const Constants = require('../util/Constants');
const Presence = require('./Presence').Presence; const Presence = require('./Presence').Presence;
const Snowflake = require('../util/Snowflake'); const Snowflake = require('../util/Snowflake');

View File

@@ -0,0 +1,168 @@
const Collection = require('../../util/Collection');
const EventEmitter = require('events').EventEmitter;
/**
* Filter to be applied to the collector.
* @typedef {Function} CollectorFilter
* @param {...*} args Any arguments received by the listener.
* @returns {boolean} To collect or not collect.
*/
/**
* Options to be applied to the collector.
* @typedef {Object} CollectorOptions
* @property {number} [time] How long to run the collector for.
*/
/**
* Interface for defining a new Collector.
* @interface
*/
class Collector extends EventEmitter {
constructor(client, filter, options = {}) {
super();
/**
* @type {Client} client The client.
*/
this.client = client;
/**
* @type {CollectorFilter} filter The filter applied to this collector.
*/
this.filter = filter;
/**
* @type {CollectorOptions} options The options of this collector.
*/
this.options = options;
/**
* @type {Collection} collected The items collected by this collector.
*/
this.collected = new Collection();
/**
* @type {boolean} ended Whether this collector has finished collecting.
*/
this.ended = false;
/**
* @type {?number} _timeout Timeout ID for cleanup.
* @private
*/
this._timeout = null;
/**
* @type {Function} listener Call this to handle an event as a collectable element.
* Accepts any event data as parameters.
* @private
*/
this.listener = this._handle.bind(this);
if (options.time) this._timeout = this.client.setTimeout(() => this.stop('time'), options.time);
}
/**
* @param {...*} args The arguments emitted by the listener.
* @emits Collector#collect
* @private
*/
_handle(...args) {
const collect = this.handle(...args);
if (!collect || !this.filter(...args)) return;
this.collected.set(collect.key, collect.value);
/**
* Emitted whenever an element is collected.
* @event Collector#collect
* @param {*} element The element that got collected.
* @param {Collector} collector The message collector.
*/
this.emit('collect', collect.value, this);
const post = this.postCheck(...args);
if (post) this.stop(post);
}
/**
* Return a promise that resolves with the next collected element;
* rejects with collected elements if the collector finishes without receving a next element.
* @type {Promise}
* @readonly
*/
get next() {
return new Promise((resolve, reject) => {
if (this.ended) {
reject(this.collected);
return;
}
const cleanup = () => {
this.removeListener('collect', onCollect);
this.removeListener('end', onEnd);
};
const onCollect = item => {
cleanup();
resolve(item);
};
const onEnd = () => {
cleanup();
reject(this.collected); // eslint-disable-line prefer-promise-reject-errors
};
this.on('collect', onCollect);
this.on('end', onEnd);
});
}
/**
* Stop this collector and emit the `end` event.
* @param {string} [reason='user'] The reason this collector is ending.
* @emits Collector#end
*/
stop(reason = 'user') {
if (this.ended) return;
if (this._timeout) this.client.clearTimeout(this._timeout);
this.ended = true;
this.cleanup();
/**
* Emitted when the collector is finished collecting.
* @event Collector#end
* @param {Collection} collected The elements collected by the collector.
* @param {string} reason The reason the collector ended.
*/
this.emit('end', this.collected, reason);
}
/* eslint-disable no-empty-function, valid-jsdoc */
/**
* @param {...*} args Any args the event listener emits.
* @returns {?{key: string, value}} Data to insert into collection, if any.
* @abstract
* @private
*/
handle() {}
/**
* @param {...*} args Any args the event listener emits.
* @returns {?string} Reason to end the collector, if any.
* @abstract
* @private
*/
postCheck() {}
/**
* Called when the collector is ending.
* @abstract
* @private
*/
cleanup() {}
/* eslint-enable no-empty-function, valid-jsdoc */
}
module.exports = Collector;

View File

@@ -367,8 +367,19 @@ class TextBasedChannel {
/** /**
* Creates a Message Collector * Creates a Message Collector
* @param {CollectorFilterFunction} filter The filter to create the collector with * @param {CollectorFilter} filter The filter to create the collector with
* @param {CollectorOptions} [options={}] The options to pass to the collector * @param {MessageCollectorOptions} [options={}] The options to pass to the collector
* @returns {MessageCollector}
* @deprecated
*/
createCollector(filter, options) {
return this.createMessageCollector(filter, options);
}
/**
* Creates a Message Collector
* @param {CollectorFilter} filter The filter to create the collector with
* @param {MessageCollectorOptions} [options={}] The options to pass to the collector
* @returns {MessageCollector} * @returns {MessageCollector}
* @example * @example
* // create a message collector * // create a message collector
@@ -379,20 +390,20 @@ class TextBasedChannel {
* collector.on('message', m => console.log(`Collected ${m.content}`)); * collector.on('message', m => console.log(`Collected ${m.content}`));
* collector.on('end', collected => console.log(`Collected ${collected.size} items`)); * collector.on('end', collected => console.log(`Collected ${collected.size} items`));
*/ */
createCollector(filter, options = {}) { createMessageCollector(filter, options = {}) {
return new MessageCollector(this, filter, options); return new MessageCollector(this, filter, options);
} }
/** /**
* An object containing the same properties as CollectorOptions, but a few more: * An object containing the same properties as CollectorOptions, but a few more:
* @typedef {CollectorOptions} AwaitMessagesOptions * @typedef {MessageCollectorOptions} AwaitMessagesOptions
* @property {string[]} [errors] Stop/end reasons that cause the promise to reject * @property {string[]} [errors] Stop/end reasons that cause the promise to reject
*/ */
/** /**
* Similar to createCollector but in promise form. Resolves with a collection of messages that pass the specified * Similar to createCollector but in promise form. Resolves with a collection of messages that pass the specified
* filter. * filter.
* @param {CollectorFilterFunction} filter The filter function to use * @param {CollectorFilter} filter The filter function to use
* @param {AwaitMessagesOptions} [options={}] Optional options to pass to the internal collector * @param {AwaitMessagesOptions} [options={}] Optional options to pass to the internal collector
* @returns {Promise<Collection<Snowflake, Message>>} * @returns {Promise<Collection<Snowflake, Message>>}
* @example * @example
@@ -406,7 +417,7 @@ class TextBasedChannel {
awaitMessages(filter, options = {}) { awaitMessages(filter, options = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const collector = this.createCollector(filter, options); const collector = this.createCollector(filter, options);
collector.on('end', (collection, reason) => { collector.once('end', (collection, reason) => {
if (options.errors && options.errors.includes(reason)) { if (options.errors && options.errors.includes(reason)) {
reject(collection); reject(collection);
} else { } else {
@@ -465,6 +476,7 @@ exports.applyToClass = (structure, full = false, ignore = []) => {
'typingCount', 'typingCount',
'fetchPinnedMessages', 'fetchPinnedMessages',
'createCollector', 'createCollector',
'createMessageCollector',
'awaitMessages' 'awaitMessages'
); );
} }