Search in sources :

Example 1 with CommandDataImpl

use of net.dv8tion.jda.internal.interactions.CommandDataImpl 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 2 with CommandDataImpl

use of net.dv8tion.jda.internal.interactions.CommandDataImpl 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 3 with CommandDataImpl

use of net.dv8tion.jda.internal.interactions.CommandDataImpl in project JDA by DV8FromTheWorld.

the class CommandDataTest method testNameChecks.

@Test
public void testNameChecks() {
    Assertions.assertThrows(IllegalArgumentException.class, () -> new CommandDataImpl("invalid name", "Valid description"));
    Assertions.assertThrows(IllegalArgumentException.class, () -> new CommandDataImpl("invalidName", "Valid description"));
    Assertions.assertThrows(IllegalArgumentException.class, () -> new CommandDataImpl("valid_name", ""));
    Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandData("invalid name", "Valid description"));
    Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandData("invalidName", "Valid description"));
    Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandData("valid_name", ""));
    Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandGroupData("invalid name", "Valid description"));
    Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandGroupData("invalidName", "Valid description"));
    Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandGroupData("valid_name", ""));
}
Also used : CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) SubcommandGroupData(net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData) Test(org.junit.jupiter.api.Test)

Example 4 with CommandDataImpl

use of net.dv8tion.jda.internal.interactions.CommandDataImpl in project JDA by DV8FromTheWorld.

the class CommandEditActionImpl method finalizeData.

@Override
protected RequestBody finalizeData() {
    DataObject json = data.toData();
    if (isUnchanged(NAME_SET))
        json.remove("name");
    if (isUnchanged(DESCRIPTION_SET))
        json.remove("description");
    if (isUnchanged(OPTIONS_SET))
        json.remove("options");
    mask = 0;
    data = new CommandDataImpl(UNDEFINED, UNDEFINED);
    return getRequestBody(json);
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl)

Example 5 with CommandDataImpl

use of net.dv8tion.jda.internal.interactions.CommandDataImpl in project Lauren by Yuhtin.

the class CommandRegistry method register.

public void register() {
    ClassPath classPath;
    try {
        classPath = ClassPath.from(getClass().getClassLoader());
    } catch (IOException exception) {
        logger.severe("ClassPath could not be instantiated");
        return;
    }
    val infoCacher = InfoCacher.getInstance();
    infoCacher.start();
    val commands = new HashMap<String, CommandDataImpl>();
    val commandMap = Startup.getLauren().getCommandCatcher().getCommandMap();
    for (val info : classPath.getTopLevelClassesRecursive("com.yuhtin.lauren.commands.impl")) {
        try {
            val name = Class.forName(info.getName());
            val object = name.newInstance();
            if (name.isAnnotationPresent(CommandInfo.class)) {
                val command = (Command) object;
                injector.injectMembers(command);
                val data = (CommandInfo) name.getAnnotation(CommandInfo.class);
                var commandName = data.name();
                commandMap.register(commandName, command);
                var subCommand = "";
                if (commandName.contains(".")) {
                    val split = commandName.split("\\.");
                    commandName = split[0];
                    subCommand = split[1];
                }
                val commandData = commands.getOrDefault(commandName, new CommandDataImpl(commandName, data.description()));
                if (!subCommand.equalsIgnoreCase("")) {
                    val subcommandData = new SubcommandData(subCommand, data.description());
                    commandData.addSubcommands(subcommandData);
                    argsInterpreter(data, null, subcommandData);
                } else {
                    argsInterpreter(data, commandData, null);
                }
                if (commands.containsKey(commandName))
                    commands.replace(commandName, commandData);
                else
                    commands.put(commandName, commandData);
                infoCacher.insert(data);
            } else
                throw new InstantiationException();
        } catch (Exception exception) {
            logger.log(LogType.SEVERE, "The " + info.getName() + " class could not be instantiated", exception);
        }
    }
    infoCacher.construct();
    client.retrieveCommands().queue(createdCommands -> {
        for (val command : commands.values()) {
            boolean exists = false;
            for (val createdCommand : createdCommands) {
                if (createdCommand.getName().equals(command.getName())) {
                    exists = true;
                    val createdSubcommands = createdCommand.getSubcommands().stream().map(Subcommand::getName).collect(Collectors.toList());
                    for (val commandSubcommand : command.getSubcommands()) {
                        if (!createdSubcommands.contains(commandSubcommand.getName())) {
                            exists = false;
                        }
                    }
                    if (!createdCommand.getDescription().equals(command.getDescription())) {
                        exists = false;
                    }
                }
            }
            if (!exists) {
                logger.info("Adding " + command.getName() + " because is a new command.");
                client.upsertCommand(command).queue();
            }
        }
    });
    logger.info("Registered " + commandMap.getCommands().size() + " commands successfully");
}
Also used : lombok.val(lombok.val) ClassPath(com.google.common.reflect.ClassPath) HashMap(java.util.HashMap) lombok.var(lombok.var) IOException(java.io.IOException) CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) IOException(java.io.IOException)

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