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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations