Search in sources :

Example 1 with SubcommandGroupData

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

the class ISubcommandGroupData method convert.

@Nonnull
public SubcommandGroupData convert() {
    SubcommandGroupData data = new SubcommandGroupData(name, " ");
    subcommands.forEach(subcommand -> data.addSubcommands(subcommand.convert()));
    return data;
}
Also used : SubcommandGroupData(net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData) Nonnull(javax.annotation.Nonnull)

Example 2 with SubcommandGroupData

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

the class InteractionHandler method getSubcommandGroupData.

/**
 * Gets all {@link SubcommandGroupData} (including Subcommands) of a single {@link BaseSlashCommand}.
 *
 * @param command The base command's instance.
 * @param guild   The current guild (if available)
 * @return All {@link SubcommandGroupData} stored in a List.
 * @throws Exception If an error occurs.
 */
private Set<SubcommandGroupData> getSubcommandGroupData(@NotNull BaseSlashCommand command, @Nullable Guild guild) throws Exception {
    Set<SubcommandGroupData> groupDataList = new HashSet<>();
    for (Class<? extends SubcommandGroup> group : command.getSubcommandGroups()) {
        SubcommandGroup instance = (SubcommandGroup) this.getClassInstance(guild, group);
        if (instance.getSubcommandGroupData() == null) {
            DIH4JDALogger.warn(String.format("Class %s is missing SubcommandGroupData. It will be ignored.", group.getName()));
            continue;
        }
        if (instance.getSubcommands() == null) {
            DIH4JDALogger.warn(String.format("SubcommandGroup %s is missing Subcommands. It will be ignored.", instance.getSubcommandGroupData().getName()));
            continue;
        }
        SubcommandGroupData groupData = instance.getSubcommandGroupData();
        groupData.addSubcommands(this.getSubcommandData(command, instance.getSubcommands(), groupData.getName(), guild));
        groupDataList.add(groupData);
    }
    return groupDataList;
}
Also used : SubcommandGroupData(net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData)

Example 3 with SubcommandGroupData

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

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

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

the class CommandDataImpl method addSubcommandGroups.

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

Aggregations

SubcommandGroupData (net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData)6 SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)3 Nonnull (javax.annotation.Nonnull)2 CommandDataImpl (net.dv8tion.jda.internal.interactions.CommandDataImpl)2 Test (org.junit.jupiter.api.Test)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)1 SlashCommandData (net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)1 DataArray (net.dv8tion.jda.api.utils.data.DataArray)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 JSONObject (org.json.JSONObject)1