Search in sources :

Example 6 with ParsingException

use of net.dv8tion.jda.api.exceptions.ParsingException 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 7 with ParsingException

use of net.dv8tion.jda.api.exceptions.ParsingException in project JDA by DV8FromTheWorld.

the class WebSocketClient method onDispatch.

protected void onDispatch(DataObject raw) {
    String type = raw.getString("t");
    long responseTotal = api.getResponseTotal();
    if (!raw.isType("d", DataType.OBJECT)) {
        // Needs special handling due to content of "d" being an array
        if (type.equals("PRESENCES_REPLACE")) {
            final DataArray payload = raw.getArray("d");
            final List<DataObject> converted = convertPresencesReplace(responseTotal, payload);
            final SocketHandler handler = getHandler("PRESENCE_UPDATE");
            LOG.trace("{} -> {}", type, payload);
            for (DataObject o : converted) {
                handler.handle(responseTotal, o);
                // Send raw event after cache has been updated - including comment
                if (api.isRawEvents())
                    api.handleEvent(new RawGatewayEvent(api, responseTotal, o));
            }
        } else {
            LOG.debug("Received event with unhandled body type JSON: {}", raw);
        }
        return;
    }
    DataObject content = raw.getObject("d");
    LOG.trace("{} -> {}", type, content);
    JDAImpl jda = (JDAImpl) getJDA();
    try {
        switch(type) {
            // INIT types
            case "READY":
                reconnectTimeoutS = 2;
                api.setStatus(JDA.Status.LOADING_SUBSYSTEMS);
                processingReady = true;
                handleIdentifyRateLimit = false;
                // first handle the ready payload before applying the session id
                // this prevents a possible race condition with the cache of the guild setup controller
                // otherwise the audio connection requests that are currently pending might be removed in the process
                handlers.get("READY").handle(responseTotal, raw);
                sessionId = content.getString("session_id");
                break;
            case "RESUMED":
                reconnectTimeoutS = 2;
                sentAuthInfo = true;
                if (!processingReady) {
                    initiating = false;
                    ready();
                } else {
                    LOG.debug("Resumed while still processing initial ready");
                    jda.setStatus(JDA.Status.LOADING_SUBSYSTEMS);
                }
                break;
            default:
                long guildId = content.getLong("guild_id", 0L);
                if (api.isUnavailable(guildId) && !type.equals("GUILD_CREATE") && !type.equals("GUILD_DELETE")) {
                    LOG.debug("Ignoring {} for unavailable guild with id {}. JSON: {}", type, guildId, content);
                    break;
                }
                SocketHandler handler = handlers.get(type);
                if (handler != null)
                    handler.handle(responseTotal, raw);
                else
                    LOG.debug("Unrecognized event:\n{}", raw);
        }
        // Send raw event after cache has been updated
        if (api.isRawEvents())
            api.handleEvent(new RawGatewayEvent(api, responseTotal, raw));
    } catch (ParsingException ex) {
        LOG.warn("Got an unexpected Json-parse error. Please redirect the following message to the devs:\n\tJDA {}\n\t{}\n\t{} -> {}", JDAInfo.VERSION, ex.getMessage(), type, content, ex);
    } catch (Exception ex) {
        LOG.error("Got an unexpected error. Please redirect the following message to the devs:\n\tJDA {}\n\t{} -> {}", JDAInfo.VERSION, type, content, ex);
    }
    if (responseTotal % EventCache.TIMEOUT_AMOUNT == 0)
        jda.getEventCache().timeout(responseTotal);
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) JDAImpl(net.dv8tion.jda.internal.JDAImpl) DataArray(net.dv8tion.jda.api.utils.data.DataArray) ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException)

Aggregations

ParsingException (net.dv8tion.jda.api.exceptions.ParsingException)7 DataArray (net.dv8tion.jda.api.utils.data.DataArray)5 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)4 ArrayList (java.util.ArrayList)3 DataObject (net.dv8tion.jda.api.utils.data.DataObject)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 DefaultIndenter (com.fasterxml.jackson.core.util.DefaultIndenter)2 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 Nonnull (javax.annotation.Nonnull)2 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 LinkedList (java.util.LinkedList)1 DataFormatException (java.util.zip.DataFormatException)1 AuditLogEntry (net.dv8tion.jda.api.audit.AuditLogEntry)1 ThreadChannel (net.dv8tion.jda.api.entities.ThreadChannel)1 User (net.dv8tion.jda.api.entities.User)1 JDAImpl (net.dv8tion.jda.internal.JDAImpl)1