Skip to content

Commit

Permalink
feat: count super reactions in events
Browse files Browse the repository at this point in the history
  • Loading branch information
jaw0r3k committed Mar 16, 2024
1 parent 35d2b59 commit fe90335
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/discord.js/src/client/actions/MessageReactionAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ class MessageReactionAdd extends Action {
emoji: data.emoji,
count: message.partial ? null : 0,
me: user.id === this.client.user.id,
burst_colors: data.burst_colors,
});
if (!reaction) return false;
reaction._add(user);
reaction._add(user, data.burst);
if (fromStructure) return { message, reaction, user };
/**
* Emitted whenever a reaction is added to a cached message.
* @event Client#messageReactionAdd
* @param {MessageReaction} messageReaction The reaction object
* @param {User} user The user that applied the guild or reaction emoji
* @param {boolean} burst Determines when a super reaction is added
*/
this.client.emit(Events.MessageReactionAdd, reaction, user);
this.client.emit(Events.MessageReactionAdd, reaction, user, data.burst);

return { message, reaction, user };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ class MessageReactionRemove extends Action {
// Verify reaction
const reaction = this.getReaction(data, message, user);
if (!reaction) return false;
reaction._remove(user);
reaction._remove(user, data.burst);
/**
* Emitted whenever a reaction is removed from a cached message.
* @event Client#messageReactionRemove
* @param {MessageReaction} messageReaction The reaction object
* @param {User} user The user whose emoji or reaction emoji was removed
* @param {boolean} burst Determines when a super reaction was removed
*/
this.client.emit(Events.MessageReactionRemove, reaction, user);
this.client.emit(Events.MessageReactionRemove, reaction, user, data.burst);

return { message, reaction, user };
}
Expand Down
22 changes: 18 additions & 4 deletions packages/discord.js/src/structures/MessageReaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,31 @@ class MessageReaction {
return this._emoji.id ?? this._emoji.name;
}

_add(user) {
_add(user, burst) {
if (this.partial) return;
this.users.cache.set(user.id, user);
if (!this.me || user.id !== this.message.client.user.id || this.count === 0) this.count++;
if (!this.me || user.id !== this.message.client.user.id || this.count === 0) {
this.count++;
if (burst) {
this.countDetails.burst++;
} else {
this.countDetails.normal++;
}
}
this.me ||= user.id === this.message.client.user.id;
}

_remove(user) {
_remove(user, burst) {
if (this.partial) return;
this.users.cache.delete(user.id);
if (!this.me || user.id !== this.message.client.user.id) this.count--;
if (!this.me || user.id !== this.message.client.user.id) {
this.count--;
if (burst) {
this.countDetails.burst--;
} else {
this.countDetails.normal--;
}
}
if (user.id === this.message.client.user.id) this.me = false;
if (this.count <= 0 && this.users.cache.size === 0) {
this.message.reactions.cache.delete(this.emoji.id ?? this.emoji.name);
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5120,8 +5120,8 @@ export interface ClientEvents {
];
messageReactionRemoveEmoji: [reaction: MessageReaction | PartialMessageReaction];
messageDeleteBulk: [messages: Collection<Snowflake, Message | PartialMessage>, channel: GuildTextBasedChannel];
messageReactionAdd: [reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser];
messageReactionRemove: [reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser];
messageReactionAdd: [reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser, burst: boolean];
messageReactionRemove: [reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser, burst: boolean];
messageUpdate: [oldMessage: Message | PartialMessage, newMessage: Message | PartialMessage];
presenceUpdate: [oldPresence: Presence | null, newPresence: Presence];
ready: [client: Client<true>];
Expand Down

0 comments on commit fe90335

Please sign in to comment.