-
Notifications
You must be signed in to change notification settings - Fork 12
Parameter Options
Sometimes, a parameter doesn't need to be present in order to execute the command. Therefore parameters that aren't mandatory for command execution can be annotated with @Optional
. An optional parameter can be followed by another optional parameter, but not by a non-optional parameter.
It is also possible to pass a default value which will be used instead if the argument isn't present. The default value will be handled as a normal input and thus the ArgumentParser will try to parse it. If the parsing fails the command will still be executed but with empty or possible null values.
@CommandController
public class BanCommand {
@Command("ban")
public void ban(CommandEvent event, Member member, @Optional("not given") String reason) {
member.ban(member, reason).queue();
}
}
Some commands may need a parameter that does not consist of a single char or word. Especially parameters that represent a reason can contain several words or sentences. Since the input gets split at every space, the sentence is cut up in single arguments. To concatenate them again, you can annotate the last parameter of a method with @Concat
. Parameters annotated with Concat will be assigned with a concatenated String of all remaining arguments. Therefore the parameter must be a String and must also be the last parameter defined. Any other occurrence will result in an error.
@CommandController
public class BanCommand {
@Command("ban")
public void ban(CommandEvent event, Member member, @Concat String reason) {
member.ban(member, reason).queue();
}
}
By default this frameworks has a set of constraints you can apply on your parameters:
- Min / Max value
- User / NotUser
- Role / NotRole
- Perm / NotPerm
Just annotate the parameter with it:
@CommandController
public class MuteCommand {
@Command("mute")
public void onCommand(CommandEvent event, @NotUser("ownerId") Member member, @Max(value = 30, message = "Maximal mute duration is 30 days!") int duration) {
}
}
If a constraint fails, an error message gets displayed. If no error message got specified, the bot will simply reply with Parameter validation failed
.
See the JavaDocs for details.
This wiki is the documentation for V3
(text commands)! If you're using V4
(interactions) please refer to the new documentation
Important
This wiki is the documentation for V3
(text commands)! If you're using V4
(interactions) please refer to the new documentation