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

Implement super reaction handling #2554

Merged
merged 3 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 70 additions & 13 deletions src/main/java/net/dv8tion/jda/api/entities/MessageReaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class MessageReaction
private final MessageChannel channel;
private final EmojiUnion emoji;
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 @@ -67,17 +67,19 @@ 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 MessageChannel channel, @Nonnull EmojiUnion emoji, long messageId, boolean self, int count)
public MessageReaction(@Nonnull MessageChannel channel, @Nonnull EmojiUnion emoji, long messageId, boolean[] self, int[] counts)
{
this.channel = channel;
this.emoji = emoji;
this.messageId = messageId;
this.self = self;
this.count = count;
this.counts = counts;
}

/**
Expand All @@ -92,16 +94,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 @@ -115,11 +136,11 @@ public boolean isSelf()
*/
public boolean hasCount()
{
return count >= 0;
return counts != null;
}

/**
* 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 @@ -130,12 +151,40 @@ public boolean hasCount()
* 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 @@ -392,7 +441,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 @@ -404,4 +453,12 @@ public String toString()
.addMetadata("emoji", emoji)
.toString();
}

/**
* Type of reaction.
*/
public enum ReactionType
{
NORMAL, SUPER
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1889,8 +1889,15 @@ private static MessageActivity createMessageActivity(DataObject jsonObject)
public MessageReaction createMessageReaction(MessageChannel chan, long id, 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.getObject("count_details").getInt("normal"),
obj.getObject("count_details").getInt("burst"),
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
};
final boolean[] me = new boolean[] {
obj.getBoolean("me"), // normal
obj.getBoolean("me_burst") // super
};
EmojiUnion emojiObj = createEmoji(emoji);

return new MessageReaction(chan, emojiObj, id, me, count);
Expand Down
Original file line number Diff line number Diff line change
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(channel, reactionEmoji, 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(channel, reactionEmoji, messageId, self, null);

getJDA().handleEvent(new MessageReactionRemoveEmojiEvent(getJDA(), responseNumber, messageId, channel, reaction));
return null;
Expand Down
Original file line number Diff line number Diff line change
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(channel, rEmoji, 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(channel, rEmoji, messageId, self, null);

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