Skip to content

πŸ› οΈ Command Arguments ​

Command arguments allow you to pass additional information to your commands when they are invoked. This is useful for creating dynamic commands that can perform different actions based on user input.

πŸ“œ @SlashCommandArgument Annotation ​

The @SlashCommandArgument annotation is used to define an argument for a slash command. You can specify the name, description, type, and whether the argument is required or optional. You can add it to the method that handles the command, and it will automatically parse the argument from the command invocation. SlashCommandArgument's can also be stacked, so you can have multiple arguments for a single command.

Optionally you can add a Map<String, Object> to the method signature to get all arguments as a Map. If you prefer to retrieve the arguments from the event, you can use #event.getOptions().

OptionDescription
nameThe name of the argument. This is what users will type to provide the value.
descriptionA short description of what the argument does.
typeThe type of the argument. This can be a string, integer, etc.
requiredWhether the argument is required or optional.
autoCompleteWhether the argument should be auto-completed.

Here’s an example of a command with an argument using the @SlashCommandArgument annotation:

java
public class MyCommand implements SlashCommandHandler {
    @SlashCommand(
        name = "greet",
        description = "Greet a user"
    )
    @SlashCommandArgument(
        name = "user",
        description = "The user to greet",
        type = OptionType.USER,
        required = true
    )
    public void onGreetCommand(SlashCommandInteractionEvent event, Map<String, Object> args) {
        // Get the user argument
        User user = (User) args.get("user");
        
        // Reply to the command
        event.reply("Hello, " + user.getName() + "!").queue();
    }
}