Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: super reactions #9336

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
48 changes: 44 additions & 4 deletions packages/discord.js/src/structures/MessageReaction.js
Expand Up @@ -43,13 +43,39 @@ class MessageReaction {
}

_patch(data) {
if ('burst_colors' in data) {
/**
* Hexadecimal colors used for this super reaction
* @type {string[]}
*/
this.burstColors = data.burst_colors;
}

if ('count' in data) {
/**
* The number of people that have given the same reaction
* @type {?number}
*/
this.count ??= data.count;
}

if ('count_details' in data) {
/**
* The reaction count details object contains information about super and normal reaction counts.
* @typedef {Object} ReactionCountDetailsData
* @property {number} burst Count of super reactions
* @property {number} normal Count of normal reactions
*/

/**
* The reaction count details object contains information about super and normal reaction counts.
* @type {ReactionCountDetailsData}
*/
this.countDetails = {
burst: data.count_details.burst,
normal: data.count_details.normal,
};
}
}

/**
Expand Down Expand Up @@ -121,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
11 changes: 9 additions & 2 deletions packages/discord.js/typings/index.d.ts
Expand Up @@ -2337,8 +2337,10 @@ export class MessageReaction {
private constructor(client: Client<true>, data: RawMessageReactionData, message: Message);
private _emoji: GuildEmoji | ReactionEmoji;

public burstColors: string[];
public readonly client: Client<true>;
public count: number;
public countDetails: ReactionCountDetailsData;
public get emoji(): GuildEmoji | ReactionEmoji;
public me: boolean;
public message: Message | PartialMessage;
Expand Down Expand Up @@ -5118,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 Expand Up @@ -6295,6 +6297,11 @@ export interface MessageSelectOption {
value: string;
}

export interface ReactionCountDetailsData {
burst: number;
normal: number;
}

export interface SelectMenuComponentOptionData {
default?: boolean;
description?: string;
Expand Down