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