fix(MessageReaction): Prevent event double fire from uncached messages (#6818)

This commit is contained in:
Jiralite
2021-10-16 23:16:05 +01:00
committed by GitHub
parent 38cc89e5ae
commit 267a4b3f68
2 changed files with 14 additions and 11 deletions

View File

@@ -15,7 +15,7 @@ const { PartialTypes } = require('../../util/Constants');
*/ */
class MessageReactionAdd extends Action { class MessageReactionAdd extends Action {
handle(data) { handle(data, fromStructure = false) {
if (!data.emoji) return false; if (!data.emoji) return false;
const user = this.getUserFromMember(data); const user = this.getUserFromMember(data);
@@ -30,9 +30,8 @@ class MessageReactionAdd extends Action {
if (!message) return false; if (!message) return false;
// Verify reaction // Verify reaction
if (message.partial && !this.client.options.partials.includes(PartialTypes.REACTION)) return false; const includePartial = this.client.options.partials.includes(PartialTypes.REACTION);
const existing = message.reactions.cache.get(data.emoji.id ?? data.emoji.name); if (message.partial && !includePartial) return false;
if (existing?.users.cache.has(user.id)) return { message, reaction: existing, user };
const reaction = message.reactions._add({ const reaction = message.reactions._add({
emoji: data.emoji, emoji: data.emoji,
count: message.partial ? null : 0, count: message.partial ? null : 0,
@@ -40,6 +39,7 @@ class MessageReactionAdd extends Action {
}); });
if (!reaction) return false; if (!reaction) return false;
reaction._add(user); reaction._add(user);
if (fromStructure) return { message, reaction, user };
/** /**
* Emitted whenever a reaction is added to a cached message. * Emitted whenever a reaction is added to a cached message.
* @event Client#messageReactionAdd * @event Client#messageReactionAdd

View File

@@ -717,14 +717,17 @@ class Message extends Base {
*/ */
async react(emoji) { async react(emoji) {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED'); if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
emoji = this.client.emojis.resolveIdentifier(emoji);
await this.channel.messages.react(this.id, emoji); await this.channel.messages.react(this.id, emoji);
return this.client.actions.MessageReactionAdd.handle({
user: this.client.user, return this.client.actions.MessageReactionAdd.handle(
channel: this.channel, {
message: this, user: this.client.user,
emoji: Util.parseEmoji(emoji), channel: this.channel,
}).reaction; message: this,
emoji: Util.resolvePartialEmoji(emoji),
},
true,
).reaction;
} }
/** /**