Search in sources :

Example 11 with EntityBuilder

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

the class ThreadChannelPaginationActionImpl method handleSuccess.

@Override
protected void handleSuccess(Response response, Request<List<ThreadChannel>> request) {
    DataObject obj = response.getObject();
    DataArray selfThreadMembers = obj.getArray("members");
    DataArray threads = obj.getArray("threads");
    List<ThreadChannel> list = new ArrayList<>(threads.length());
    EntityBuilder builder = api.getEntityBuilder();
    TLongObjectMap<DataObject> selfThreadMemberMap = new TLongObjectHashMap<>();
    for (int i = 0; i < selfThreadMembers.length(); i++) {
        DataObject selfThreadMember = selfThreadMembers.getObject(i);
        // Store the thread member based on the "id" which is the _thread's_ id, not the member's id (which would be our id)
        selfThreadMemberMap.put(selfThreadMember.getLong("id"), selfThreadMember);
    }
    for (int i = 0; i < threads.length(); i++) {
        try {
            DataObject threadObj = threads.getObject(i);
            DataObject selfThreadMemberObj = selfThreadMemberMap.get(threadObj.getLong("id", 0));
            if (selfThreadMemberObj != null) {
                // Combine the thread and self thread-member into a single object to model what we get from
                // thread payloads (like from Gateway, etc)
                threadObj.put("member", selfThreadMemberObj);
            }
            ThreadChannel thread = builder.createThreadChannel(threadObj, getGuild().getIdLong());
            list.add(thread);
            if (this.useCache)
                this.cached.add(thread);
            this.last = thread;
            this.lastKey = last.getIdLong();
        } catch (ParsingException | NullPointerException e) {
            LOG.warn("Encountered exception in ThreadChannelPagination", e);
        }
    }
    request.onSuccess(list);
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) ArrayList(java.util.ArrayList) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) ThreadChannel(net.dv8tion.jda.api.entities.ThreadChannel) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 12 with EntityBuilder

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

the class GuildMembersChunkHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    final long guildId = content.getLong("guild_id");
    DataArray members = content.getArray("members");
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
    if (guild != null) {
        if (api.getClient().getChunkManager().handleChunk(guildId, content))
            return null;
        WebSocketClient.LOG.debug("Received member chunk for guild that is already in cache. GuildId: {} Count: {} Index: {}/{}", guildId, members.length(), content.getInt("chunk_index"), content.getInt("chunk_count"));
        // Chunk handling
        EntityBuilder builder = getJDA().getEntityBuilder();
        TLongObjectMap<DataObject> presences = content.optArray("presences").map(it -> builder.convertToUserMap(o -> o.getObject("user").getUnsignedLong("id"), it)).orElseGet(TLongObjectHashMap::new);
        for (int i = 0; i < members.length(); i++) {
            DataObject object = members.getObject(i);
            long userId = object.getObject("user").getUnsignedLong("id");
            DataObject presence = presences.get(userId);
            MemberImpl member = builder.createMember(guild, object, null, presence);
            builder.updateMemberCache(member);
        }
        return null;
    }
    getJDA().getGuildSetupController().onMemberChunk(guildId, content);
    return null;
}
Also used : TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) DataArray(net.dv8tion.jda.api.utils.data.DataArray) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) WebSocketClient(net.dv8tion.jda.internal.requests.WebSocketClient) JDAImpl(net.dv8tion.jda.internal.JDAImpl) TLongObjectMap(gnu.trove.map.TLongObjectMap) DataObject(net.dv8tion.jda.api.utils.data.DataObject) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 13 with EntityBuilder

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

the class MessageUpdateHandler method handleMessageEmbed.

private Long handleMessageEmbed(DataObject content) {
    EntityBuilder builder = getJDA().getEntityBuilder();
    final long messageId = content.getLong("id");
    final long channelId = content.getLong("channel_id");
    LinkedList<MessageEmbed> embeds = new LinkedList<>();
    // TODO-v5-unified-channel-cache
    // TODO-v5: handle for threads.
    MessageChannel channel = getJDA().getTextChannelsView().get(channelId);
    if (channel == null)
        channel = getJDA().getNewsChannelById(channelId);
    if (channel == null)
        channel = getJDA().getPrivateChannelsView().get(channelId);
    if (channel == null) {
        getJDA().getEventCache().cache(EventCache.Type.CHANNEL, channelId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Received message update for embeds for a channel/group that JDA does not have cached yet.");
        return null;
    }
    DataArray embedsJson = content.getArray("embeds");
    for (int i = 0; i < embedsJson.length(); i++) embeds.add(builder.createMessageEmbed(embedsJson.getObject(i)));
    getJDA().handleEvent(new MessageEmbedEvent(getJDA(), responseNumber, messageId, channel, embeds));
    return null;
}
Also used : MessageEmbedEvent(net.dv8tion.jda.api.events.message.MessageEmbedEvent) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) LinkedList(java.util.LinkedList) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 14 with EntityBuilder

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

the class ReadyHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    EntityBuilder builder = getJDA().getEntityBuilder();
    DataArray guilds = content.getArray("guilds");
    // Make sure we don't have any duplicates here!
    TLongObjectMap<DataObject> distinctGuilds = new TLongObjectHashMap<>();
    for (int i = 0; i < guilds.length(); i++) {
        DataObject guild = guilds.getObject(i);
        long id = guild.getUnsignedLong("id");
        DataObject previous = distinctGuilds.put(id, guild);
        if (previous != null)
            WebSocketClient.LOG.warn("Found duplicate guild for id {} in ready payload", id);
    }
    DataObject selfJson = content.getObject("user");
    // Used to update SelfUser#getApplicationId
    selfJson.put(// Used to update SelfUser#getApplicationId
    "application_id", content.optObject("application").map(obj -> obj.getUnsignedLong("id")).orElse(selfJson.getUnsignedLong("id")));
    builder.createSelfUser(selfJson);
    if (getJDA().getGuildSetupController().setIncompleteCount(distinctGuilds.size())) {
        distinctGuilds.forEachEntry((id, guild) -> {
            getJDA().getGuildSetupController().onReady(id, guild);
            return true;
        });
    }
    handleReady(content);
    return null;
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 15 with EntityBuilder

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

the class IInviteContainerMixin method retrieveInvites.

@Nonnull
@Override
default RestAction<List<Invite>> retrieveInvites() {
    checkPermission(Permission.MANAGE_CHANNEL);
    final Route.CompiledRoute route = Route.Invites.GET_CHANNEL_INVITES.compile(getId());
    JDAImpl jda = (JDAImpl) getJDA();
    return new RestActionImpl<>(jda, route, (response, request) -> {
        EntityBuilder entityBuilder = jda.getEntityBuilder();
        DataArray array = response.getArray();
        List<Invite> invites = new ArrayList<>(array.length());
        for (int i = 0; i < array.length(); i++) invites.add(entityBuilder.createInvite(array.getObject(i)));
        return Collections.unmodifiableList(invites);
    });
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) ArrayList(java.util.ArrayList) JDAImpl(net.dv8tion.jda.internal.JDAImpl) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) Route(net.dv8tion.jda.internal.requests.Route) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Invite(net.dv8tion.jda.api.entities.Invite) Nonnull(javax.annotation.Nonnull)

Aggregations

EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)19 DataArray (net.dv8tion.jda.api.utils.data.DataArray)15 DataObject (net.dv8tion.jda.api.utils.data.DataObject)9 ArrayList (java.util.ArrayList)7 Nonnull (javax.annotation.Nonnull)7 JDAImpl (net.dv8tion.jda.internal.JDAImpl)7 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)5 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)5 Route (net.dv8tion.jda.internal.requests.Route)5 ParsingException (net.dv8tion.jda.api.exceptions.ParsingException)4 CheckReturnValue (javax.annotation.CheckReturnValue)3 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)3 MemberImpl (net.dv8tion.jda.internal.entities.MemberImpl)3 TLongObjectMap (gnu.trove.map.TLongObjectMap)2 LinkedList (java.util.LinkedList)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 ThreadChannel (net.dv8tion.jda.api.entities.ThreadChannel)2 User (net.dv8tion.jda.api.entities.User)2 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)2 MissingAccessException (net.dv8tion.jda.api.exceptions.MissingAccessException)2