use of net.dv8tion.jda.internal.interactions.CommandDataImpl in project JDA by DV8FromTheWorld.
the class CommandDataTest method testRequiredThrows.
@Test
public void testRequiredThrows() {
CommandDataImpl command = new CommandDataImpl("ban", "Simple ban command");
command.addOption(OptionType.STRING, "opt", "desc");
Assertions.assertThrows(IllegalArgumentException.class, () -> command.addOption(OptionType.STRING, "other", "desc", true));
SubcommandData subcommand = new SubcommandData("sub", "Simple subcommand");
subcommand.addOption(OptionType.STRING, "opt", "desc");
Assertions.assertThrows(IllegalArgumentException.class, () -> subcommand.addOption(OptionType.STRING, "other", "desc", true));
}
use of net.dv8tion.jda.internal.interactions.CommandDataImpl in project JDA by DV8FromTheWorld.
the class CommandDataTest method testNormal.
@Test
public void testNormal() {
CommandData command = new CommandDataImpl("ban", "Ban a user from this server").setDefaultEnabled(false).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("ban", data.getString("name"));
Assertions.assertEquals("Ban a user from this server", data.getString("description"));
Assertions.assertFalse(data.getBoolean("default_permission"));
DataArray options = data.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.internal.interactions.CommandDataImpl in project JDA by DV8FromTheWorld.
the class CommandData method fromData.
/**
* Parses the provided serialization back into an CommandData instance.
* <br>This is the reverse function for {@link CommandData#toData()}.
*
* @param object
* The serialized {@link DataObject} representing the command
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the serialized object is missing required fields
* @throws IllegalArgumentException
* If any of the values are failing the respective checks such as length
*
* @return The parsed CommandData instance, which can be further configured through setters
*
* @see SlashCommandData#fromData(DataObject)
* @see Commands#fromList(Collection)
*/
@Nonnull
static CommandData fromData(@Nonnull DataObject object) {
Checks.notNull(object, "DataObject");
String name = object.getString("name");
Command.Type commandType = Command.Type.fromId(object.getInt("type", 1));
if (commandType != Command.Type.SLASH)
return new CommandDataImpl(commandType, name);
return SlashCommandData.fromData(object);
}
use of net.dv8tion.jda.internal.interactions.CommandDataImpl in project JDA by DV8FromTheWorld.
the class SlashCommandData method fromData.
/**
* Parses the provided serialization back into a SlashCommandData instance.
* <br>This is the reverse function for {@link SlashCommandData#toData()}.
*
* @param object
* The serialized {@link DataObject} representing the command
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the serialized object is missing required fields
* @throws IllegalArgumentException
* If any of the values are failing the respective checks such as length
*
* @return The parsed SlashCommandData instance, which can be further configured through setters
*
* @see CommandData#fromData(DataObject)
* @see Commands#fromList(Collection)
*/
@Nonnull
static SlashCommandData fromData(@Nonnull DataObject object) {
Checks.notNull(object, "DataObject");
String name = object.getString("name");
Command.Type commandType = Command.Type.fromId(object.getInt("type", 1));
if (commandType != Command.Type.SLASH)
throw new IllegalArgumentException("Cannot convert command of type " + commandType + " to SlashCommandData!");
String description = object.getString("description");
DataArray options = object.optArray("options").orElseGet(DataArray::empty);
CommandDataImpl command = new CommandDataImpl(name, description);
options.stream(DataArray::getObject).forEach(opt -> {
OptionType type = OptionType.fromKey(opt.getInt("type"));
switch(type) {
case SUB_COMMAND:
command.addSubcommands(SubcommandData.fromData(opt));
break;
case SUB_COMMAND_GROUP:
command.addSubcommandGroups(SubcommandGroupData.fromData(opt));
break;
default:
command.addOptions(OptionData.fromData(opt));
}
});
return command;
}
use of net.dv8tion.jda.internal.interactions.CommandDataImpl in project JDA by DV8FromTheWorld.
the class SlashCommandData method fromCommand.
/**
* Converts the provided {@link Command} into a SlashCommandData instance.
*
* @param command
* The command to convert
*
* @throws IllegalArgumentException
* If null is provided or the command has illegal configuration
*
* @return An instance of SlashCommandData
*/
@Nonnull
static SlashCommandData fromCommand(@Nonnull Command command) {
Checks.notNull(command, "Command");
if (command.getType() != Command.Type.SLASH)
throw new IllegalArgumentException("Cannot convert command of type " + command.getType() + " to SlashCommandData!");
CommandDataImpl data = new CommandDataImpl(command.getName(), command.getDescription());
data.setDefaultEnabled(command.isDefaultEnabled());
command.getOptions().stream().map(OptionData::fromOption).forEach(data::addOptions);
command.getSubcommands().stream().map(SubcommandData::fromSubcommand).forEach(data::addSubcommands);
command.getSubcommandGroups().stream().map(SubcommandGroupData::fromGroup).forEach(data::addSubcommandGroups);
return data;
}
Aggregations