Skip to content

Commit

Permalink
Add message reaction collectors & abstract collectors (#1335)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
appellation authored and iCrawl committed Apr 19, 2017
1 parent 8475a4a commit ca34c43
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/structures/DMChannel.js
@@ -1,5 +1,5 @@
const Channel = require('./Channel');
const TextBasedChannel = require('./interface/TextBasedChannel');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Collection = require('../util/Collection');

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/GroupDMChannel.js
@@ -1,5 +1,5 @@
const Channel = require('./Channel');
const TextBasedChannel = require('./interface/TextBasedChannel');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Collection = require('../util/Collection');

/*
Expand Down
2 changes: 1 addition & 1 deletion src/structures/GuildMember.js
@@ -1,4 +1,4 @@
const TextBasedChannel = require('./interface/TextBasedChannel');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Role = require('./Role');
const Permissions = require('../util/Permissions');
const Collection = require('../util/Collection');
Expand Down
42 changes: 42 additions & 0 deletions src/structures/Message.js
Expand Up @@ -2,6 +2,7 @@ const Mentions = require('./MessageMentions');
const Attachment = require('./MessageAttachment');
const Embed = require('./MessageEmbed');
const MessageReaction = require('./MessageReaction');
const ReactionCollector = require('./ReactionCollector');
const Util = require('../util/Util');
const Collection = require('../util/Collection');
const Constants = require('../util/Constants');
Expand Down Expand Up @@ -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.
* Sorted from latest (first) to oldest (last).
Expand Down
167 changes: 45 additions & 122 deletions src/structures/MessageCollector.js
@@ -1,150 +1,73 @@
const EventEmitter = require('events').EventEmitter;
const Collection = require('../util/Collection');
const Collector = require('./interfaces/Collector');

/**
* Collects messages based on a specified filter, then emits them.
* @extends {EventEmitter}
* @typedef {CollectorOptions} MessageCollectorOptions
* @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.
* ```js
* function(message, collector) {
* if (message.content.includes('discord')) {
* 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.
* @typedef {Object} CollectorOptions
* @property {number} [time] Duration for the collector in milliseconds
* @property {number} [max] Maximum number of messages to handle
* @property {number} [maxMatches] Maximum number of successfully filtered messages to obtain
*/
/**
* Collects messages on a channel.
* @implements {Collector}
*/
class MessageCollector extends Collector {

/**
* @param {Channel} channel The channel to collect messages in
* @param {CollectorFilterFunction} filter The filter function
* @param {CollectorOptions} [options] Options for the collector
* @param {TextBasedChannel} channel The channel.
* @param {CollectorFilter} filter The filter to be applied to this collector.
* @param {MessageCollectorOptions} options The options to be applied to this collector.
* @emits MessageCollector#message
*/
constructor(channel, filter, options = {}) {
super();
super(channel.client, filter, options);

/**
* The channel this collector is operating on
* @type {Channel}
* @type {TextBasedChannel} channel The channel.
*/
this.channel = channel;

/**
* A function used to filter messages that the collector collects.
* @type {CollectorFilterFunction}
*/
this.filter = filter;

/**
* Options for the collecor.
* @type {CollectorOptions}
*/
this.options = options;

/**
* Whether this collector has stopped collecting messages.
* @type {boolean}
*/
this.ended = false;

/**
* A collection of collected messages, mapped by message ID.
* @type {Collection<Snowflake, Message>}
* @type {number} received Total number of messages that were received in the
* channel during message collection.
*/
this.collected = new Collection();
this.received = 0;

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);
}
this.client.on('message', this.listener);

/**
* 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);
// For backwards compatibility (remove in v12).
if (this.options.max) this.options.maxProcessed = this.options.max;
if (this.options.maxMatches) this.options.max = this.options.maxMatches;
this._reEmitter = message => {
/**
* Emitted whenever the collector receives a message that passes the filter test.
* @param {Message} message The received message
* @param {MessageCollector} collector The collector the message passed through
* Emitted when the collector receives a message.
* @event MessageCollector#message
* @param {Message} message The message.
* @deprecated
*/
this.emit('message', message, this);
if (this.collected.size >= this.options.maxMatches) this.stop('matchesLimit');
else if (this.options.max && this.collected.size === this.options.max) this.stop('limit');
return true;
}
return false;
this.emit('message', message);
};
this.on('collect', this._reEmitter);
}

/**
* 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<Message>}
* @readonly
*/
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
};
handle(message) {
if (message.channel.id !== this.channel.id) return null;
this.received++;
return {
key: message.id,
value: message,
};
}

this.once('message', onMessage);
this.once('end', onEnd);
});
postCheck() {
// Consider changing the end reasons for v12
if (this.options.maxMatches && this.collected.size >= this.options.max) return 'matchesLimit';
if (this.options.max && this.received >= this.options.maxProcessed) return 'limit';
return null;
}

/**
* Stops the collector and emits `end`.
* @param {string} [reason='user'] An optional reason for stopping the collector
*/
stop(reason = 'user') {
if (this.ended) return;
this.ended = true;
this.channel.client.removeListener('message', this.listener);
/**
* Emitted when the Collector stops collecting.
* @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);
cleanup() {
this.removeListener('collect', this._reEmitter);
this.client.removeListener('message', this.listener);
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/structures/ReactionCollector.js
@@ -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;
3 changes: 2 additions & 1 deletion src/structures/TextChannel.js
@@ -1,5 +1,5 @@
const GuildChannel = require('./GuildChannel');
const TextBasedChannel = require('./interface/TextBasedChannel');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Collection = require('../util/Collection');

/**
Expand Down Expand Up @@ -89,6 +89,7 @@ class TextChannel extends GuildChannel {
get typing() {}
get typingCount() {}
createCollector() {}
createMessageCollector() {}
awaitMessages() {}
bulkDelete() {}
acknowledge() {}
Expand Down
2 changes: 1 addition & 1 deletion src/structures/User.js
@@ -1,4 +1,4 @@
const TextBasedChannel = require('./interface/TextBasedChannel');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Constants = require('../util/Constants');
const Presence = require('./Presence').Presence;
const Snowflake = require('../util/Snowflake');
Expand Down

0 comments on commit ca34c43

Please sign in to comment.