use of org.javacord.api.event.message.reaction.ReactionAddEvent in project Javacord by BtoBastian.
the class MessageReactionAddHandler method handle.
@Override
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();
String serverId = packet.hasNonNull("guild_id") ? packet.get("guild_id").asText() : null;
TextChannel channel;
if (serverId == null) {
// if private channel:
channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
} else {
channel = api.getTextChannelById(channelId).orElse(null);
}
if (channel == null) {
LoggerUtil.logMissingChannel(logger, channelId);
return;
}
Optional<Server> server = api.getServerById(serverId);
Member member = null;
if (packet.hasNonNull("member") && server.isPresent()) {
member = new MemberImpl(api, (ServerImpl) server.get(), packet.get("member"), null);
}
Optional<Message> message = api.getCachedMessageById(messageId);
Emoji emoji;
JsonNode emojiJson = packet.get("emoji");
if (!emojiJson.has("id") || emojiJson.get("id").isNull()) {
emoji = UnicodeEmojiImpl.fromString(emojiJson.get("name").asText());
} else {
emoji = api.getKnownCustomEmojiOrCreateCustomEmoji(emojiJson);
}
message.ifPresent(msg -> ((MessageImpl) msg).addReaction(emoji, userId == api.getYourself().getId()));
ReactionAddEvent event = new ReactionAddEventImpl(api, messageId, channel, emoji, userId, member);
api.getEventDispatcher().dispatchReactionAddEvent(server.map(DispatchQueueSelector.class::cast).orElse(api), messageId, server.orElse(null), channel, userId, event);
}
Aggregations