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;
}
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;
}
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;
}
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"));
}
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"));
}
Aggregations