use of net.dv8tion.jda.api.interactions.commands.build.OptionData in project HangmanDiscordBot by megoRU.
the class BotStartConfig method updateSlashCommands.
private void updateSlashCommands(boolean isUpdateInGuilds) {
try {
if (isUpdateInGuilds) {
for (int i = 0; i < jda.getGuilds().size(); i++) {
jda.getGuilds().get(i).updateCommands().queue();
}
} else {
CommandListUpdateAction commands = jda.updateCommands();
List<OptionData> options = new ArrayList<>();
options.add(new OptionData(STRING, "game", "Setting the Game language").addChoice("eng", "eng").addChoice("rus", "rus").setRequired(true));
options.add(new OptionData(STRING, "bot", "Setting the bot language").addChoice("eng", "eng").addChoice("rus", "rus").setRequired(true));
commands.addCommands(Commands.slash("language", "Setting language").addOptions(options));
commands.addCommands(Commands.slash("hg", "Start the game"));
commands.addCommands(Commands.slash("stop", "Stop the game"));
commands.addCommands(Commands.slash("help", "Bot commands"));
commands.addCommands(Commands.slash("stats", "Get your statistics"));
commands.addCommands(Commands.slash("mystats", "Find out the number of your wins and losses"));
commands.addCommands(Commands.slash("allstats", "Find out the statistics of all the bot's games"));
commands.addCommands(Commands.slash("delete", "Deleting your data"));
commands.queue();
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of net.dv8tion.jda.api.interactions.commands.build.OptionData in project JDA by DV8FromTheWorld.
the class CommandDataTest method testChoices.
@Test
public void testChoices() {
OptionData option = new OptionData(OptionType.INTEGER, "choice", "Option with choices!");
Assertions.assertThrows(IllegalArgumentException.class, () -> option.addChoice("invalid name", "Valid description"));
Assertions.assertThrows(IllegalArgumentException.class, () -> option.addChoice("invalidName", "Valid description"));
Assertions.assertThrows(IllegalArgumentException.class, () -> option.addChoice("valid_name", ""));
List<Command.Choice> choices = new ArrayList<>();
for (int i = 0; i < 25; i++) {
option.addChoice("choice_" + i, i);
choices.add(new Command.Choice("choice_" + i, i));
}
Assertions.assertThrows(IllegalArgumentException.class, () -> option.addChoice("name", 100));
Assertions.assertEquals(25, option.getChoices().size());
Assertions.assertEquals(choices, option.getChoices());
}
use of net.dv8tion.jda.api.interactions.commands.build.OptionData in project JDA by DV8FromTheWorld.
the class CommandDataImpl method addOptions.
@Nonnull
@Override
public CommandDataImpl addOptions(@Nonnull OptionData... options) {
Checks.noneNull(options, "Option");
if (options.length == 0)
return this;
checkType(Command.Type.SLASH, "add options");
Checks.check(options.length + this.options.length() <= 25, "Cannot have more than 25 options for a command!");
Checks.check(allowOption, "You cannot mix options with subcommands/groups.");
boolean allowRequired = this.allowRequired;
for (OptionData option : options) {
Checks.check(option.getType() != OptionType.SUB_COMMAND, "Cannot add a subcommand with addOptions(...). Use addSubcommands(...) instead!");
Checks.check(option.getType() != OptionType.SUB_COMMAND_GROUP, "Cannot add a subcommand group with addOptions(...). Use addSubcommandGroups(...) instead!");
Checks.check(allowRequired || !option.isRequired(), "Cannot add required options after non-required options!");
// prevent adding required options after non-required options
allowRequired = option.isRequired();
}
Checks.checkUnique(Stream.concat(getOptions().stream(), Arrays.stream(options)).map(OptionData::getName), "Cannot have multiple options with the same name. Name: \"%s\" appeared %d times!", (count, value) -> new Object[] { value, count });
allowSubcommands = allowGroups = false;
this.allowRequired = allowRequired;
for (OptionData option : options) this.options.add(option);
return this;
}
use of net.dv8tion.jda.api.interactions.commands.build.OptionData in project JDACommand by Badbird-5907.
the class JDACommand method registerCommand.
private void registerCommand(Command command, String name, Method method, Object o) {
try {
out.println("Registering command: " + name);
if (command.disable() || method.isAnnotationPresent(Disable.class))
return;
if (!COMMAND_REGEX.matcher(name.toLowerCase()).matches()) {
throw new IllegalArgumentException("Command name must match regex: " + COMMAND_REGEX.pattern() + " see https://discord.com/developers/docs/interactions/application-commands for more info");
}
if (commandMap.containsKey(name) || name.isEmpty()) {
return;
}
List<CommandCreateAction> actions = new ArrayList<>();
for (Guild guild : jda.getGuilds()) {
boolean upsert = returnCallBack == null || returnCallBack.shouldUpsertCommand(guild);
if (upsert) {
actions.add(guild.upsertCommand(name.toLowerCase(), command.description()));
}
}
Parameter[] params = method.getParameters();
List<Pair<ParameterContext, Provider<?>>> parameters = new ArrayList<>();
for (int i = 0; i < params.length; i++) {
Parameter param = params[i];
Provider<?> provider = getProvider(param);
if (provider == null) {
throw new IllegalArgumentException("Could not find a Parameter Provider for " + param.getType().getName() + " in " + method.getName());
}
ParameterContext pCtx = new ParameterContext(params, i, param, param.getDeclaredAnnotations());
if (provider.getOptionData(pCtx) != null) {
for (CommandCreateAction action : actions) {
OptionData option = provider.getOptionData(pCtx);
if (!option.isRequired() && pCtx.isRequired())
option.setRequired(true);
action.addOptions(option);
}
}
parameters.add(new Pair<>(pCtx, provider));
}
CommandWrapper wrapper = new CommandWrapper(command, name.toLowerCase(), method, o, params, parameters);
for (CommandCreateAction action : actions) {
action.queue((c) -> {
System.out.println("Registered command " + c);
commandMap.put(c.getName(), wrapper);
});
}
System.out.println("Done Registering command " + name);
} catch (Exception e) {
e.printStackTrace();
}
}
use of net.dv8tion.jda.api.interactions.commands.build.OptionData in project JDA by DV8FromTheWorld.
the class SlashBotExample method main.
public static void main(String[] args) throws LoginException {
JDA jda = // slash commands don't need any intents
JDABuilder.createLight("BOT_TOKEN_HERE", EnumSet.noneOf(GatewayIntent.class)).addEventListeners(new SlashBotExample()).build();
// These commands take up to an hour to be activated after creation/update/delete
CommandListUpdateAction commands = jda.updateCommands();
// Moderation commands with required options
commands.addCommands(Commands.slash("ban", "Ban a user from this server. Requires permission to ban users.").addOptions(// USER type allows to include members of the server or other users by id
new OptionData(USER, "user", "The user to ban").setRequired(// This command requires a parameter
true)).addOptions(// This is optional
new OptionData(INTEGER, "del_days", "Delete messages from the past days.").setRequiredRange(0, // Only allow values between 0 and 7 (inclusive)
7)).addOptions(// optional reason
new OptionData(STRING, "reason", "The ban reason to use (default: Banned by <user>)")));
// Simple reply commands
commands.addCommands(Commands.slash("say", "Makes the bot say what you tell it to").addOption(STRING, "content", "What the bot should say", // you can add required options like this too
true));
// Commands without any inputs
commands.addCommands(Commands.slash("leave", "Make the bot leave the server"));
commands.addCommands(Commands.slash("prune", "Prune messages from this channel").addOption(INTEGER, "amount", // simple optional argument
"How many messages to prune (Default 100)"));
// Send the new set of commands to discord, this will override any existing global commands with the new set provided here
commands.queue();
}
Aggregations