Search in sources :

Example 1 with SubcommandData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData in project triumph-cmds by TriumphTeam.

the class SlashCommand method asCommandData.

@NotNull
public SlashCommandData asCommandData() {
    final SlashCommandData commandData = Commands.slash(name, description);
    commandData.setDefaultEnabled(enabledRoles.isEmpty());
    if (isDefault) {
        final SlashSubCommand<S> subCommand = getDefaultSubCommand();
        // Should never be null.
        if (subCommand == null)
            throw new CommandRegistrationException("Could not find default subcommand");
        commandData.addOptions(subCommand.getJdaOptions());
        return commandData;
    }
    final List<SubcommandData> subData = subCommands.entrySet().stream().map(entry -> new SubcommandData(entry.getKey(), entry.getValue().getDescription()).addOptions(entry.getValue().getJdaOptions())).collect(Collectors.toList());
    commandData.addSubcommands(subData);
    return commandData;
}
Also used : Command(dev.triumphteam.cmd.core.Command) NamedArgumentRegistry(dev.triumphteam.cmd.core.argument.named.NamedArgumentRegistry) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) ExecutionProvider(dev.triumphteam.cmd.core.execution.ExecutionProvider) ArgumentRegistry(dev.triumphteam.cmd.core.argument.ArgumentRegistry) MessageRegistry(dev.triumphteam.cmd.core.message.MessageRegistry) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) BaseCommand(dev.triumphteam.cmd.core.BaseCommand) CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Default(dev.triumphteam.cmd.core.annotation.Default) ChoiceRegistry(dev.triumphteam.cmd.slash.choices.ChoiceRegistry) Registry(dev.triumphteam.cmd.core.registry.Registry) RequirementRegistry(dev.triumphteam.cmd.core.requirement.RequirementRegistry) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull) SenderValidator(dev.triumphteam.cmd.core.sender.SenderValidator) Method(java.lang.reflect.Method) CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SubcommandData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData in project Utility by anweisen.

the class ISubcommandData method convert.

@Nonnull
public SubcommandData convert() {
    SubcommandData data = new SubcommandData(name, " ");
    options.forEach(data::addOptions);
    return data;
}
Also used : SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) Nonnull(javax.annotation.Nonnull)

Example 3 with SubcommandData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData in project DIH4JDA by DynxstyGIT.

the class InteractionHandler method getSubcommandData.

/**
 * Gets all {@link SubcommandData} from the given array of {@link Subcommand} classes.
 *
 * @param command      The base command's instance.
 * @param subClasses   All sub command classes.
 * @param subGroupName The Subcommand Group's name. (if available)
 * @param guild        The current guild (if available)
 * @return The new {@link CommandListUpdateAction}.
 * @throws Exception If an error occurs.
 */
private Set<SubcommandData> getSubcommandData(BaseSlashCommand command, Class<? extends Subcommand>[] subClasses, @Nullable String subGroupName, @Nullable Guild guild) throws Exception {
    Set<SubcommandData> subDataList = new HashSet<>();
    for (Class<? extends Subcommand> sub : subClasses) {
        Subcommand instance = (Subcommand) this.getClassInstance(guild, sub);
        if (instance.getSubcommandData() == null) {
            DIH4JDALogger.warn(String.format("Class %s is missing SubcommandData. It will be ignored.", sub.getName()));
            continue;
        }
        String commandPath;
        if (subGroupName == null) {
            commandPath = buildCommandPath(command.getCommandData().getName(), instance.getSubcommandData().getName());
        } else {
            commandPath = buildCommandPath(command.getCommandData().getName(), subGroupName, instance.getSubcommandData().getName());
        }
        slashCommandIndex.put(commandPath, new SlashCommandInteraction((ISlashCommand) instance, sub, command.getCommandPrivileges()));
        DIH4JDALogger.info(String.format("\t[*] Registered command: /%s", commandPath), DIH4JDALogger.Type.SLASH_COMMAND_REGISTERED);
        subDataList.add(instance.getSubcommandData());
    }
    return subDataList;
}
Also used : ISlashCommand(com.dynxsty.dih4jda.commands.interactions.slash_command.ISlashCommand) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) SlashCommandInteraction(com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction)

Example 4 with SubcommandData

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

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

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