Skip to content

Commit

Permalink
Implement super reaction handling (#2554)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Oct 21, 2023
1 parent 2d97b27 commit c7a5a2c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 17 deletions.
83 changes: 70 additions & 13 deletions src/main/java/net/dv8tion/jda/api/entities/MessageReaction.java
Expand Up @@ -57,8 +57,8 @@ public class MessageReaction
private final MessageChannel channel;
private final long channelId;
private final long messageId;
private final boolean self;
private final int count;
private final boolean[] self;
private final int[] counts;

/**
* Creates a new MessageReaction instance
Expand All @@ -72,19 +72,21 @@ public class MessageReaction
* @param messageId
* The message id this reaction is attached to
* @param self
* Whether we already reacted with this Reaction
* @param count
* The amount of people that reacted with this Reaction
* Whether we already reacted with this Reaction,
* as an array of {@code [normal, super]}
* @param counts
* The amount of people that reacted with this Reaction,
* as an array of {@code [total, normal, super]}
*/
public MessageReaction(@Nonnull JDA jda, @Nullable MessageChannel channel, @Nonnull EmojiUnion emoji, long channelId, long messageId, boolean self, int count)
public MessageReaction(@Nonnull JDA jda, @Nullable MessageChannel channel, @Nonnull EmojiUnion emoji, long channelId, long messageId, boolean[] self, int[] counts)
{
this.jda = jda;
this.emoji = emoji;
this.channel = channel;
this.channelId = channelId;
this.messageId = messageId;
this.self = self;
this.count = count;
this.counts = counts;
}

/**
Expand All @@ -99,16 +101,35 @@ public JDA getJDA()
}

/**
* Whether the currently logged in account has reacted with this reaction
* Whether the currently logged in account has reacted with this reaction at all, including both super and normal.
*
* <p><b>This will always be false for events. Discord does not provide this information for reaction events.</b>
* You can use {@link MessageChannel#retrieveMessageById(String)} to get this information on a complete message.
*
* @return True, if we reacted with this reaction
*
* @see #isSelf(ReactionType)
*/
public boolean isSelf()
{
return self;
return self[0] || self[1];
}

/**
* Whether the currently logged in account has reacted with this reaction as specifically a super or normal reaction.
*
* <p><b>This will always be false for events. Discord does not provide this information for reaction events.</b>
* You can use {@link MessageChannel#retrieveMessageById(String)} to get this information on a complete message.
*
* @param type
* The specific type of reaction
*
* @return True, if we reacted with this reaction
*/
public boolean isSelf(@Nonnull ReactionType type)
{
Checks.notNull(type, "Type");
return self[type == ReactionType.NORMAL ? 0 : 1];
}

/**
Expand All @@ -122,7 +143,7 @@ public boolean isSelf()
*/
public boolean hasCount()
{
return count >= 0;
return counts != null;
}

/**
Expand All @@ -138,7 +159,7 @@ public boolean hasChannel()
}

/**
* The amount of users that already reacted with this Reaction
* The total amount of users that already reacted with this Reaction.
* <br><b>This is not updated, it is a {@code final int} per Reaction instance</b>
*
* <p>This value is not available in events such as {@link net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent MessageReactionAddEvent}
Expand All @@ -149,12 +170,40 @@ public boolean hasChannel()
* If this MessageReaction is from an event which does not provide a count
*
* @return The amount of users that reacted with this Reaction
*
* @see #getCount(ReactionType)
*/
public int getCount()
{
if (!hasCount())
throw new IllegalStateException("Cannot retrieve count for this MessageReaction!");
return count;
return counts[0];
}

/**
* The specific amount of users that already reacted with this Reaction.
* <br><b>This is not updated, it is a {@code final int} per Reaction instance</b>
*
* <p>This value is not available in events such as {@link net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent MessageReactionAddEvent}
* and {@link net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent MessageReactionRemoveEvent} in which case an
* {@link java.lang.IllegalStateException IllegalStateException} is thrown!
*
* @param type
* The specific type of reaction
*
* @throws java.lang.IllegalStateException
* If this MessageReaction is from an event which does not provide a count
*
* @return The amount of users that reacted with this Reaction
*
* @see #getCount()
*/
public int getCount(@Nonnull ReactionType type)
{
if (!hasCount())
throw new IllegalStateException("Cannot retrieve count for this MessageReaction!");
Checks.notNull(type, "Type");
return counts[type == ReactionType.NORMAL ? 1 : 2];
}

/**
Expand Down Expand Up @@ -443,7 +492,7 @@ public boolean equals(Object obj)
return false;
MessageReaction r = (MessageReaction) obj;
return r.emoji.equals(emoji)
&& r.self == self
&& r.isSelf() == this.isSelf()
&& r.messageId == messageId;
}

Expand All @@ -456,4 +505,12 @@ public String toString()
.addMetadata("emoji", emoji)
.toString();
}

/**
* Type of reaction.
*/
public enum ReactionType
{
NORMAL, SUPER
}
}
Expand Up @@ -1908,8 +1908,15 @@ private static MessageActivity createMessageActivity(DataObject jsonObject)
public MessageReaction createMessageReaction(MessageChannel chan, long channelId, long messageId, DataObject obj)
{
DataObject emoji = obj.getObject("emoji");
final int count = obj.getInt("count", -1);
final boolean me = obj.getBoolean("me");
final int[] count = new int[]{
obj.getInt("count", 0), // total
obj.optObject("count_details").map(o -> o.getInt("normal", 0)).orElse(0),
obj.optObject("count_details").map(o -> o.getInt("burst", 0)).orElse(0),
};
final boolean[] me = new boolean[] {

This comment has been minimized.

Copy link
@JustRed23

JustRed23 Oct 21, 2023

Had a quick question about this. Would setting new boolean[2] not be more memory efficient in this case?

obj.getBoolean("me"), // normal
obj.getBoolean("me_burst") // super
};
EmojiUnion emojiObj = createEmoji(emoji);

return new MessageReaction(api, chan, emojiObj, channelId, messageId, me, count);
Expand Down
Expand Up @@ -69,7 +69,13 @@ protected Long handleInternally(DataObject content)
DataObject emoji = content.getObject("emoji");
EmojiUnion reactionEmoji = EntityBuilder.createEmoji(emoji);

MessageReaction reaction = new MessageReaction(api, channel, reactionEmoji, channelId, messageId, false, 0);
// We don't know if it is a normal or super reaction
boolean[] self = new boolean[] {
false,
false
};

MessageReaction reaction = new MessageReaction(api, channel, reactionEmoji, channelId, messageId, self, null);

getJDA().handleEvent(new MessageReactionRemoveEmojiEvent(getJDA(), responseNumber, messageId, channel, reaction));
return null;
Expand Down
Expand Up @@ -158,7 +158,13 @@ protected Long handleInternally(DataObject content)
// reaction remove has null name sometimes
EmojiUnion rEmoji = EntityBuilder.createEmoji(emoji);

MessageReaction reaction = new MessageReaction(api, channel, rEmoji, channelId, messageId, userId == api.getSelfUser().getIdLong(), -1);
// We don't know if it is a normal or super reaction
boolean[] self = new boolean[] {
false,
false
};

MessageReaction reaction = new MessageReaction(api, channel, rEmoji, channelId, messageId, self, null);

if (channel.getType() == ChannelType.PRIVATE)
{
Expand Down

0 comments on commit c7a5a2c

Please sign in to comment.