Skip to content

Commit

Permalink
Add message author id to ReactionAddEvent (#1289)
Browse files Browse the repository at this point in the history
Add message author id to ReactionAddEvent
  • Loading branch information
kezc committed Jan 19, 2024
1 parent db1980f commit f93c822
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.javacord.api.event.message.reaction;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
Expand All @@ -14,4 +15,11 @@ public interface ReactionAddEvent extends SingleReactionEvent {
*/
CompletableFuture<Void> removeReaction();

/**
* Gets the message author id.
*
* @return The message author id.
*/
Optional<Long> getMessageAuthorId();

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
import org.javacord.api.event.message.reaction.ReactionAddEvent;
import org.javacord.core.entity.user.Member;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
* The implementation of {@link ReactionAddEvent}.
*/
public class ReactionAddEventImpl extends SingleReactionEventImpl implements ReactionAddEvent {

/**
* The id of the user that sent the message.
*/
private final Long messageAuthorId;

/**
* Creates a new reaction add event.
*
Expand All @@ -23,15 +29,21 @@ public class ReactionAddEventImpl extends SingleReactionEventImpl implements Rea
* @param emoji The emoji.
* @param userId The id of the user who added the reaction.
* @param member The member if it happened in a server.
* @param messageAuthorId The id of the user that sent the message
*/
public ReactionAddEventImpl(
DiscordApi api, long messageId, TextChannel channel, Emoji emoji, long userId, Member member) {
public ReactionAddEventImpl(DiscordApi api, long messageId, TextChannel channel, Emoji emoji,
long userId, Member member, Long messageAuthorId) {
super(api, messageId, channel, emoji, userId);
this.messageAuthorId = messageAuthorId;
}

@Override
public CompletableFuture<Void> removeReaction() {
return Reaction.removeUser(getApi(), getChannel().getId(), getMessageId(), getEmoji(), getUserId());
}

@Override
public Optional<Long> getMessageAuthorId() {
return Optional.ofNullable(messageAuthorId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public void handle(JsonNode packet) {
long channelId = packet.get("channel_id").asLong();
long messageId = packet.get("message_id").asLong();
long userId = packet.get("user_id").asLong();
Long messageAuthorId = packet.hasNonNull("message_author_id")
? packet.get("message_author_id").asLong() : null;
String serverId = packet.hasNonNull("guild_id") ? packet.get("guild_id").asText() : null;

TextChannel channel;
Expand Down Expand Up @@ -77,7 +79,8 @@ public void handle(JsonNode packet) {

message.ifPresent(msg -> ((MessageImpl) msg).addReaction(emoji, userId == api.getYourself().getId()));

ReactionAddEvent event = new ReactionAddEventImpl(api, messageId, channel, emoji, userId, member);
ReactionAddEvent event =
new ReactionAddEventImpl(api, messageId, channel, emoji, userId, member, messageAuthorId);

api.getEventDispatcher().dispatchReactionAddEvent(
server.map(DispatchQueueSelector.class::cast).orElse(api),
Expand Down

0 comments on commit f93c822

Please sign in to comment.