Search in sources :

Example 6 with MemberImpl

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

the class GuildRoleDeleteHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    final long guildId = content.getLong("guild_id");
    if (getJDA().getGuildSetupController().isLocked(guildId))
        return guildId;
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
    if (guild == null) {
        getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("GUILD_ROLE_DELETE was received for a Guild that is not yet cached: {}", content);
        return null;
    }
    final long roleId = content.getLong("role_id");
    Role removedRole = guild.getRolesView().remove(roleId);
    if (removedRole == null) {
        // getJDA().getEventCache().cache(EventCache.Type.ROLE, roleId, () -> handle(responseNumber, allContent));
        WebSocketClient.LOG.debug("GUILD_ROLE_DELETE was received for a Role that is not yet cached: {}", content);
        return null;
    }
    // Now that the role is removed from the Guild, remove it from all users and emotes.
    guild.getMembersView().forEach(m -> {
        MemberImpl member = (MemberImpl) m;
        member.getRoleSet().remove(removedRole);
    });
    for (Emote emote : guild.getEmoteCache()) {
        EmoteImpl impl = (EmoteImpl) emote;
        if (impl.canProvideRoles())
            impl.getRoleSet().remove(removedRole);
    }
    getJDA().handleEvent(new RoleDeleteEvent(getJDA(), responseNumber, removedRole));
    getJDA().getEventCache().clear(EventCache.Type.ROLE, roleId);
    return null;
}
Also used : Role(net.dv8tion.jda.api.entities.Role) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) EmoteImpl(net.dv8tion.jda.internal.entities.EmoteImpl) RoleDeleteEvent(net.dv8tion.jda.api.events.role.RoleDeleteEvent) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) Emote(net.dv8tion.jda.api.entities.Emote)

Example 7 with MemberImpl

use of net.dv8tion.jda.internal.entities.MemberImpl 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)

Example 8 with MemberImpl

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

the class PresenceUpdateHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    // Ignore events for relationships, presences are guild only to us
    if (content.isNull("guild_id")) {
        log.debug("Received PRESENCE_UPDATE without guild_id. Ignoring event.");
        return null;
    }
    if (api.getCacheFlags().stream().noneMatch(CacheFlag::isPresence))
        return null;
    // Do a pre-check to see if this is for a Guild, and if it is, if the guild is currently locked or not cached.
    final long guildId = content.getUnsignedLong("guild_id");
    if (getJDA().getGuildSetupController().isLocked(guildId))
        return guildId;
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
    if (guild == null) {
        getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Received a PRESENCE_UPDATE for a guild that is not yet cached! GuildId:{} UserId: {}", guildId, content.getObject("user").get("id"));
        return null;
    }
    CacheView.SimpleCacheView<MemberPresenceImpl> presences = guild.getPresenceView();
    if (presences == null)
        // technically this should be impossible
        return null;
    DataObject jsonUser = content.getObject("user");
    final long userId = jsonUser.getUnsignedLong("id");
    MemberImpl member = (MemberImpl) guild.getMemberById(userId);
    MemberPresenceImpl presence = presences.get(userId);
    OnlineStatus status = OnlineStatus.fromKey(content.getString("status"));
    if (status == OnlineStatus.OFFLINE)
        presences.remove(userId);
    if (presence == null) {
        presence = new MemberPresenceImpl();
        if (status != OnlineStatus.OFFLINE) {
            try (UnlockHook lock = presences.writeLock()) {
                presences.getMap().put(userId, presence);
            }
        }
    }
    // Now that we've update the User's info, lets see if we need to set the specific Presence information.
    // This is stored in the Member objects.
    // We set the activities to null to prevent parsing if the cache was disabled
    final DataArray activityArray = !getJDA().isCacheFlagSet(CacheFlag.ACTIVITY) || content.isNull("activities") ? null : content.getArray("activities");
    List<Activity> newActivities = new ArrayList<>();
    boolean parsedActivity = parseActivities(userId, activityArray, newActivities);
    if (getJDA().isCacheFlagSet(CacheFlag.CLIENT_STATUS) && !content.isNull("client_status"))
        handleClientStatus(content, presence);
    // Check if activities changed
    if (parsedActivity)
        handleActivities(newActivities, member, presence);
    if (presence.getOnlineStatus() != status) {
        OnlineStatus oldStatus = presence.getOnlineStatus();
        presence.setOnlineStatus(status);
        if (member != null) {
            getJDA().getEntityBuilder().updateMemberCache(member);
            getJDA().handleEvent(new UserUpdateOnlineStatusEvent(getJDA(), responseNumber, member, oldStatus));
        }
    }
    return null;
}
Also used : CacheFlag(net.dv8tion.jda.api.utils.cache.CacheFlag) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) MemberPresenceImpl(net.dv8tion.jda.internal.entities.MemberPresenceImpl) OnlineStatus(net.dv8tion.jda.api.OnlineStatus) ArrayList(java.util.ArrayList) Activity(net.dv8tion.jda.api.entities.Activity) DataArray(net.dv8tion.jda.api.utils.data.DataArray) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) CacheView(net.dv8tion.jda.api.utils.cache.CacheView) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) UserUpdateOnlineStatusEvent(net.dv8tion.jda.api.events.user.update.UserUpdateOnlineStatusEvent)

Example 9 with MemberImpl

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

the class GuildMemberUpdateHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    final long id = content.getLong("guild_id");
    if (getJDA().getGuildSetupController().isLocked(id))
        return id;
    DataObject userJson = content.getObject("user");
    final long userId = userJson.getLong("id");
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(id);
    if (guild == null) {
        // Do not cache this here, it will be outdated once we receive the GUILD_CREATE and could cause invalid cache
        // getJDA().getEventCache().cache(EventCache.Type.GUILD, userId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Got GuildMember update but JDA currently does not have the Guild cached. Ignoring. {}", content);
        return null;
    }
    MemberImpl member = (MemberImpl) guild.getMembersView().get(userId);
    if (member == null) {
        member = getJDA().getEntityBuilder().createMember(guild, content);
    } else {
        List<Role> newRoles = toRolesList(guild, content.getArray("roles"));
        getJDA().getEntityBuilder().updateMember(guild, member, content, newRoles);
    }
    getJDA().getEntityBuilder().updateMemberCache(member);
    getJDA().handleEvent(new GuildMemberUpdateEvent(getJDA(), responseNumber, member));
    return null;
}
Also used : Role(net.dv8tion.jda.api.entities.Role) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) GuildMemberUpdateEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberUpdateEvent) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl)

Aggregations

MemberImpl (net.dv8tion.jda.internal.entities.MemberImpl)9 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)8 DataObject (net.dv8tion.jda.api.utils.data.DataObject)6 DataArray (net.dv8tion.jda.api.utils.data.DataArray)3 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)3 TLongObjectMap (gnu.trove.map.TLongObjectMap)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 OffsetDateTime (java.time.OffsetDateTime)2 ArrayList (java.util.ArrayList)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 Role (net.dv8tion.jda.api.entities.Role)2 JDAImpl (net.dv8tion.jda.internal.JDAImpl)2 EmoteImpl (net.dv8tion.jda.internal.entities.EmoteImpl)2 List (java.util.List)1 Objects (java.util.Objects)1 Nonnull (javax.annotation.Nonnull)1 OnlineStatus (net.dv8tion.jda.api.OnlineStatus)1 Activity (net.dv8tion.jda.api.entities.Activity)1 AudioChannel (net.dv8tion.jda.api.entities.AudioChannel)1 Emote (net.dv8tion.jda.api.entities.Emote)1