Search in sources :

Example 1 with Reaction

use of de.btobastian.javacord.entities.message.Reaction in project Javacord by BtoBastian.

the class MessageReactionAddHandler method handle.

@Override
public void handle(JSONObject packet) {
    String userId = packet.getString("user_id");
    String messageId = packet.getString("message_id");
    JSONObject emoji = packet.getJSONObject("emoji");
    boolean isCustomEmoji = !emoji.isNull("id");
    Message message = api.getMessageById(messageId);
    if (message == null) {
        return;
    }
    Reaction reaction = null;
    if (isCustomEmoji) {
        String emojiId = emoji.getString("id");
        if (message.isPrivateMessage()) {
            // Private messages with custom emoji? Maybe with Nitro, but there's no documentation so far.
            return;
        }
        CustomEmoji customEmoji = message.getChannelReceiver().getServer().getCustomEmojiById(emojiId);
        if (customEmoji == null) {
            // We don't know this emoji
            return;
        }
        reaction = ((ImplMessage) message).addCustomEmojiReactionToCache(customEmoji, api.getYourself().getId().equals(userId));
    } else {
        reaction = ((ImplMessage) message).addUnicodeReactionToCache(emoji.getString("name"), api.getYourself().getId().equals(userId));
    }
    if (reaction != null) {
        final User user = api.getCachedUserById(userId);
        if (user != null) {
            final Reaction reactionFinal = reaction;
            listenerExecutorService.submit(new Runnable() {

                @Override
                public void run() {
                    List<ReactionAddListener> listeners = api.getListeners(ReactionAddListener.class);
                    synchronized (listeners) {
                        for (ReactionAddListener listener : listeners) {
                            try {
                                listener.onReactionAdd(api, reactionFinal, user);
                            } catch (Throwable t) {
                                logger.warn("Uncaught exception in ReactionAddListener!", t);
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : CustomEmoji(de.btobastian.javacord.entities.CustomEmoji) User(de.btobastian.javacord.entities.User) JSONObject(org.json.JSONObject) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) ReactionAddListener(de.btobastian.javacord.listener.message.ReactionAddListener) List(java.util.List) Reaction(de.btobastian.javacord.entities.message.Reaction)

Example 2 with Reaction

use of de.btobastian.javacord.entities.message.Reaction in project Javacord by BtoBastian.

the class MessageReactionRemoveHandler method handle.

@Override
public void handle(JSONObject packet) {
    String userId = packet.getString("user_id");
    String messageId = packet.getString("message_id");
    JSONObject emoji = packet.getJSONObject("emoji");
    boolean isCustomEmoji = !emoji.isNull("id");
    Message message = api.getMessageById(messageId);
    if (message == null) {
        return;
    }
    Reaction reaction = null;
    if (isCustomEmoji) {
        String emojiId = emoji.getString("id");
        if (message.isPrivateMessage()) {
            // Private messages with custom emoji? Maybe with Nitro, but there's no documentation so far.
            return;
        }
        CustomEmoji customEmoji = message.getChannelReceiver().getServer().getCustomEmojiById(emojiId);
        if (customEmoji == null) {
            // We don't know this emoji
            return;
        }
        reaction = ((ImplMessage) message).removeCustomEmojiReactionToCache(customEmoji, api.getYourself().getId().equals(userId));
    } else {
        reaction = ((ImplMessage) message).removeUnicodeReactionToCache(emoji.getString("name"), api.getYourself().getId().equals(userId));
    }
    if (reaction != null) {
        final User user = api.getCachedUserById(userId);
        if (user != null) {
            final Reaction reactionFinal = reaction;
            listenerExecutorService.submit(new Runnable() {

                @Override
                public void run() {
                    List<ReactionRemoveListener> listeners = api.getListeners(ReactionRemoveListener.class);
                    synchronized (listeners) {
                        for (ReactionRemoveListener listener : listeners) {
                            try {
                                listener.onReactionRemove(api, reactionFinal, user);
                            } catch (Throwable t) {
                                logger.warn("Uncaught exception in ReactionRemoveListener!", t);
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : CustomEmoji(de.btobastian.javacord.entities.CustomEmoji) User(de.btobastian.javacord.entities.User) JSONObject(org.json.JSONObject) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) List(java.util.List) Reaction(de.btobastian.javacord.entities.message.Reaction) ReactionRemoveListener(de.btobastian.javacord.listener.message.ReactionRemoveListener)

Example 3 with Reaction

use of de.btobastian.javacord.entities.message.Reaction in project Javacord by BtoBastian.

the class ImplMessage method addUnicodeReactionToCache.

/**
 * Adds an unicode reaction to the cache.
 *
 * @param unicodeReaction The reaction to add.
 * @param you Whether the reaction was by you or not.
 * @return The reaction.
 */
public Reaction addUnicodeReactionToCache(String unicodeReaction, boolean you) {
    for (Reaction reaction : reactions) {
        if (unicodeReaction.equals(reaction.getUnicodeEmoji())) {
            ((ImplReaction) reaction).incrementCount(you);
            return reaction;
        }
    }
    Reaction reaction = new ImplReaction(api, this, you, 1, unicodeReaction, null);
    reactions.add(reaction);
    return reaction;
}
Also used : Reaction(de.btobastian.javacord.entities.message.Reaction)

Example 4 with Reaction

use of de.btobastian.javacord.entities.message.Reaction in project Javacord by BtoBastian.

the class ImplMessage method addCustomEmojiReactionToCache.

/**
 * Adds an unicode reaction to the cache.
 *
 * @param customEmoji The reaction to add.
 * @param you Whether the reaction was by you or not.
 * @return The reaction.
 */
public Reaction addCustomEmojiReactionToCache(CustomEmoji customEmoji, boolean you) {
    for (Reaction reaction : reactions) {
        if (customEmoji == reaction.getCustomEmoji()) {
            ((ImplReaction) reaction).incrementCount(you);
            return reaction;
        }
    }
    Reaction reaction = new ImplReaction(api, this, you, 1, null, customEmoji);
    reactions.add(reaction);
    return reaction;
}
Also used : Reaction(de.btobastian.javacord.entities.message.Reaction)

Example 5 with Reaction

use of de.btobastian.javacord.entities.message.Reaction in project Javacord by BtoBastian.

the class MessageReactionRemoveAllHandler method handle.

@Override
public void handle(JSONObject packet) {
    // {"message_id":"269166028959776768","channel_id":"81402706320699392"}
    String messageId = packet.getString("message_id");
    final Message message = api.getMessageById(messageId);
    if (message == null) {
        return;
    }
    final List<Reaction> reactions = message.getReactions();
    ((ImplMessage) message).removeAllReactionsFromCache();
    listenerExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            List<ReactionRemoveAllListener> listeners = api.getListeners(ReactionRemoveAllListener.class);
            synchronized (listeners) {
                for (ReactionRemoveAllListener listener : listeners) {
                    try {
                        listener.onReactionRemoveAll(api, message, reactions);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in ReactionRemoveAllListener!", t);
                    }
                }
            }
        }
    });
}
Also used : ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) List(java.util.List) ReactionRemoveAllListener(de.btobastian.javacord.listener.message.ReactionRemoveAllListener) Reaction(de.btobastian.javacord.entities.message.Reaction)

Aggregations

Reaction (de.btobastian.javacord.entities.message.Reaction)5 Message (de.btobastian.javacord.entities.message.Message)3 ImplMessage (de.btobastian.javacord.entities.message.impl.ImplMessage)3 List (java.util.List)3 CustomEmoji (de.btobastian.javacord.entities.CustomEmoji)2 User (de.btobastian.javacord.entities.User)2 JSONObject (org.json.JSONObject)2 ReactionAddListener (de.btobastian.javacord.listener.message.ReactionAddListener)1 ReactionRemoveAllListener (de.btobastian.javacord.listener.message.ReactionRemoveAllListener)1 ReactionRemoveListener (de.btobastian.javacord.listener.message.ReactionRemoveListener)1