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:
@Role
: The member must have the specified guild role.@NotRole
: The member must not have the specified guild role.@User
: Must be the specified user or member@NotUser
: Must not be the specified user or member.@Perm
: The user or member that have the specified discord permission.@NotPerm
: The user or member that doesn't have the specified discord permission.
The user and role annotations will resolve the snowflake entity dynamically using the respective type adapter. This means that you can either pass an ID or a name.
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.