Search in sources :

Example 6 with SubcommandData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData 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 7 with SubcommandData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData in project JDA by DV8FromTheWorld.

the class CommandDataImpl method addSubcommands.

@Nonnull
@Override
public CommandDataImpl addSubcommands(@Nonnull SubcommandData... subcommands) {
    Checks.noneNull(subcommands, "Subcommands");
    if (subcommands.length == 0)
        return this;
    checkType(Command.Type.SLASH, "add subcommands");
    if (!allowSubcommands)
        throw new IllegalArgumentException("You cannot mix options with subcommands/groups.");
    Checks.check(subcommands.length + options.length() <= 25, "Cannot have more than 25 subcommands for a command!");
    Checks.checkUnique(Stream.concat(getSubcommands().stream(), Arrays.stream(subcommands)).map(SubcommandData::getName), "Cannot have multiple subcommands with the same name. Name: \"%s\" appeared %d times!", (count, value) -> new Object[] { value, count });
    allowOption = false;
    for (SubcommandData data : subcommands) options.add(data);
    return this;
}
Also used : SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) Nonnull(javax.annotation.Nonnull)

Example 8 with SubcommandData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData 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)

Example 9 with SubcommandData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData 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 10 with SubcommandData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData in project Bean by Xirado.

the class CommandsRoute method handle.

@Override
public Object handle(Request request, Response response) throws Exception {
    List<SlashCommand> commands = Bean.getInstance().isDebug() ? Bean.getInstance().getInteractionCommandHandler().getRegisteredGuildCommands().get(DEV_GUILD_ID).stream().filter(cmd -> cmd instanceof SlashCommand).map(cmd -> (SlashCommand) cmd).toList() : Bean.getInstance().getInteractionCommandHandler().getRegisteredSlashCommands();
    DataArray commandArray = DataArray.empty();
    for (SlashCommand command : commands) {
        DataObject commandObject = DataObject.empty();
        SlashCommandData slashCommandData = command.getData();
        DataArray options = DataArray.empty();
        DataArray subCommands = DataArray.empty();
        for (OptionData option : slashCommandData.getOptions()) {
            DataObject optionObject = DataObject.empty().put("name", option.getName()).put("type", option.getType().toString()).put("description", option.getDescription()).put("required", option.isRequired());
            options.add(optionObject);
        }
        for (SubcommandData subcommandData : slashCommandData.getSubcommands()) {
            DataObject subCommandObject = DataObject.empty();
            DataArray subCommandOptions = DataArray.empty();
            for (OptionData option : subcommandData.getOptions()) {
                DataObject optionObject = DataObject.empty().put("name", option.getName()).put("type", option.getType().toString()).put("description", option.getDescription()).put("required", option.isRequired());
                subCommandOptions.add(optionObject);
            }
            subCommandObject.put("name", subcommandData.getName()).put("description", subcommandData.getDescription());
            subCommandObject.put("options", subCommandOptions);
            subCommands.add(subCommandObject);
        }
        commandObject.put("name", slashCommandData.getName()).put("description", slashCommandData.getDescription()).put("options", options).put("sub_commands", subCommands);
        commandArray.add(commandObject);
    }
    return commandArray.toString();
}
Also used : SlashCommand(at.xirado.bean.command.SlashCommand) DataArray(net.dv8tion.jda.api.utils.data.DataArray) List(java.util.List) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) Request(spark.Request) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) Response(spark.Response) SlashCommand(at.xirado.bean.command.SlashCommand) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(spark.Route) Bean(at.xirado.bean.Bean) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) DataObject(net.dv8tion.jda.api.utils.data.DataObject) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) DataArray(net.dv8tion.jda.api.utils.data.DataArray) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData)

Aggregations

SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)12 CommandDataImpl (net.dv8tion.jda.internal.interactions.CommandDataImpl)5 Test (org.junit.jupiter.api.Test)4 List (java.util.List)3 SlashCommandData (net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)3 SubcommandGroupData (net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData)3 DataArray (net.dv8tion.jda.api.utils.data.DataArray)3 DataObject (net.dv8tion.jda.api.utils.data.DataObject)3 Modifier (java.lang.reflect.Modifier)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Nonnull (javax.annotation.Nonnull)2 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)2 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)2 Bean (at.xirado.bean.Bean)1 SlashCommand (at.xirado.bean.command.SlashCommand)1 ISlashCommand (com.dynxsty.dih4jda.commands.interactions.slash_command.ISlashCommand)1 SlashCommandInteraction (com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction)1 ClassPath (com.google.common.reflect.ClassPath)1 BaseCommand (dev.triumphteam.cmd.core.BaseCommand)1