Checks
Checks provide a way to define a reusable, generic set of predicates that may be applied to event handlers, commands, components, and other APIs.
In its simplest form, a check is an extension or receiver function against the CheckContext
or CheckContextWithCache
types, making use of the predefined check functions or APIs provided.
The default state for all checks is passed, with the passed
property set to true
. A check will not fail unless passed
is set to false
, but this property is automatically set for you when using the provided API functions.
Check Context API
When writing your own checks, the following APIs are available.
Functions
Name | Description |
---|---|
| Mark this check as having failed. This function optionally takes a message Key object explaining the failure, which will be displayed to the user in most situations. |
| Mark this check as having failed if the callable block returns This function optionally takes a message Key object explaining the failure, which will be displayed to the user in most situations. |
| Mark this check as having failed if the callable block returns This function optionally takes a message Key object explaining the failure, which will be displayed to the user in most situations. |
| Mark this check as having passed. |
| Mark this check as having passed if the callable block returns |
| Mark this check as having passed if the callable block returns |
| If the receiver is |
| If the receiver is |
Properties
Name | Type | Description | |
---|---|---|---|
|
| For You may store data in this cache to be shared between your checks or contexts as required. In some cases, this may help you to avoid making long-running calculations or API calls multiple times, which will speed up your bot. | |
|
| Translation Key object to use when generating an error message for display on Discord, intended only for formatting. Defaults to | |
|
| Event object that triggered this check. | |
|
| Relevant locale object for this check. Provided by whatever system the check belongs to and used to translate messages. | |
|
| Error message to return to the user, typically set by the | |
|
| Whether this check is currently passing. Set by the |
Bundled Checks
Kord Extensions provides a large number of predefined check functions. These checks are generic and work with all event types, with some caveats that are documented below.
Before writing your own checks, it's best to make sure that there are no existing checks that cover your use-case!
Channel Checks
All functions in this section take either a Snowflake
argument representing a channel ID, or a suspending callable block that returns a ChannelBehavior
.
Name | Description |
---|---|
| Check whether the channel the event fired in is above the given channel in the channel list. This check attempts to calculate the channel positions in the same way Discord does:
Always fails when the event doesn't concern a channel, or concerns a DM channel. |
| Check whether the channel the event fired in is below the given channel in the channel list. This check attempts to calculate the channel positions in the same way Discord does:
Always fails when the event doesn't concern a channel, or concerns a DM channel. |
| Check whether the event fired within the given category. Always fails when the event doesn't concern a channel, or concerns a DM channel. |
| Check whether the event fired within the given channel. Always fails when the event doesn't concern a channel. |
| Check whether the event didn't fire within the given category. Always passes when the event doesn't concern a channel, or concerns a DM channel. |
| Check whether the event didn't fire within the given channel. Always passes when the event doesn't concern a channel. |
Channel Type Checks
All functions in this section take a variadic argument, allowing you to specify more than one channel type.
Name | Description |
---|---|
| Check whether the channel the event fired in is one of the given channel types. Always fails when the event doesn't concern a channel. |
| Check whether the channel the event fired in is not one of the given channel types. Always passes when the event doesn't concern a channel. |
Guild Checks
All functions that take arguments in this section take either a Snowflake
argument representing a guild ID, or a suspending callable block that returns a GuildBehavior
.
Name | Description |
---|---|
| Check whether the event fired within the given guild. Always fails when the event doesn't concern a guild. |
| Check whether the event didn't fire within the given guild. Always passes when the event doesn't concern a guild. |
| Check whether the event fired within a guild. Always fails when the event doesn't concern a guild, or when the corresponding If you're not seeing the results you expected, please double-check your cache configuration. |
| Check whether the event didn't fire within a guild. Always passes when the event doesn't concern a guild, or when the corresponding If you're not seeing the results you expected, please double-check your cache configuration. |
Member Checks
Name | Description |
---|---|
| Check whether the guild member an event fired for has the given permission, or the Administrator permission. Always fails when the event doesn't concern a guild member, or when the corresponding If you're not seeing the results you expected, please double-check your cache configuration and intents configuration. |
| Check whether the guild member an event fired for has the given permissions set, or the Administrator permission. Always fails when the event doesn't concern a guild member, or when the corresponding If you're not seeing the results you expected, please double-check your cache configuration and intents configuration. |
| Check whether the guild member an event fired for doesn't have the given permission, and doesn't have the Administrator permission. Always passes when the event doesn't concern a guild member, or when the corresponding If you're not seeing the results you expected, please double-check your cache configuration and intents configuration. |
| Check whether the guild member an event fired for doesn't have the given permissions set, and doesn't have the Administrator permission. Always passes when the event doesn't concern a guild member, or when the corresponding If you're not seeing the results you expected, please double-check your cache configuration and intents configuration. |
Miscellaneous Checks
Name | Description |
---|---|
| Check whether the user an event fired for is a bot. Always fails when the event doesn't concern a user. |
| Check whether the user an event fired for is one of the current bot's application admins. The team the application belongs to on the Discord developer dashboard defines the application's admins. Always fails when the event doesn't concern a user, or the bot's application doesn't belong to a team. |
| Check whether the user an event fired for is the current bot's application owner. For applications that don't belong to a team, this is the person that owns the bot's application on the Discord developer dashboard. Always fails when the event doesn't concern a user, or the bot's application belongs to a team. |
| Check whether the event fired within a thread. Always fails when the event doesn't concern a channel. |
| Check whether the user an event fired for is not a bot. Always passes when the event doesn't concern a user. |
| Check whether the user an event fired for is not one of the current bot's application admins. The team the application belongs to on the Discord developer dashboard defines the application's admins. Always passes when the event doesn't concern a user, or the bot's application doesn't belong to a team. |
| Check whether the user an event fired for is not the current bot's application owner. For applications that don't belong to a team, this is the person that owns the bot's application on the Discord developer dashboard. Always passes when the event doesn't concern a user, or the bot's application belongs to a team. |
| Check whether the event didn't fire within a thread. Always passes when the event doesn't concern a channel. |
NSFW Checks
Name | Description |
---|---|
| Check whether the event fired in an age-restricted channel. Always fails when the event doesn't concern a channel. |
| Check whether the event fired in an age-restricted channel, or an NSFW-marked guild. Always fails when the event doesn't concern a channel or guild. This check combines the |
| Check whether the event fired in a guild that has a more severe NSFW level than the given one. See the note at the top of this section for information on how Kord Extensions compares NSFW levels. Always fails when the event doesn't concern a guild. |
| Check whether the event fired in a guild that has an equal or more severe NSFW level than the given one. See the note at the top of this section for information on how Kord Extensions compares NSFW levels. Always fails when the event doesn't concern a guild. |
| Check whether the event fired in a guild that has a less severe NSFW level than the given one. See the note at the top of this section for information on how Kord Extensions compares NSFW levels. Always fails when the event doesn't concern a guild. |
| Check whether the event fired in a guild that has an equal or less severe NSFW level than the given one. See the note at the top of this section for information on how Kord Extensions compares NSFW levels. Always fails when the event doesn't concern a guild. |
| Check whether the event fired in a guild with the given NSFW level. Always fails when the event doesn't concern a guild. |
| Check whether the event didn't fire in an age-restricted channel. Always passes when the event doesn't concern a channel. |
| Check whether the event didn't fire in an age-restricted channel or an NSFW-marked guild. This check combines the |
| Check whether the event didn't fire in a guild with the given NSFW level. Always passes when the event doesn't concern a guild. |
Role Checks
All functions in this section take either a Snowflake
argument representing a role ID, or a suspending callable block that returns a RoleBehavior
.
Name | Description |
---|---|
| Check whether the guild member an event fired for has the given role. Always fails when the event doesn't concern a guild member, when the corresponding If you're not seeing the results you expected, please double-check your cache configuration. |
| Check whether the guild member an event fired for doesn't have the given role. Always passes when the event doesn't concern a guild member, when the corresponding If you're not seeing the results you expected, please double-check your cache configuration. |
| Check whether the guild member an event fired for has the given role as their top role. Always fails when the event doesn't concern a guild member, when the guild member doesn't have any roles, when the corresponding |
| Check whether the guild member an event fired for has a top role listed above the given role. Always fails when the event doesn't concern a guild member, when the guild member doesn't have any roles, when the corresponding |
| Check whether the guild member an event fired for has a top role equal to or listed above the given role. Always fails when the event doesn't concern a guild member, when the guild member doesn't have any roles, when the corresponding |
| Check whether the guild member an event fired for has a top role listed below the given role. Always fails when the event doesn't concern a guild member, when the guild member doesn't have any roles, when the corresponding |
| Check whether the guild member an event fired for has a top role equal to or listed below the given role. Always fails when the event doesn't concern a guild member, when the guild member doesn't have any roles, when the corresponding |
| Check whether the guild member an event fired for doesn't have the given role as their top role. Always passes when the event doesn't concern a guild member, when the guild member doesn't have any roles, when the corresponding |
Top Channel Checks
All functions in this section take either a Snowflake
argument representing a role ID, or a suspending callable block that returns a ChannelBehavior
.
Name | Description |
---|---|
| Check whether the event fired within the given top channel. Resolves the parent channel when the event fired within a thread, and uses that for the comparison. Always fails when the event doesn't concern a channel. |
| Check whether the event didn't fire within the given top channel. Resolves the parent channel when the event fired within a thread, and uses that for the comparison. Always passes when the event doesn't concern a channel. |
Utilities
Kord Extensions provides several utilities to help you when working with checks.
General Functions
These functions are extension functions against the CheckContext
type.
Name | Description |
---|---|
| Convenience function intended for combining checks. This allows you to supply multiple checks, where only one of them needs to pass.
public suspend fun CheckContext<*>.channelOrGuildIsNsfw() {
channelIsNsfw()
or { guildNsfwLevelHigherOrEqual(NsfwLevel.AgeRestricted) }
}
Combines failure messages when there's already a check failure message and the check within the If any previous checks have passed, doesn't run the code within the |
| If a check has set a failure message, this function will clear it. This prevents an error message from being sent if the check fails. |
Entity Extraction Functions
These functions exist to provide easy ways to extract entities from generic events. While these functions are particularly useful for implementing checks, they may also be useful in other scenarios.
Name | Description |
---|---|
| Retrieve the corresponding |
| Retrieve the corresponding |
| Retrieve the corresponding |
| Retrieve the corresponding |
| Retrieve the corresponding |
| Retrieve the corresponding |
| Retrieve the corresponding |
| Retrieve the corresponding Retrieves the parent channel when the event concerns a thread. |
| Retrieve the corresponding |
Logging Functions
These functions are extension functions against the KLogger
type, part of the Kotlin Logging library. Designed for specific situations, these functions log predefined messages for you.
Name | Description |
---|---|
| Log a failing check, along with a message that explains why. |
| Log that this check failed because the given ID doesn't correspond with a known category. |
| Log that this check failed because the given ID doesn't correspond with a known channel. |
| Log that this check failed because the given ID doesn't correspond with a known guild. |
| Log that this check failed because the given ID doesn't correspond with a known role. |
| Log that the event's channel is null, and that this check may not support the event. |
| Log that the event's guild is null, and that this check may not support the event. |
| Log that the event's member is null, and that this check may not support the event. |
| Log that the event's message is null, and that this check may not support the event. |
| Log a passing check, optionally along with a message. |
Custom Check Functions
Sometimes, it can be useful to define your own check functions. This allows you to create custom, reusable checks for your own use-cases.
All check functions are extension functions against the CheckContext
type, and they may or may not take arguments as required. There's no mandated structure for these functions, but you may wish to use a similar structure to the bundled checks:
If you need more examples, feel free to take a look at the bundled checks.