Search in sources :

Example 1 with ReactionRemoveEvent

use of org.javacord.api.event.message.reaction.ReactionRemoveEvent in project Javacord by BtoBastian.

the class MessageReactionRemoveHandler method handle.

@Override
public void handle(JsonNode packet) {
    long messageId = packet.get("message_id").asLong();
    long userId = packet.get("user_id").asLong();
    Optional<Message> message = api.getCachedMessageById(messageId);
    long channelId = packet.get("channel_id").asLong();
    TextChannel channel;
    if (packet.hasNonNull("guild_id")) {
        channel = api.getTextChannelById(channelId).orElse(null);
    } else {
        // if private channel:
        channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
    }
    if (channel == null) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    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).removeReaction(emoji, userId == api.getYourself().getId()));
    ReactionRemoveEvent event = new ReactionRemoveEventImpl(api, messageId, channel, emoji, userId);
    Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
    api.getEventDispatcher().dispatchReactionRemoveEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, userId, event);
}
Also used : ReactionRemoveEvent(org.javacord.api.event.message.reaction.ReactionRemoveEvent) TextChannel(org.javacord.api.entity.channel.TextChannel) Message(org.javacord.api.entity.message.Message) Server(org.javacord.api.entity.server.Server) ReactionRemoveEventImpl(org.javacord.core.event.message.reaction.ReactionRemoveEventImpl) Emoji(org.javacord.api.entity.emoji.Emoji) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServerChannel(org.javacord.api.entity.channel.ServerChannel) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ServerChannel (org.javacord.api.entity.channel.ServerChannel)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 ReactionRemoveEvent (org.javacord.api.event.message.reaction.ReactionRemoveEvent)1 ReactionRemoveEventImpl (org.javacord.core.event.message.reaction.ReactionRemoveEventImpl)1 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)1