Search in sources :

Example 6 with EntityBuilder

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

the class ReadyHandler method handleReady.

public void handleReady(DataObject content) {
    EntityBuilder builder = getJDA().getEntityBuilder();
    DataArray privateChannels = content.getArray("private_channels");
    for (int i = 0; i < privateChannels.length(); i++) {
        DataObject chan = privateChannels.getObject(i);
        ChannelType type = ChannelType.fromId(chan.getInt("type"));
        // noinspection SwitchStatementWithTooFewBranches
        switch(type) {
            case PRIVATE:
                builder.createPrivateChannel(chan);
                break;
            default:
                WebSocketClient.LOG.warn("Received a Channel in the private_channels array in READY of an unknown type! Type: {}", type);
        }
    }
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) ChannelType(net.dv8tion.jda.api.entities.ChannelType) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 7 with EntityBuilder

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

the class JDAImpl method retrieveWebhookById.

@Nonnull
@Override
public RestAction<Webhook> retrieveWebhookById(@Nonnull String webhookId) {
    Checks.isSnowflake(webhookId, "Webhook ID");
    Route.CompiledRoute route = Route.Webhooks.GET_WEBHOOK.compile(webhookId);
    return new RestActionImpl<>(this, route, (response, request) -> {
        DataObject object = response.getObject();
        EntityBuilder builder = getEntityBuilder();
        return builder.createWebhook(object);
    });
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) Nonnull(javax.annotation.Nonnull)

Example 8 with EntityBuilder

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

the class MessagePaginationActionImpl method handleSuccess.

@Override
protected void handleSuccess(Response response, Request<List<Message>> request) {
    DataArray array = response.getArray();
    List<Message> messages = new ArrayList<>(array.length());
    EntityBuilder builder = api.getEntityBuilder();
    for (int i = 0; i < array.length(); i++) {
        try {
            Message msg = builder.createMessageWithChannel(array.getObject(i), channel, false);
            messages.add(msg);
        } catch (ParsingException | NullPointerException e) {
            LOG.warn("Encountered an exception in MessagePagination", e);
        } catch (IllegalArgumentException e) {
            if (EntityBuilder.UNKNOWN_MESSAGE_TYPE.equals(e.getMessage()))
                LOG.warn("Skipping unknown message type during pagination", e);
            else
                LOG.warn("Unexpected issue trying to parse message during pagination", e);
        }
    }
    if (order == PaginationOrder.FORWARD)
        Collections.reverse(messages);
    if (useCache)
        cached.addAll(messages);
    if (!messages.isEmpty()) {
        last = messages.get(messages.size() - 1);
        lastKey = last.getIdLong();
    }
    request.onSuccess(messages);
}
Also used : ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) ArrayList(java.util.ArrayList) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 9 with EntityBuilder

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

the class ReactionPaginationActionImpl method handleSuccess.

@Override
protected void handleSuccess(Response response, Request<List<User>> request) {
    final EntityBuilder builder = api.getEntityBuilder();
    final DataArray array = response.getArray();
    final List<User> users = new LinkedList<>();
    for (int i = 0; i < array.length(); i++) {
        try {
            final User user = builder.createUser(array.getObject(i));
            users.add(user);
            if (useCache)
                cached.add(user);
            last = user;
            lastKey = last.getIdLong();
        } catch (ParsingException | NullPointerException e) {
            LOG.warn("Encountered exception in ReactionPagination", e);
        }
    }
    request.onSuccess(users);
}
Also used : User(net.dv8tion.jda.api.entities.User) ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray) LinkedList(java.util.LinkedList)

Example 10 with EntityBuilder

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

the class AuditLogPaginationActionImpl method handleSuccess.

@Override
protected void handleSuccess(Response response, Request<List<AuditLogEntry>> request) {
    DataObject obj = response.getObject();
    DataArray users = obj.getArray("users");
    DataArray webhooks = obj.getArray("webhooks");
    DataArray entries = obj.getArray("audit_log_entries");
    List<AuditLogEntry> list = new ArrayList<>(entries.length());
    EntityBuilder builder = api.getEntityBuilder();
    TLongObjectMap<DataObject> userMap = new TLongObjectHashMap<>();
    for (int i = 0; i < users.length(); i++) {
        DataObject user = users.getObject(i);
        userMap.put(user.getLong("id"), user);
    }
    TLongObjectMap<DataObject> webhookMap = new TLongObjectHashMap<>();
    for (int i = 0; i < webhooks.length(); i++) {
        DataObject webhook = webhooks.getObject(i);
        webhookMap.put(webhook.getLong("id"), webhook);
    }
    for (int i = 0; i < entries.length(); i++) {
        try {
            DataObject entry = entries.getObject(i);
            DataObject user = userMap.get(entry.getLong("user_id", 0));
            DataObject webhook = webhookMap.get(entry.getLong("target_id", 0));
            AuditLogEntry result = builder.createAuditLogEntry((GuildImpl) guild, entry, user, webhook);
            list.add(result);
            if (this.useCache)
                this.cached.add(result);
            this.last = result;
            this.lastKey = last.getIdLong();
        } catch (ParsingException | NullPointerException e) {
            LOG.warn("Encountered exception in AuditLogPagination", e);
        }
    }
    request.onSuccess(list);
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) AuditLogEntry(net.dv8tion.jda.api.audit.AuditLogEntry) ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) ArrayList(java.util.ArrayList) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

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