Search in sources :

Example 1 with ReactionAddEventImpl

use of org.javacord.core.event.message.reaction.ReactionAddEventImpl 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);
}
Also used : ReactionAddEventImpl(org.javacord.core.event.message.reaction.ReactionAddEventImpl) Server(org.javacord.api.entity.server.Server) Message(org.javacord.api.entity.message.Message) MemberImpl(org.javacord.core.entity.user.MemberImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ReactionAddEvent(org.javacord.api.event.message.reaction.ReactionAddEvent) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) TextChannel(org.javacord.api.entity.channel.TextChannel) ServerImpl(org.javacord.core.entity.server.ServerImpl) Emoji(org.javacord.api.entity.emoji.Emoji) Member(org.javacord.core.entity.user.Member)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 TextChannel (org.javacord.api.entity.channel.TextChannel)1 Emoji (org.javacord.api.entity.emoji.Emoji)1 Message (org.javacord.api.entity.message.Message)1 Server (org.javacord.api.entity.server.Server)1 ReactionAddEvent (org.javacord.api.event.message.reaction.ReactionAddEvent)1 ServerImpl (org.javacord.core.entity.server.ServerImpl)1 Member (org.javacord.core.entity.user.Member)1 MemberImpl (org.javacord.core.entity.user.MemberImpl)1 ReactionAddEventImpl (org.javacord.core.event.message.reaction.ReactionAddEventImpl)1 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)1