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:
@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.
The fail messages of these two default constraints be localized with the localization keys
validator.noperm.fail
or validator.perm.fail
respectively.
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.
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.
If the constraint should fail, you must call context.fail(...)
.
Example
3. Registration¶
Lastly, you have to register your new validator.