Search in sources :

Example 1 with ISubcommandGroupData

use of net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandGroupData in project Utility by anweisen.

the class SlashCommandHelper method acquireCommand.

@Nonnull
private IBaseCommandData<?, ?> acquireCommand(@Nonnull RegisteredCommand command) {
    String name = command.getOptions().getFirstName();
    List<String> subcommandNames = new ArrayList<>(Arrays.asList(name.split(" ")));
    String parentName = subcommandNames.get(0);
    ICommandData parentRootCommand = slashCommands.computeIfAbsent(parentName, key -> new ICommandData(parentName));
    switch(subcommandNames.size()) {
        default:
            throw new IllegalArgumentException("Discord only supports 3 chained command names for subcommands in slashcommands (got " + subcommandNames.size() + ": '" + name + "')");
        case 1:
            return parentRootCommand;
        case 2:
            {
                String commandName = subcommandNames.get(1);
                ISubcommandData commandData = new ISubcommandData(commandName);
                parentRootCommand.addSubcommand(commandData);
                return commandData;
            }
        case 3:
            {
                String subCommandName = subcommandNames.get(1);
                ISubcommandGroupData subcommandGroup = parentRootCommand.getSubcommandGroups().stream().filter(data -> data.getName().equals(subCommandName)).findFirst().orElseGet(() -> {
                    ISubcommandGroupData result = new ISubcommandGroupData(subCommandName);
                    parentRootCommand.addSubcommandGroup(result);
                    return result;
                });
                String commandName = subcommandNames.get(2);
                ISubcommandData commandData = new ISubcommandData(commandName);
                subcommandGroup.addSubcommand(commandData);
                return commandData;
            }
    }
}
Also used : PrintWriter(java.io.PrintWriter) IBaseCommandData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.IBaseCommandData) java.util(java.util) JDA(net.dv8tion.jda.api.JDA) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) ISubcommandGroupData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandGroupData) WrappedException(net.anweisen.utilities.common.collection.WrappedException) RequiredArgument(net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument) RegisteredCommand(net.anweisen.utilities.jda.manager.hooks.registered.RegisteredCommand) ICommandData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ICommandData) ISubcommandData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandData) CommandManager(net.anweisen.utilities.jda.manager.CommandManager) Nonnull(javax.annotation.Nonnull) StringBuilderPrintWriter(net.anweisen.utilities.common.collection.StringBuilderPrintWriter) ISubcommandData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandData) ISubcommandGroupData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandGroupData) ICommandData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ICommandData) Nonnull(javax.annotation.Nonnull)

Aggregations

PrintWriter (java.io.PrintWriter)1 java.util (java.util)1 Nonnull (javax.annotation.Nonnull)1 StringBuilderPrintWriter (net.anweisen.utilities.common.collection.StringBuilderPrintWriter)1 WrappedException (net.anweisen.utilities.common.collection.WrappedException)1 CommandManager (net.anweisen.utilities.jda.manager.CommandManager)1 RegisteredCommand (net.anweisen.utilities.jda.manager.hooks.registered.RegisteredCommand)1 RequiredArgument (net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument)1 IBaseCommandData (net.anweisen.utilities.jda.manager.impl.slashcommands.build.IBaseCommandData)1 ICommandData (net.anweisen.utilities.jda.manager.impl.slashcommands.build.ICommandData)1 ISubcommandData (net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandData)1 ISubcommandGroupData (net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandGroupData)1 JDA (net.dv8tion.jda.api.JDA)1 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)1 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)1