Search in sources :

Example 1 with ReactionRemoveEvent

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

the class DispatchHandler method reactionRemove.

private void reactionRemove(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 remove 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) {
        // the last reaction of the emoji was removed
        long id = event.emoji.id == null ? 0 : Long.parseUnsignedLong(event.emoji.id);
        reaction = new Reaction(message, 0, ReactionEmoji.of(event.emoji.name, id));
    } else {
        reaction = new Reaction(message, !cached ? reaction.getCount() : reaction.getCount() - 1, reaction.getEmoji());
    }
    if (reaction.getCount() > 0) {
        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 ReactionRemoveEvent(message, reaction, user));
}
Also used : ReactionRemoveEvent(sx.blah.discord.handle.impl.events.guild.channel.message.reaction.ReactionRemoveEvent)

Aggregations

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