Custom Checks
You can create your own custom checks in one of several ways, using the context API and check utilities.
Writing Checks
Inline
If you don't need to re-use a check, you can define it inline in your check {} or validate {} builder.
check {}builders use aCheckContextreceiver.validate {}builders use aValidationContextreceiver, which extendsCheckContextwith some extra APIs.
check {
val channel = guildFor(event)
if (channel == null) {
fail(CoreTranslations.Checks.AnyGuild.failed)
}
}
Re-usable
If you need a re-usable check, define an extension function against the CheckContext type.
Checks written this way should be more in-depth and include logging.
suspend fun CheckContext<*>.anyGuild() {
if (!passed) {
return // If the check context is already failing, we can skip this check.
}
// Create a logger with the containing package and function name.
val logger = KotlinLogging.logger("dev.kordex.core.checks.anyGuild")
if (guildFor(event) != null) {
logger.passed() // Logging a pass is as important as logging a failure!
pass()
} else {
logger.failed("Event did not happen within a guild.")
fail(CoreTranslations.Checks.AnyGuild.failed)
}
}
Context API
All check {} builders use CheckContext as their receiver type.
Check context type, containing everything you need to write your own checks.
Generic type representing the event being checked.
Event object to be checked.
Locale for the current event, retrieved via your bot's locale resolvers.
Builders
Fail this check if the provided lambda returns false.
Calls fail() internally.
Returns true if the check is now failing, false otherwise.
Message to respond with explaining why the check failed.
Fail this check if the provided lambda returns true.
Calls fail() internally.
Returns true if the check is now failing.
Message to respond with explaining why the check failed.
Passes this check if the provided lambda returns true.
Calls pass() internally.
Returns true if the check is now passing.
Passes this check if the provided lambda returns false.
Calls pass() internally.
Returns true if the check is now passing.
Calls the provided lambda only if this boolean is false, returning whatever the lambda does.
If this boolean is true, simply returns null.
Calls the provided lambda only if this boolean is true, returning whatever the lambda does.
If this boolean is false, simply returns null.
Functions
Fails this check, setting passed to true.
Message to respond with explaining why the check failed.
Fails this check if the provided boolean is true.
Calls fail() internally.
Returns true if the check is now failing.
Boolean value to check.
Message to respond with explaining why the check failed.
Fails this check if the provided boolean is false.
Calls fail() internally.
Returns true if the check is now failing.
Boolean value to check.
Message to respond with explaining why the check failed.
Passes this check, setting passed to true.
Passes this check if the provided boolean is true.
Calls pass() internally.
Returns true if the check is now passing.
Boolean value to check.
Passes this check if the provided boolean is false.
Calls pass() internally.
Returns true if the check is now passing.
Boolean value to check.
Show/Hide Internal APIs
Get a pre-translation Key representing the current failure message, provided the check is failing and a message
was set.
Otherwise, this function returns null.
Returns a translated string based on the value returned by getMessageKey(), returning null in the same
situations.
Throws a DiscordRelayedException with the value returned by getMessageKey(),
if that value isn't null.
Properties
Translation key used to construct an error message to display on Discord.
You can replace this with your own key if you need to.
Custom keys must support one ordinal placeholder, {0}, containing the error message.
Error message to respond with.
Usually set via the various fail builders and functions, and not used if passed is true.
Whether this check is currently passing, which is also the default state.
Usually set via the various pass and fail builders and functions.
Validation Contexts
All validate {} builders use ValidationContext as their receiver type.
This type provides the same CheckContext API surface, with some extras.
Validation context type, containing everything you need to write your own validators.
Generic type representing the type of the command argument's value.
Converter used to generate the current argument's value.
The current argument's value.
The command context representing the command invocation triggering this validator.
Locale for the current event, retrieved via your bot's locale resolvers.
Show/Hide Internal APIs
Get a pre-translation Key representing the current failure message, provided the validator is failing.
If message is null, this returns a default error message.
Returns null if the validator is passing.
If this validator is failing, throws a DiscordRelayedException with the value
returned by getMessageKey(),.
If the value returned by getMessageKey() is null, this function throws an IllegalStateException instead.