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