Search in sources :

Example 1 with PrivateChannelImpl

use of net.dv8tion.jda.internal.entities.PrivateChannelImpl in project JDA by DV8FromTheWorld.

the class MessageReactionHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    if (!content.isNull("guild_id")) {
        long guildId = content.getLong("guild_id");
        if (api.getGuildSetupController().isLocked(guildId))
            return guildId;
    }
    DataObject emoji = content.getObject("emoji");
    final long userId = content.getLong("user_id");
    final long messageId = content.getLong("message_id");
    final long channelId = content.getLong("channel_id");
    final Long emojiId = emoji.isNull("id") ? null : emoji.getLong("id");
    String emojiName = emoji.getString("name", null);
    final boolean emojiAnimated = emoji.getBoolean("animated");
    if (emojiId == null && emojiName == null) {
        WebSocketClient.LOG.debug("Received a reaction {} with no name nor id. json: {}", JDALogger.getLazyString(() -> add ? "add" : "remove"), content);
        return null;
    }
    final long guildId = content.getUnsignedLong("guild_id", 0);
    Guild guild = api.getGuildById(guildId);
    MemberImpl member = null;
    if (guild != null) {
        member = (MemberImpl) guild.getMemberById(userId);
        // Attempt loading the member if possible
        Optional<DataObject> memberJson = content.optObject("member");
        if (// Check if we can load a member here
        memberJson.isPresent()) {
            DataObject json = memberJson.get();
            if (// do we need to load a member?
            member == null || !member.hasTimeJoined())
                member = api.getEntityBuilder().createMember((GuildImpl) guild, json);
            else // otherwise update the cache
            {
                List<Role> roles = json.getArray("roles").stream(DataArray::getUnsignedLong).map(guild::getRoleById).filter(Objects::nonNull).collect(Collectors.toList());
                api.getEntityBuilder().updateMember((GuildImpl) guild, member, json, roles);
            }
            // update internal references
            api.getEntityBuilder().updateMemberCache(member);
        }
        if (member == null && add && guild.isLoaded()) {
            WebSocketClient.LOG.debug("Dropping reaction event for unknown member {}", content);
            return null;
        }
    }
    User user = api.getUserById(userId);
    if (user == null && member != null)
        // this happens when we have guild subscriptions disabled
        user = member.getUser();
    if (user == null) {
        // The only time we can receive a reaction add but not have the user cached would be if we receive the event in an uncached or partially built PrivateChannel.
        if (add && guild != null) {
            api.getEventCache().cache(EventCache.Type.USER, userId, responseNumber, allContent, this::handle);
            EventCache.LOG.debug("Received a reaction for a user that JDA does not currently have cached. " + "UserID: {} ChannelId: {} MessageId: {}", userId, channelId, messageId);
            return null;
        }
    }
    // TODO-v5-unified-channel-cache
    MessageChannel channel = api.getTextChannelById(channelId);
    if (channel == null)
        channel = api.getNewsChannelById(channelId);
    if (channel == null)
        channel = api.getThreadChannelById(channelId);
    if (channel == null)
        channel = api.getPrivateChannelById(channelId);
    if (channel == null) {
        if (guildId != 0) {
            api.getEventCache().cache(EventCache.Type.CHANNEL, channelId, responseNumber, allContent, this::handle);
            EventCache.LOG.debug("Received a reaction for a channel that JDA does not currently have cached");
            return null;
        }
        // create a new private channel with minimal information for this event
        channel = getJDA().getEntityBuilder().createPrivateChannel(DataObject.empty().put("id", channelId));
    }
    MessageReaction.ReactionEmote rEmote;
    if (emojiId != null) {
        Emote emote = api.getEmoteById(emojiId);
        if (emote == null) {
            if (emojiName != null) {
                emote = new EmoteImpl(emojiId, api).setAnimated(emojiAnimated).setName(emojiName);
            } else {
                WebSocketClient.LOG.debug("Received a reaction {} with a null name. json: {}", JDALogger.getLazyString(() -> add ? "add" : "remove"), content);
                return null;
            }
        }
        rEmote = MessageReaction.ReactionEmote.fromCustom(emote);
    } else {
        rEmote = MessageReaction.ReactionEmote.fromUnicode(emojiName, api);
    }
    MessageReaction reaction = new MessageReaction(channel, rEmote, messageId, userId == api.getSelfUser().getIdLong(), -1);
    if (channel.getType() == ChannelType.PRIVATE) {
        api.usedPrivateChannel(reaction.getChannel().getIdLong());
        PrivateChannelImpl priv = (PrivateChannelImpl) channel;
        // try to add the user here if we need to, as we have their ID
        if (priv.getUser() == null && user != null) {
            priv.setUser(user);
        }
    }
    if (add) {
        api.handleEvent(new MessageReactionAddEvent(api, responseNumber, user, member, reaction, userId));
    } else {
        api.handleEvent(new MessageReactionRemoveEvent(api, responseNumber, user, member, reaction, userId));
    }
    return null;
}
Also used : MessageReactionAddEvent(net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent) MessageReactionRemoveEvent(net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) PrivateChannelImpl(net.dv8tion.jda.internal.entities.PrivateChannelImpl) EmoteImpl(net.dv8tion.jda.internal.entities.EmoteImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject)

Aggregations

MessageReactionAddEvent (net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent)1 MessageReactionRemoveEvent (net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 EmoteImpl (net.dv8tion.jda.internal.entities.EmoteImpl)1 MemberImpl (net.dv8tion.jda.internal.entities.MemberImpl)1 PrivateChannelImpl (net.dv8tion.jda.internal.entities.PrivateChannelImpl)1