Search in sources :

Example 6 with CommandDataImpl

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));
}
Also used : CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) Test(org.junit.jupiter.api.Test)

Example 7 with CommandDataImpl

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"));
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Test(org.junit.jupiter.api.Test)

Example 8 with CommandDataImpl

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);
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command) CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) Nonnull(javax.annotation.Nonnull)

Example 9 with CommandDataImpl

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;
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command) CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) DataArray(net.dv8tion.jda.api.utils.data.DataArray) OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Nonnull(javax.annotation.Nonnull)

Example 10 with CommandDataImpl

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;
}
Also used : CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) Nonnull(javax.annotation.Nonnull)

Aggregations

CommandDataImpl (net.dv8tion.jda.internal.interactions.CommandDataImpl)11 SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)5 Test (org.junit.jupiter.api.Test)5 Nonnull (javax.annotation.Nonnull)4 DataArray (net.dv8tion.jda.api.utils.data.DataArray)4 DataObject (net.dv8tion.jda.api.utils.data.DataObject)4 Command (net.dv8tion.jda.api.interactions.commands.Command)2 SubcommandGroupData (net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData)2 ClassPath (com.google.common.reflect.ClassPath)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 lombok.val (lombok.val)1 lombok.var (lombok.var)1 OptionType (net.dv8tion.jda.api.interactions.commands.OptionType)1 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)1