Search in sources :

Example 6 with DataArray

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

the class RoleOrderActionImpl method finalizeData.

@Override
protected RequestBody finalizeData() {
    final Member self = guild.getSelfMember();
    final boolean isOwner = self.isOwner();
    if (!isOwner) {
        if (self.getRoles().isEmpty())
            throw new IllegalStateException("Cannot move roles above your highest role unless you are the guild owner");
        if (!self.hasPermission(Permission.MANAGE_ROLES))
            throw new InsufficientPermissionException(guild, Permission.MANAGE_ROLES);
    }
    DataArray array = DataArray.empty();
    List<Role> ordering = new ArrayList<>(orderList);
    // Normal order is descending, not ascending.
    if (ascendingOrder)
        Collections.reverse(ordering);
    for (int i = 0; i < ordering.size(); i++) {
        Role role = ordering.get(i);
        final int initialPos = role.getPosition();
        if (initialPos != i && !isOwner && !self.canInteract(role))
            // If the current role was moved, we are not owner and we can't interact with the role then throw a PermissionException
            throw new IllegalStateException("Cannot change order: One of the roles could not be moved due to hierarchical power!");
        array.add(DataObject.empty().put("id", role.getId()).put("position", // plus 1 because position 0 is the @everyone position.
        i + 1));
    }
    return getRequestBody(array);
}
Also used : Role(net.dv8tion.jda.api.entities.Role) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) ArrayList(java.util.ArrayList) Member(net.dv8tion.jda.api.entities.Member) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 7 with DataArray

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

the class CommandDataTest method testSubcommand.

@Test
public void testSubcommand() {
    CommandDataImpl command = new CommandDataImpl("mod", "Moderation commands").setDefaultEnabled(true).addSubcommands(new SubcommandData("ban", "Ban a user from this server").addOption(OptionType.USER, "user", "The user to ban", // required before non-required
    true).addOption(OptionType.STRING, "reason", // test that default is false
    "The ban reason").addOption(OptionType.INTEGER, "days", "The duration of the ban", // test with explicit false
    false));
    DataObject data = command.toData();
    Assertions.assertEquals("mod", data.getString("name"));
    Assertions.assertEquals("Moderation commands", data.getString("description"));
    Assertions.assertTrue(data.getBoolean("default_permission"));
    DataObject subdata = data.getArray("options").getObject(0);
    Assertions.assertEquals("ban", subdata.getString("name"));
    Assertions.assertEquals("Ban a user from this server", subdata.getString("description"));
    DataArray options = subdata.getArray("options");
    DataObject option = options.getObject(0);
    Assertions.assertTrue(option.getBoolean("required"));
    Assertions.assertEquals("user", option.getString("name"));
    Assertions.assertEquals("The user to ban", option.getString("description"));
    option = options.getObject(1);
    Assertions.assertFalse(option.getBoolean("required"));
    Assertions.assertEquals("reason", option.getString("name"));
    Assertions.assertEquals("The ban reason", option.getString("description"));
    option = options.getObject(2);
    Assertions.assertFalse(option.getBoolean("required"));
    Assertions.assertEquals("days", option.getString("name"));
    Assertions.assertEquals("The duration of the ban", option.getString("description"));
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Test(org.junit.jupiter.api.Test)

Example 8 with DataArray

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

the class CommandDataTest method testSubcommandGroup.

@Test
public void testSubcommandGroup() {
    CommandDataImpl command = new CommandDataImpl("mod", "Moderation commands").addSubcommandGroups(new SubcommandGroupData("ban", "Ban or unban a user from this server").addSubcommands(new SubcommandData("add", "Ban a user from this server").addOption(OptionType.USER, "user", "The user to ban", // required before non-required
    true).addOption(OptionType.STRING, "reason", // test that default is false
    "The ban reason").addOption(OptionType.INTEGER, "days", "The duration of the ban", // test with explicit false
    false)));
    DataObject data = command.toData();
    Assertions.assertEquals("mod", data.getString("name"));
    Assertions.assertEquals("Moderation commands", data.getString("description"));
    Assertions.assertTrue(data.getBoolean("default_permission"));
    DataObject group = data.getArray("options").getObject(0);
    Assertions.assertEquals("ban", group.getString("name"));
    Assertions.assertEquals("Ban or unban a user from this server", group.getString("description"));
    DataObject subdata = group.getArray("options").getObject(0);
    Assertions.assertEquals("add", subdata.getString("name"));
    Assertions.assertEquals("Ban a user from this server", subdata.getString("description"));
    DataArray options = subdata.getArray("options");
    DataObject option = options.getObject(0);
    Assertions.assertTrue(option.getBoolean("required"));
    Assertions.assertEquals("user", option.getString("name"));
    Assertions.assertEquals("The user to ban", option.getString("description"));
    option = options.getObject(1);
    Assertions.assertFalse(option.getBoolean("required"));
    Assertions.assertEquals("reason", option.getString("name"));
    Assertions.assertEquals("The ban reason", option.getString("description"));
    option = options.getObject(2);
    Assertions.assertFalse(option.getBoolean("required"));
    Assertions.assertEquals("days", option.getString("name"));
    Assertions.assertEquals("The duration of the ban", option.getString("description"));
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) SubcommandGroupData(net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Test(org.junit.jupiter.api.Test)

Example 9 with DataArray

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

the class ThreadListSyncHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    long guildId = content.getLong("guild_id");
    if (api.getGuildSetupController().isLocked(guildId))
        return guildId;
    EntityBuilder entityBuilder = api.getEntityBuilder();
    DataArray threadsArrayJson = content.getArray("threads");
    for (int i = 0; i < threadsArrayJson.length(); i++) {
        DataObject threadJson = threadsArrayJson.getObject(i);
        ThreadChannel thread = entityBuilder.createThreadChannel(threadJson, guildId);
        api.handleEvent(new ThreadRevealedEvent(api, responseNumber, thread));
    }
    return null;
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) ThreadRevealedEvent(net.dv8tion.jda.api.events.thread.ThreadRevealedEvent) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) ThreadChannel(net.dv8tion.jda.api.entities.ThreadChannel) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 10 with DataArray

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

the class BaseGuildMessageChannelMixin method retrieveWebhooks.

@Nonnull
@Override
default RestAction<List<Webhook>> retrieveWebhooks() {
    checkPermission(Permission.MANAGE_WEBHOOKS);
    Route.CompiledRoute route = Route.Channels.GET_WEBHOOKS.compile(getId());
    JDAImpl jda = (JDAImpl) getJDA();
    return new RestActionImpl<>(jda, route, (response, request) -> {
        DataArray array = response.getArray();
        List<Webhook> webhooks = new ArrayList<>(array.length());
        EntityBuilder builder = jda.getEntityBuilder();
        for (int i = 0; i < array.length(); i++) {
            try {
                webhooks.add(builder.createWebhook(array.getObject(i)));
            } catch (UncheckedIOException | NullPointerException e) {
                JDAImpl.LOG.error("Error while creating websocket from json", e);
            }
        }
        return Collections.unmodifiableList(webhooks);
    });
}
Also used : ArrayList(java.util.ArrayList) JDAImpl(net.dv8tion.jda.internal.JDAImpl) UncheckedIOException(java.io.UncheckedIOException) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray) RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) Webhook(net.dv8tion.jda.api.entities.Webhook) Route(net.dv8tion.jda.internal.requests.Route) 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