Validators¶
Command Options of a Slash Command can have constraints. You can add constraints by annotating the method parameter with the respective annotation.
Default Validators¶
JDA-Commands comes with the following default constraints:
@Min
: The number must be less or equal to the specified maximum.@Max
: The number must be greater or equal to the specified minimum.@Perm
: The user or member that have the specified discord permission.@NotPerm
: The user or member that doesn't have the specified discord permission.
Example
An error message is sent, if a parameter constraint fails:
You can customize this error message, find more about it here.
Writing own Validators¶
1. Creating the Annotation¶
First, you need to create an annotation type for your validator. Your annotation must meet the following conditions:
-
@Target
must beElementType.PARAMETER
-
RetentionPolicy
must beRUNTIME
- Must be annotated with
@Constraint
defining the valid types for this annotation. - Must contain a
message()
field for the error message
Example
2. Creating the Validator¶
Secondly, you must create the actual validator by implementing the Validator
interface.
The apply(...)
method will give you the argument (command option) as well as the annotation object untyped, you must cast
them on your own. If the constraint shall pass, you must return true
, if it fails false
.
Example
3. Registration¶
Lastly, you have to register your new validator.