Search in sources :

Example 1 with ReactionAddEvent

use of sx.blah.discord.handle.impl.events.guild.channel.message.reaction.ReactionAddEvent in project Discord4J by Discord4J.

the class DispatchHandler method reactionAdd.

private void reactionAdd(ReactionEventResponse event) {
    IChannel channel = shard.getChannelByID(Long.parseUnsignedLong(event.channel_id));
    if (channel == null)
        return;
    // Discord sends this event no matter our permissions for some reason.
    if (!PermissionUtils.hasPermissions(channel, client.ourUser, Permissions.READ_MESSAGES, Permissions.READ_MESSAGE_HISTORY))
        return;
    boolean cached = ((Channel) channel).messages.containsKey(Long.parseUnsignedLong(event.message_id));
    IMessage message = channel.fetchMessage(Long.parseUnsignedLong(event.message_id));
    if (message == null) {
        Discord4J.LOGGER.debug("Unable to fetch the message specified by a reaction add event\nObject={}", ToStringBuilder.reflectionToString(event));
        return;
    }
    IReaction reaction = event.emoji.id == null ? message.getReactionByUnicode(event.emoji.name) : message.getReactionByID(Long.parseUnsignedLong(event.emoji.id));
    message.getReactions().remove(reaction);
    if (reaction == null) {
        // Only happens in the case of a cached message with a new reaction
        long id = event.emoji.id == null ? 0 : Long.parseUnsignedLong(event.emoji.id);
        reaction = new Reaction(message, 1, ReactionEmoji.of(event.emoji.name, id));
    } else if (cached) {
        reaction = new Reaction(message, reaction.getCount() + 1, reaction.getEmoji());
    }
    message.getReactions().add(reaction);
    IUser user;
    if (channel.isPrivate()) {
        user = channel.getUsersHere().get(channel.getUsersHere().get(0).getLongID() == Long.parseUnsignedLong(event.user_id) ? 0 : 1);
    } else {
        user = channel.getGuild().getUserByID(Long.parseUnsignedLong(event.user_id));
    }
    client.dispatcher.dispatch(new ReactionAddEvent(message, reaction, user));
}
Also used : ReactionAddEvent(sx.blah.discord.handle.impl.events.guild.channel.message.reaction.ReactionAddEvent)

Aggregations

ReactionAddEvent (sx.blah.discord.handle.impl.events.guild.channel.message.reaction.ReactionAddEvent)1