Skip to content

Commit

Permalink
burst reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
DonovanDMC committed Sep 3, 2023
1 parent 7f7b71b commit 8a87287
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 33 deletions.
63 changes: 42 additions & 21 deletions lib/gateway/Shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,17 +702,29 @@ export default class Shard extends TypedEmitter<ShardEvents> {
: this.client.users.get(packet.d.user_id) ?? { id: packet.d.user_id };

if (message) {
const name = packet.d.emoji.id ? `${packet.d.emoji.name}:${packet.d.emoji.id}` : packet.d.emoji.name;
if (message.reactions[name]) {
message.reactions[name].count++;
const index = message.reactions.findIndex(r => r.emoji.id === packet.d.emoji.id && r.emoji.name === packet.d.emoji.name);
if (index === -1) {
message.reactions.push({
burstColors: packet.d.burst_colors,
count: 1,
countDetails: {
burst: packet.d.burst ? 1 : 0,
normal: packet.d.burst ? 0 : 1
},
emoji: packet.d.emoji,
me: packet.d.user_id === this.client.user.id,
meBurst: packet.d.user_id === this.client.user.id && packet.d.burst
});
} else {
if (packet.d.burst) {
message.reactions[index].countDetails.burst++;
} else {
message.reactions[index].countDetails.normal++;
}
message.reactions[index].count++;
if (packet.d.user_id === this.client.user.id) {
message.reactions[name].me = true;
message.reactions[index].me = true;
}
} else {
message.reactions[name] = {
count: 1,
me: packet.d.user_id === this.client.user.id
};
}

}
Expand All @@ -724,7 +736,7 @@ export default class Shard extends TypedEmitter<ShardEvents> {
guildID: packet.d.guild_id,
id: packet.d.message_id ,
author: packet.d.message_author_id === undefined ? undefined : guild?.members.get(packet.d.user_id) ?? this.client.users.get(packet.d.user_id) ?? { id: packet.d.message_author_id }
}, reactor, packet.d.emoji);
}, reactor, packet.d.emoji, packet.d.burst);
break;
}

Expand All @@ -734,14 +746,23 @@ export default class Shard extends TypedEmitter<ShardEvents> {
const reactor = this.client.users.get(packet.d.user_id) ?? { id: packet.d.user_id };

if (message) {
const name = packet.d.emoji.id ? `${packet.d.emoji.name}:${packet.d.emoji.id}` : packet.d.emoji.name;
if (message.reactions[name]) {
message.reactions[name].count--;
const index = message.reactions.findIndex(r => r.emoji.id === packet.d.emoji.id && r.emoji.name === packet.d.emoji.name);
if (index !== -1) {
if (packet.d.burst) {
message.reactions[index].countDetails.burst--;
} else {
message.reactions[index].countDetails.normal--;
}
message.reactions[index].count--;
if (packet.d.user_id === this.client.user.id) {
message.reactions[name].me = false;
if (packet.d.burst) {
message.reactions[index].meBurst = false;
} else {
message.reactions[index].me = false;
}
}
if (message.reactions[name].count === 0) {
delete message.reactions[name];
if (message.reactions[index].count === 0) {
message.reactions.splice(index, 1);
}
}
}
Expand All @@ -752,7 +773,7 @@ export default class Shard extends TypedEmitter<ShardEvents> {
guild: packet.d.guild_id ? this.client.guilds.get(packet.d.guild_id) : undefined,
guildID: packet.d.guild_id,
id: packet.d.message_id
}, reactor, packet.d.emoji);
}, reactor, packet.d.emoji, packet.d.burst);
break;
}

Expand All @@ -761,7 +782,7 @@ export default class Shard extends TypedEmitter<ShardEvents> {
const message = channel?.messages?.get(packet.d.message_id);

if (message) {
message.reactions = {};
message.reactions = [];
}

this.client.emit("messageReactionRemoveAll", message ?? {
Expand All @@ -779,9 +800,9 @@ export default class Shard extends TypedEmitter<ShardEvents> {
const message = channel?.messages?.get(packet.d.message_id);

if (message) {
const name = packet.d.emoji.id ? `${packet.d.emoji.name}:${packet.d.emoji.id}` : packet.d.emoji.name;
if (message.reactions[name]) {
delete message.reactions[name];
const index = message.reactions.findIndex(r => r.emoji.id === packet.d.emoji.id && r.emoji.name === packet.d.emoji.name);
if (index !== -1) {
message.reactions.splice(index, 1);
}
}

Expand Down
19 changes: 10 additions & 9 deletions lib/structures/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class Message<T extends AnyTextableChannel | Uncached = AnyTextab
/** This message's relative position, if in a thread. */
position?: number;
/** The reactions on this message. */
reactions: Record<string, MessageReaction>;
reactions: Array<MessageReaction>;
/** If this message is a `REPLY` or `THREAD_STARTER_MESSAGE`, this will be the message that's referenced. */
referencedMessage?: Message | null;
/** The data of the role subscription purchase or renewal that prompted this message. */
Expand Down Expand Up @@ -142,7 +142,7 @@ export default class Message<T extends AnyTextableChannel | Uncached = AnyTextab
users: []
};
this.pinned = !!data.pinned;
this.reactions = {};
this.reactions = [];
// message updates can be missing a timestamp
this.timestamp = data.timestamp === undefined ? Base.getCreatedAt(this.id) : new Date(data.timestamp);
this.tts = !!data.tts;
Expand Down Expand Up @@ -250,13 +250,14 @@ export default class Message<T extends AnyTextableChannel | Uncached = AnyTextab
this.position = data.position;
}
if (data.reactions) {
for (const reaction of data.reactions) {
const name = reaction.emoji.id ? `${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name;
this.reactions[name] = {
count: reaction.count,
me: reaction.me
};
}
this.reactions = data.reactions.map(r => ({
burstColors: r.burst_colors,
count: r.count,
countDetails: r.count_details,
emoji: r.emoji,
me: r.me,
meBurst: r.me_burst
}));
}

if (data.referenced_message !== undefined) {
Expand Down
12 changes: 12 additions & 0 deletions lib/types/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,15 +689,27 @@ export interface ChannelMention {
type: ChannelTypes;
}

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

export interface RawMessageReaction {
burst_colors: Array<string>;
count: number;
count_details: MessageReactionCountDetails;
emoji: PartialEmoji;
me: boolean;
me_burst: boolean;
}

export interface MessageReaction {
burstColors: Array<string>;
count: number;
countDetails: MessageReactionCountDetails;
emoji: PartialEmoji;
me: boolean;
meBurst: boolean;
}

export interface MessageActivity {
Expand Down
4 changes: 2 additions & 2 deletions lib/types/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ export interface ClientEvents {
/** @event Emitted when messages are bulk deleted. Requires the `GUILD_MESSAGES` intent. The `MESSAGE_CONTENT` intent is required for `content`, `embeds`, and similar to be present on most messages. */
messageDeleteBulk: [messages: Array<PossiblyUncachedMessage>];
/** @event Emitted when a reaction is added to a message. For uncached messages, `author` will not be present if the reaction was added to a webhook message. Requires the `GUILD_MESSAGE_REACTIONS` for guild messages, and `DIRECT_MESSAGE_REACTIONS` for direct messages. */
messageReactionAdd: [message: PossiblyUncachedMessage & { author?: Member | User | Uncached; }, reactor: Member | User | Uncached, reaction: PartialEmoji];
messageReactionAdd: [message: PossiblyUncachedMessage & { author?: Member | User | Uncached; }, reactor: Member | User | Uncached, reaction: PartialEmoji, burst: boolean];
/** @event Emitted when a reaction is removed from a message. Requires the `GUILD_MESSAGE_REACTIONS` for guild messages, and `DIRECT_MESSAGE_REACTIONS` for direct messages. */
messageReactionRemove: [message: PossiblyUncachedMessage, reactor: Member | User | Uncached, reaction: PartialEmoji];
messageReactionRemove: [message: PossiblyUncachedMessage, reactor: Member | User | Uncached, reaction: PartialEmoji, burst: boolean];
/** @event Emitted when all reactions are removed from a message. Requires the `GUILD_MESSAGE_REACTIONS` for guild messages, and `DIRECT_MESSAGE_REACTIONS` for direct messages. */
messageReactionRemoveAll: [message: PossiblyUncachedMessage];
/** @event Emitted when a specific reaction is removed for all users from a message. Requires the `GUILD_MESSAGE_REACTIONS` for guild messages, and `DIRECT_MESSAGE_REACTIONS` for direct messages. */
Expand Down
4 changes: 4 additions & 0 deletions lib/types/gateway-raw.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ export interface MessageUpdatePacket extends BaseDispatchPacket {

export interface MessageReactionAddPacket extends BaseDispatchPacket {
d: {
burst: boolean;
burst_colors: Array<string>;
channel_id: string;
emoji: PartialEmoji;
guild_id?: string;
Expand All @@ -420,6 +422,8 @@ export interface MessageReactionAddPacket extends BaseDispatchPacket {

export interface MessageReactionRemovePacket extends BaseDispatchPacket {
d: {
burst: boolean;
burst_colors: Array<string>;
channel_id: string;
emoji: PartialEmoji;
guild_id?: string;
Expand Down
2 changes: 1 addition & 1 deletion lib/types/json.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ export interface JSONMessage extends JSONBase {
nonce?: number | string;
pinned: boolean;
position?: number;
reactions: Record<string, MessageReaction>;
reactions: Array<MessageReaction>;
referencedMessage?: JSONMessage | null;
stickerItems?: Array<StickerItem>;
thread?: JSONAnnouncementThreadChannel | JSONPublicThreadChannel | JSONPrivateThreadChannel;
Expand Down

0 comments on commit 8a87287

Please sign in to comment.