Search in sources :

Example 31 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray 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 32 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray 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 33 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class AllowedMentionsImpl method toData.

@Nonnull
@Override
public DataObject toData() {
    DataObject allowedMentionsObj = DataObject.empty();
    DataArray parsable = DataArray.empty();
    if (parse != null) {
        // Add parsing options
        parse.stream().map(Message.MentionType::getParseKey).filter(Objects::nonNull).distinct().forEach(parsable::add);
    }
    if (!users.isEmpty()) {
        // Whitelist certain users
        parsable.remove(Message.MentionType.USER.getParseKey());
        allowedMentionsObj.put("users", DataArray.fromCollection(users));
    }
    if (!roles.isEmpty()) {
        // Whitelist certain roles
        parsable.remove(Message.MentionType.ROLE.getParseKey());
        allowedMentionsObj.put("roles", DataArray.fromCollection(roles));
    }
    allowedMentionsObj.put("replied_user", mentionRepliedUser);
    return allowedMentionsObj.put("parse", parsable);
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) DataMessage(net.dv8tion.jda.internal.entities.DataMessage) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Nonnull(javax.annotation.Nonnull)

Example 34 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray 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)

Example 35 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray 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)

Aggregations

DataArray (net.dv8tion.jda.api.utils.data.DataArray)67 DataObject (net.dv8tion.jda.api.utils.data.DataObject)40 Nonnull (javax.annotation.Nonnull)21 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)14 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)10 JDAImpl (net.dv8tion.jda.internal.JDAImpl)9 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)8 ArrayList (java.util.ArrayList)8 Route (net.dv8tion.jda.internal.requests.Route)8 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)7 Collectors (java.util.stream.Collectors)6 CheckReturnValue (javax.annotation.CheckReturnValue)6 TemplateRole (net.dv8tion.jda.api.entities.templates.TemplateRole)5 ParsingException (net.dv8tion.jda.api.exceptions.ParsingException)5 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)5 Bean (at.xirado.bean.Bean)4 java.util (java.util)4 OnlineStatus (net.dv8tion.jda.api.OnlineStatus)4 Template (net.dv8tion.jda.api.entities.templates.Template)4 CacheView (net.dv8tion.jda.api.utils.cache.CacheView)4