Skip to content

Commit

Permalink
fix(MessageReaction): Prevent event double fire from uncached messages (
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Oct 16, 2021
1 parent 38cc89e commit 267a4b3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/client/actions/MessageReactionAdd.js
Expand Up @@ -15,7 +15,7 @@ const { PartialTypes } = require('../../util/Constants');
*/

class MessageReactionAdd extends Action {
handle(data) {
handle(data, fromStructure = false) {
if (!data.emoji) return false;

const user = this.getUserFromMember(data);
Expand All @@ -30,16 +30,16 @@ class MessageReactionAdd extends Action {
if (!message) return false;

// Verify reaction
if (message.partial && !this.client.options.partials.includes(PartialTypes.REACTION)) return false;
const existing = message.reactions.cache.get(data.emoji.id ?? data.emoji.name);
if (existing?.users.cache.has(user.id)) return { message, reaction: existing, user };
const includePartial = this.client.options.partials.includes(PartialTypes.REACTION);
if (message.partial && !includePartial) return false;
const reaction = message.reactions._add({
emoji: data.emoji,
count: message.partial ? null : 0,
me: user.id === this.client.user.id,
});
if (!reaction) return false;
reaction._add(user);
if (fromStructure) return { message, reaction, user };
/**
* Emitted whenever a reaction is added to a cached message.
* @event Client#messageReactionAdd
Expand Down
17 changes: 10 additions & 7 deletions src/structures/Message.js
Expand Up @@ -717,14 +717,17 @@ class Message extends Base {
*/
async react(emoji) {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
emoji = this.client.emojis.resolveIdentifier(emoji);
await this.channel.messages.react(this.id, emoji);
return this.client.actions.MessageReactionAdd.handle({
user: this.client.user,
channel: this.channel,
message: this,
emoji: Util.parseEmoji(emoji),
}).reaction;

return this.client.actions.MessageReactionAdd.handle(
{
user: this.client.user,
channel: this.channel,
message: this,
emoji: Util.resolvePartialEmoji(emoji),
},
true,
).reaction;
}

/**
Expand Down

0 comments on commit 267a4b3

Please sign in to comment.