Search in sources :

Example 21 with DataArray

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

the class GuildSetupController method onMemberChunk.

public void onMemberChunk(long id, DataObject chunk) {
    DataArray members = chunk.getArray("members");
    int index = chunk.getInt("chunk_index");
    int count = chunk.getInt("chunk_count");
    log.debug("Received member chunk for guild id: {} size: {} index: {}/{}", id, members.length(), index, count);
    GuildSetupNode node = setupNodes.get(id);
    if (node != null)
        node.handleMemberChunk(MemberChunkManager.isLastChunk(chunk), members);
}
Also used : DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 22 with DataArray

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

the class ThreadChannelImpl method retrieveThreadMembers.

@Override
public RestAction<List<ThreadMember>> retrieveThreadMembers() {
    // TODO-threads: This interacts with GUILD_MEMBERS in some way. Need to test and document how.
    Route.CompiledRoute route = Route.Channels.LIST_THREAD_MEMBERS.compile(getId());
    return new RestActionImpl<>(getJDA(), route, (response, request) -> {
        EntityBuilder builder = api.getEntityBuilder();
        List<ThreadMember> threadMembers = new LinkedList<>();
        DataArray memberArr = response.getArray();
        for (int i = 0; i < memberArr.length(); i++) {
            final DataObject object = memberArr.getObject(i);
            // Very possible this is gonna break because we don't get user/member info with threadmembers
            // TODO revisit the @Nonnull annotations in ThreadMember due to the lack of user/member info. Might be a time to introduce RestX entities?
            threadMembers.add(builder.createThreadMember((GuildImpl) this.getGuild(), this, object));
        }
        return Collections.unmodifiableList(threadMembers);
    });
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(net.dv8tion.jda.internal.requests.Route) LinkedList(java.util.LinkedList) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 23 with DataArray

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

the class MessageEmbed method toData.

/**
 * Creates a new {@link net.dv8tion.jda.api.utils.data.DataObject}
 * used for sending.
 *
 * @return JSONObject for this embed
 */
@Nonnull
@Override
public DataObject toData() {
    if (json != null)
        return json;
    synchronized (mutex) {
        if (json != null)
            return json;
        DataObject obj = DataObject.empty();
        if (url != null)
            obj.put("url", url);
        if (title != null)
            obj.put("title", title);
        if (description != null)
            obj.put("description", description);
        if (timestamp != null)
            obj.put("timestamp", timestamp.format(DateTimeFormatter.ISO_INSTANT));
        if (color != Role.DEFAULT_COLOR_RAW)
            obj.put("color", color & 0xFFFFFF);
        if (thumbnail != null)
            obj.put("thumbnail", DataObject.empty().put("url", thumbnail.getUrl()));
        if (siteProvider != null) {
            DataObject siteProviderObj = DataObject.empty();
            if (siteProvider.getName() != null)
                siteProviderObj.put("name", siteProvider.getName());
            if (siteProvider.getUrl() != null)
                siteProviderObj.put("url", siteProvider.getUrl());
            obj.put("provider", siteProviderObj);
        }
        if (author != null) {
            DataObject authorObj = DataObject.empty();
            if (author.getName() != null)
                authorObj.put("name", author.getName());
            if (author.getUrl() != null)
                authorObj.put("url", author.getUrl());
            if (author.getIconUrl() != null)
                authorObj.put("icon_url", author.getIconUrl());
            obj.put("author", authorObj);
        }
        if (videoInfo != null)
            obj.put("video", DataObject.empty().put("url", videoInfo.getUrl()));
        if (footer != null) {
            DataObject footerObj = DataObject.empty();
            if (footer.getText() != null)
                footerObj.put("text", footer.getText());
            if (footer.getIconUrl() != null)
                footerObj.put("icon_url", footer.getIconUrl());
            obj.put("footer", footerObj);
        }
        if (image != null)
            obj.put("image", DataObject.empty().put("url", image.getUrl()));
        if (!fields.isEmpty()) {
            DataArray fieldsArray = DataArray.empty();
            for (Field field : fields) {
                fieldsArray.add(DataObject.empty().put("name", field.getName()).put("value", field.getValue()).put("inline", field.isInline()));
            }
            obj.put("fields", fieldsArray);
        }
        return json = obj;
    }
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Nonnull(javax.annotation.Nonnull)

Example 24 with DataArray

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

the class EntityBuilder method map.

private <T> List<T> map(DataObject jsonObject, String key, Function<DataObject, T> convert) {
    if (jsonObject.isNull(key))
        return Collections.emptyList();
    final DataArray arr = jsonObject.getArray(key);
    final List<T> mappedObjects = new ArrayList<>(arr.length());
    for (int i = 0; i < arr.length(); i++) {
        DataObject obj = arr.getObject(i);
        T result = convert.apply(obj);
        if (result != null)
            mappedObjects.add(result);
    }
    return mappedObjects;
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 25 with DataArray

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

the class EntityBuilder method createActivity.

public static Activity createActivity(DataObject gameJson) {
    String name = String.valueOf(gameJson.get("name"));
    String url = gameJson.isNull("url") ? null : String.valueOf(gameJson.get("url"));
    Activity.ActivityType type;
    try {
        type = gameJson.isNull("type") ? Activity.ActivityType.PLAYING : Activity.ActivityType.fromKey(Integer.parseInt(gameJson.get("type").toString()));
    } catch (NumberFormatException e) {
        type = Activity.ActivityType.PLAYING;
    }
    RichPresence.Timestamps timestamps = null;
    if (!gameJson.isNull("timestamps")) {
        DataObject obj = gameJson.getObject("timestamps");
        long start, end;
        start = obj.getLong("start", 0L);
        end = obj.getLong("end", 0L);
        timestamps = new RichPresence.Timestamps(start, end);
    }
    Activity.Emoji emoji = null;
    if (!gameJson.isNull("emoji")) {
        DataObject emojiJson = gameJson.getObject("emoji");
        String emojiName = emojiJson.getString("name");
        long emojiId = emojiJson.getUnsignedLong("id", 0);
        boolean emojiAnimated = emojiJson.getBoolean("animated");
        emoji = new Activity.Emoji(emojiName, emojiId, emojiAnimated);
    }
    if (type == Activity.ActivityType.CUSTOM_STATUS) {
        if (gameJson.hasKey("state") && name.equalsIgnoreCase("Custom Status")) {
            name = gameJson.getString("state", "");
            gameJson = gameJson.remove("state");
        }
    }
    if (!CollectionUtils.containsAny(gameJson.keys(), richGameFields))
        return new ActivityImpl(name, url, type, timestamps, emoji);
    // data for spotify
    long id = gameJson.getLong("application_id", 0L);
    String sessionId = gameJson.getString("session_id", null);
    String syncId = gameJson.getString("sync_id", null);
    int flags = gameJson.getInt("flags", 0);
    String details = gameJson.isNull("details") ? null : String.valueOf(gameJson.get("details"));
    String state = gameJson.isNull("state") ? null : String.valueOf(gameJson.get("state"));
    RichPresence.Party party = null;
    if (!gameJson.isNull("party")) {
        DataObject obj = gameJson.getObject("party");
        String partyId = obj.isNull("id") ? null : obj.getString("id");
        DataArray sizeArr = obj.isNull("size") ? null : obj.getArray("size");
        long size = 0, max = 0;
        if (sizeArr != null && sizeArr.length() > 0) {
            size = sizeArr.getLong(0);
            max = sizeArr.length() < 2 ? 0 : sizeArr.getLong(1);
        }
        party = new RichPresence.Party(partyId, size, max);
    }
    String smallImageKey = null, smallImageText = null;
    String largeImageKey = null, largeImageText = null;
    if (!gameJson.isNull("assets")) {
        DataObject assets = gameJson.getObject("assets");
        if (!assets.isNull("small_image")) {
            smallImageKey = String.valueOf(assets.get("small_image"));
            smallImageText = assets.isNull("small_text") ? null : String.valueOf(assets.get("small_text"));
        }
        if (!assets.isNull("large_image")) {
            largeImageKey = String.valueOf(assets.get("large_image"));
            largeImageText = assets.isNull("large_text") ? null : String.valueOf(assets.get("large_text"));
        }
    }
    return new RichPresenceImpl(type, name, url, id, emoji, party, details, state, timestamps, syncId, sessionId, flags, largeImageKey, largeImageText, smallImageKey, smallImageText);
}
Also used : DataArray(net.dv8tion.jda.api.utils.data.DataArray) DataObject(net.dv8tion.jda.api.utils.data.DataObject)

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