Search in sources :

Example 41 with DataArray

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

the class GuildImpl method retrieveRegions.

@Nonnull
@Override
public RestAction<EnumSet<Region>> retrieveRegions(boolean includeDeprecated) {
    Route.CompiledRoute route = Route.Guilds.GET_VOICE_REGIONS.compile(getId());
    return new RestActionImpl<>(getJDA(), route, (response, request) -> {
        EnumSet<Region> set = EnumSet.noneOf(Region.class);
        DataArray arr = response.getArray();
        for (int i = 0; i < arr.length(); i++) {
            DataObject obj = arr.getObject(i);
            if (!includeDeprecated && obj.getBoolean("deprecated"))
                continue;
            String id = obj.getString("id", "");
            Region region = Region.fromKey(id);
            if (region != Region.UNKNOWN)
                set.add(region);
        }
        return set;
    });
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) Region(net.dv8tion.jda.api.Region) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Nonnull(javax.annotation.Nonnull)

Example 42 with DataArray

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

the class GuildImpl method updateCommandPrivilegesById.

@Nonnull
@Override
public RestAction<List<CommandPrivilege>> updateCommandPrivilegesById(@Nonnull String id, @Nonnull Collection<? extends CommandPrivilege> privileges) {
    Checks.isSnowflake(id, "ID");
    Checks.noneNull(privileges, "Privileges");
    Checks.check(privileges.size() <= 10, "Cannot have more than 10 privileges for a command!");
    Route.CompiledRoute route = Route.Interactions.EDIT_COMMAND_PERMISSIONS.compile(getJDA().getSelfUser().getApplicationId(), getId(), id);
    DataArray array = DataArray.fromCollection(privileges);
    return new RestActionImpl<>(getJDA(), route, DataObject.empty().put("permissions", array), (response, request) -> parsePrivilegesList(response.getObject()));
}
Also used : DataArray(net.dv8tion.jda.api.utils.data.DataArray) Nonnull(javax.annotation.Nonnull)

Example 43 with DataArray

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

the class GuildImpl method retrieveWebhooks.

@Nonnull
@Override
public RestAction<List<Webhook>> retrieveWebhooks() {
    if (!getSelfMember().hasPermission(Permission.MANAGE_WEBHOOKS))
        throw new InsufficientPermissionException(this, Permission.MANAGE_WEBHOOKS);
    Route.CompiledRoute route = Route.Guilds.GET_WEBHOOKS.compile(getId());
    return new RestActionImpl<>(getJDA(), route, (response, request) -> {
        DataArray array = response.getArray();
        List<Webhook> webhooks = new ArrayList<>(array.length());
        EntityBuilder builder = api.getEntityBuilder();
        for (int i = 0; i < array.length(); i++) {
            try {
                webhooks.add(builder.createWebhook(array.getObject(i)));
            } catch (Exception e) {
                JDAImpl.LOG.error("Error creating webhook from json", e);
            }
        }
        return Collections.unmodifiableList(webhooks);
    });
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) DataArray(net.dv8tion.jda.api.utils.data.DataArray) HierarchyException(net.dv8tion.jda.api.exceptions.HierarchyException) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) Nonnull(javax.annotation.Nonnull)

Example 44 with DataArray

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

the class GuildImpl method retrieveActiveThreads.

@Nonnull
@Override
public RestAction<List<ThreadChannel>> retrieveActiveThreads() {
    Route.CompiledRoute route = Route.Guilds.LIST_ACTIVE_THREADS.compile(getId());
    return new RestActionImpl<>(api, route, (response, 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++) {
            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, this.getIdLong());
            list.add(thread);
        }
        return Collections.unmodifiableList(list);
    });
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Nonnull(javax.annotation.Nonnull)

Example 45 with DataArray

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

the class GuildImpl method retrieveTemplates.

@Nonnull
@Override
public RestAction<List<Template>> retrieveTemplates() {
    if (!this.getSelfMember().hasPermission(Permission.MANAGE_SERVER))
        throw new InsufficientPermissionException(this, Permission.MANAGE_SERVER);
    final Route.CompiledRoute route = Route.Templates.GET_GUILD_TEMPLATES.compile(getId());
    return new RestActionImpl<>(getJDA(), route, (response, request) -> {
        EntityBuilder entityBuilder = api.getEntityBuilder();
        DataArray array = response.getArray();
        List<Template> templates = new ArrayList<>(array.length());
        for (int i = 0; i < array.length(); i++) {
            try {
                templates.add(entityBuilder.createTemplate(array.getObject(i)));
            } catch (Exception e) {
                JDAImpl.LOG.error("Error creating template from json", e);
            }
        }
        return Collections.unmodifiableList(templates);
    });
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) DataArray(net.dv8tion.jda.api.utils.data.DataArray) HierarchyException(net.dv8tion.jda.api.exceptions.HierarchyException) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) Template(net.dv8tion.jda.api.entities.templates.Template) Nonnull(javax.annotation.Nonnull)

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