Event Handlers
When something happens on Discord, or in your bot's lifecycle, it will fire an event. You can define event handlers in your extensions to react to these events.
event<MessageCreateEvent> {
action {
if (event.message.content == "hello") {
event.message.respond("Hello!")
}
}
}
Extension API
Builders
Use this builder to register an event handler.
The event type you'll be handling. You can use a less specific generic if you want to handle multiple event types.
If you need to use a custom event handler subtype, you can pass the constructor here.
Note: You shouldn't need to use this in most situations. This is for advanced use-cases that the existing event handler type isn't suitable for.
Usage
To create an event handler, use the event {} builder detailed above.
All event handlers must define an action.
Builders
Register your event handler's action block, defining what it will do when your bot fires a matching event. See the context API section for more information on what you can do here.
Register a check, which must pass for the event handler's action block to execute.
Functions
You can use these utility functions in your action {} block.
Retrieve this event's locale, calculated by your bot's locale resolvers.
This function's output is cached.
Quickly translate the given translation key using the event's resolved locale.
Translation key object to translate.
Optionally, an array or map of placeholders to replace in the translation string.
Context API
The action { } builder is a receiver against the EventContext type.
This type provides a set of APIs, along with those provided by the base TranslatableContext type (TODO),
which allow your bot to respond to events.
The event type you're handling, provided by your event handler.
Map representing a data cache shared with the event handler's registered checks.
Object representing the event you're currently handling.
The event handler that was triggered by the event.
A Sentry context (TODO) you can use to keep track of what happens in your event handler.
Extra Data
All Kord Extensions events extend the Kord Event type,
which provides a customContext property.
Your bot will store a MutableMap<String, Any> here for all events it processes, which allows you to store extra data
on event objects as part of processing.
Kord Extensions provides some extra APIs that make using this more convenient.
Types
The sections below use the following generic types for brevity.
Type representing all string-keyed maps.
Type representing only mutable string-keyed maps without nullable values.
Properties
Convenient access to the extra data map stored in customContext.
Functions
Attempt to retrieve a value from the map stored with the specified key, casting it to the given type T and
returning it.
This function will throw exceptions:
ClassCastException- Thrown when the value stored under the givenkeycan't be cast toT.IllegalArgumentException- Thrown when the map doesn't contain the givenkey.
Type to cast the value to.
Key to attempt to retrieve a value for.
Attempt to retrieve a value from the map stored with the specified key, casting it to the given type T and
returning it.
If the map doesn't contain the given key or the value can't be cast to T, this function will return the
specified default value.
Type to cast the value to.
Key to attempt to retrieve a value for.
Default value to return if the key is missing, or the value is the wrong type.
Attempt to retrieve a value from the map stored with the specified key, casting it to the given type T and
returning it.
If the map doesn't contain the given key or the value can't be cast to T, this function will return the
specified default value, optionally storing it under the given key if store is true.
Note: This function variant only supports mutable string-keyed maps with non-nullable values.
If your map may contain null values, you can only use the getOfOrDefault function specified above.
Type to cast the value to.
Key to attempt to retrieve a value for.
Default value to return if the key is missing, or the value is the wrong type.
Set this to true to store the default value in the map under the given key, when the default value would be
returned.
Attempt to retrieve a value from the map stored with the specified key, casting it to the given type T and
returning it.
If the map doesn't contain the given key or the value can't be cast to T, this function will return null.
Type to cast the value to.
Key to attempt to retrieve a value for.