use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project JDA by DV8FromTheWorld.
the class CommandDataTest method testNormal.
@Test
public void testNormal() {
CommandData command = new CommandDataImpl("ban", "Ban a user from this server").setDefaultEnabled(false).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("ban", data.getString("name"));
Assertions.assertEquals("Ban a user from this server", data.getString("description"));
Assertions.assertFalse(data.getBoolean("default_permission"));
DataArray options = data.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.CommandData in project TJ-Bot by Together-Java.
the class SlashCommandAdapterTest method getData.
@Test
void getData() {
SlashCommandAdapter adapter = createAdapter();
CommandData data = adapter.getData();
assertEquals(NAME, data.getName(), "adapters name is inconsistent with the base data object");
assertEquals(DESCRIPTION, data.getDescription(), "adapters description is inconsistent with the base data object");
// Check that the method does not return a new data instance and does not mess with it
String otherName = NAME + "-bar";
String otherDescription = DESCRIPTION + "-bar";
data.setName(otherName).setDescription(otherDescription);
CommandData otherData = adapter.getData();
assertSame(data, otherData, "adapter changed the data object");
assertEquals(otherName, otherData.getName(), "name changes did not carry over");
assertEquals(otherDescription, otherData.getDescription(), "description changes did not carry over");
}
use of net.dv8tion.jda.api.interactions.commands.build.CommandData 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;
}
}
}
use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project Utility by anweisen.
the class ICommandData method convert.
@Nonnull
public CommandData convert() {
CommandData data = new CommandData(name, " ");
try {
options.forEach(option -> data.addOptions(option));
subcommands.forEach(subcommand -> data.addSubcommands(subcommand.convert()));
subcommandGroups.forEach(group -> data.addSubcommandGroups(group.convert()));
} catch (IllegalArgumentException ex) {
throw new WrappedException("Cannot convert '" + name + "'", ex);
}
return data;
}
use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project DIH4JDA by DynxstyGIT.
the class InteractionHandler method getContextCommandData.
/**
* Gets the complete {@link CommandData} from a single {@link BaseContextCommand}.
*
* @param command The base context command's instance.
* @param commandClass The base context command's class.
* @return The new {@link CommandListUpdateAction}.
*/
private CommandData getContextCommandData(@NotNull BaseContextCommand command, Class<? extends BaseContextCommand> commandClass) {
if (command.getCommandData() == null) {
DIH4JDALogger.warn(String.format("Class %s is missing CommandData. It will be ignored.", commandClass.getName()));
return null;
}
CommandData commandData = command.getCommandData();
if (commandData.getType() == Command.Type.MESSAGE) {
messageContextIndex.put(commandData.getName(), new MessageContextInteraction((IMessageContextCommand) command));
} else if (commandData.getType() == Command.Type.USER) {
userContextIndex.put(commandData.getName(), new UserContextInteraction((IUserContextCommand) command));
} else {
DIH4JDALogger.error(String.format("Invalid Command Type \"%s\" for Context Command! This command will be ignored.", commandData.getType()));
return null;
}
DIH4JDALogger.info(String.format("\t[*] Registered context command: %s", command.getCommandData().getName()), DIH4JDALogger.Type.CONTEXT_COMMAND_REGISTERED);
return commandData;
}
Aggregations