From 09d1f2f18f5ec536bb25156553986fee51c80d1e Mon Sep 17 00:00:00 2001 From: Alon Livne <37940683+Alon-L@users.noreply.github.com> Date: Wed, 27 Jan 2021 13:13:15 +0200 Subject: [PATCH] feat(ReactionCollector): event create (#4108) * fix(Typing): setSpeaking public * feat(ReactionCollector): create event, close #2844 * Revert "fix(Typing): setSpeaking public" This reverts commit ccc0e0cc768ccd9af5f5d6962ff259d250f21b64. --- src/structures/ReactionCollector.js | 16 +++++++++++- test/reactionCollectorCreated.test.js | 37 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/reactionCollectorCreated.test.js diff --git a/src/structures/ReactionCollector.js b/src/structures/ReactionCollector.js index e0fa316fc..ee7e75886 100644 --- a/src/structures/ReactionCollector.js +++ b/src/structures/ReactionCollector.js @@ -81,10 +81,11 @@ class ReactionCollector extends Collector { /** * Handles an incoming reaction for possible collection. * @param {MessageReaction} reaction The reaction to possibly collect + * @param {User} user The user that added the reaction * @returns {?Snowflake|string} * @private */ - collect(reaction) { + collect(reaction, user) { /** * Emitted whenever a reaction is collected. * @event ReactionCollector#collect @@ -92,6 +93,19 @@ class ReactionCollector extends Collector { * @param {User} user The user that added the reaction */ if (reaction.message.id !== this.message.id) return null; + + /** + * Emitted whenever a reaction is newly created on a message. Will emit only when a new reaction is + * added to the message, as opposed to {@link Collector#collect} which which will + * be emitted even when a reaction has already been added to the message. + * @event ReactionCollector#create + * @param {MessageReaction} reaction The reaction that was added + * @param {User} user The user that added the reaction + */ + if (reaction.count === 1 && this.filter(reaction, user, this.collected)) { + this.emit('create', reaction, user); + } + return ReactionCollector.key(reaction); } diff --git a/test/reactionCollectorCreated.test.js b/test/reactionCollectorCreated.test.js new file mode 100644 index 000000000..a9220e37b --- /dev/null +++ b/test/reactionCollectorCreated.test.js @@ -0,0 +1,37 @@ +'use strict'; + +const { token, guildId, channelId, messageId } = require('./auth.js'); +const { Client, ReactionCollector } = require('../src'); + +const client = new Client(); + +client.on('ready', async () => { + const guild = client.guilds.cache.get(guildId); + + const channel = guild.channels.cache.get(channelId); + + const message = await channel.messages.fetch(messageId); + + await message.react('🔔'); + // Await message.reactions.removeAll(); + + const collector = new ReactionCollector(message, () => true, { dispose: true }); + + collector.on('collect', () => { + console.log('collected'); + }); + + collector.on('create', () => { + console.log('created'); + }); + + collector.on('remove', () => { + console.log('removed'); + }); + + collector.on('dispose', () => { + console.log('disposed'); + }); +}); + +client.login(token);