Messages: Easy DSL
Messages support a specific set of components, which you can read about on the basics page.
The components DSL (based on the ComponentContainer class) is a convenient API for working with short-lived message
components.
If you need your components to work after your bot restarts, we recommend using the manual handling approach.
Kord Extensions doesn't provide an atomic API for modifying your components after you've sent them to Discord. This is mostly due to technical limitations within Kotlin's type system, and it is unlikely this will change any time soon.
If you wish to edit components, we suggest keeping a reference to the ComponentContainer returned by the components
builder, and any relevant components that you create.
The components within any given ComponentContainer may be applied to any message creation/editing builder via the
applyComponents extension function.
Usage
To get started, use the components builder in any message creation or editing builder.
This builder will automatically add the defined components to the message, and set up the code necessary for your bot
to respond to them.
respond {
components {
publicButton {
label = "Button"
style = ButtonStyle.Primary
action {
respond {
content = "Clicked button."
}
}
}
}
}
The above example produces a message like the following:
For more information on what components you can add with this DSL, see the builder API section below.
Packing
The components DSL automatically packs its defined components as tightly as possible, while retaining the order you
defined them in.
As an example, let's say you provided a select menu, six buttons, and another select menu. The resulting message would look like this:
You can prevent this for specific components by specifying the row to add your component to — just pass an integer representing the row to the corresponding component builder function. This will add that component to the specified row immediately, and the builder will pack any components without defined rows around it before your bot finishes creating the message.
As an example, let's say you provided a select menu on row 2, six buttons without row numbers, and another select menu on row 3. The resulting message would look like this:
Builder Functions
Entrypoint
Convenience function for creating a ComponentContainer with components, and applying it to the message you're creating/editing.
Optional component interaction timeout. When nobody interacts with the components for the length of the timeout, Kord Extensions will unregister them and they'll stop working.
Buttons
Create a disabled button.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Create an interactable button based on an ephemeral interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Create a link button.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Create an interactable button based on a public interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Select Menu (Channels)
Create an channel select menu based on an ephemeral interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Create an channel select menu based on a public interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Select Menu (Mentionables)
Create a mentionable select menu based on an ephemeral interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Create a mentionable select menu based on a public interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Select Menu (Roles)
Create a role select menu based on an ephemeral interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Create a role select menu based on a public interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Select Menu (Strings)
Create a string select menu based on an ephemeral interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Create a string select menu based on a public interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Select Menu (Users)
Create a user select menu based on an ephemeral interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Create a user select menu based on a public interaction flow.
Optional callable returning the Modal form to present users with when they interact with this component.
Optional action row the component should be placed into, if you don't want it to be packed automatically.
Container API
Class representing a single set of components to apply to any message.
Optional timeout, after which all registered components will be unregistered and won't work anymore. This timeout is reset whenever a user interacts with a registered component.
Whether to start the timeout immediately.
This is usually set to true by the components { } DSL, but you can call timeoutTask.start() yourself
in other situations.
Builders
Register a callback to be run when the timeout expires, if one was provided.
Component containers can only have one registered callback. Calling this function twice will overwrite the previous one.
Functions
Cancel the timeout task and remove all stored components, unregistering them.
This behaviour is similar to letting the timeout expire, but it won't run the extra timeoutCallback provided by
the onTimeout { } builder.
Remove a component from this container, and unregister it.
Component to remove.
Remove all components from this container, and unregister them.
Remove a component from this container, replacing it with a new one and likewise handling un/registration.
The component to remove.
You can provide the component object, or a String representing its ID.
The component to replace with.
Sort any unsorted components into rows, by packing them as tightly as possible.
Show/Hide Internal APIs
Add a component to this container, and register it.
This is usually called by the builder functions.
Component to add.
Optional row to pack the component into.
If you provide a row number, the component will immediately be packed into the corresponding list in rows.
Otherwise, it'll be added to unsortedComponents for the sort() function to handle later.
Properties
Timeout task, responsible for enforcing the provided timeout.
This will be null if no timeout was provided.
Show/Hide Internal APIs
Array representing up to five rows of sorted and packed components.
This array always contains five mutable lists, which are filled by the sort() function.
Extra callback to run when the timeout expires, if one was provided.
Usually set via the onTimeout { } builder.
List containing all components without an explicitly defined row, to be packed into rows before adding the
components to a message.
Component API
Abstract class representing a Discord component.
How many units this component will take up.
Apply this component to a Kord action row.
Throws exceptions if this component isn't valid.
Abstract class representing a Discord component with an ID.
Component's ID. Defaults to a random UUID, but you can overwrite this in your component builders.
Abstract class representing a Discord component which can respond to an interaction.
Generic type representing the event type corresponding with the interactions this component can respond to.
Generic type representing the context object type that will be created when handling interactions for this component.
Generic type representing the modal form type that will be sent in response to interactions for this component.
Timeout task, passed down from the component container.
Will be null if no timeout was provided.
Callback returning a ModalForm object, as explained here.
Call this builder to register an action block, which will be run in response to a component interaction.
Modal form object, if a modal callback was provided when registering this component.
Will be null if you don't provide that callback, or if the user doesn't submit the modal quickly enough.
Register a check that must pass before the action can be run.
Checks to add.
Register a set of permissions the bot will need to respond successfully to an interaction.
Permissions to add.
Whether to use a deferred ack when responding to interactions. This will prevent Discord from showing a "Thinking..." message.
Buttons
Class representing a button component.
Translated button label, for display on Discord. Optional if you provide an emoji.
Class representing a button component that has an ID.
Translated button label, for display on Discord. Optional if you provide an emoji.
Class representing a button component that can respond to interactions.
Whether this button is disabled.
Translated button label, for display on Discord. Optional if you provide an emoji.
Disable this button by setting disabled to true.
Enable this button by setting disabled to false.
Interaction Buttons
Ephemeral and public interaction buttons extend InteractionButtonWithAction, and provide some additional APIs.
This button's style.
Anything other than Link or Premium is valid here.
Use this builder to respond to interactions with a message immediately, instead of using an empty acknowledgement.
Disabled Buttons
Disabled buttons extend InteractionButtonWithID, and provide some additional APIs.
This button's style.
Anything other than Link or Premium is valid here.
Link Buttons
Link buttons extend InteractionButton, and provide some additional APIs.
URL to send the user to when they click/tap this button.
Select Menus
All ephemeral and public select menus extend the SelectMenu type below, along with an interface representing the
type of data they handle.
Class representing a select menu component.
Whether this select menu is disabled.
A true value here will disable the component.
To enable the component, the value must be null instead, not false.
We don't know why this is, unfortunately.
Consider using the disable() and enable() functions instead of setting this directly.
The maximum number of selections a user can choose.
Set to null for no maximum.
The minimum number of selections a user can choose.
Placeholder text to show when the user hasn't made a selection.
Disable this select menu by setting disabled to true.
Enable this select menu by setting disabled to null.
Channel Menus
Add one or more channel types to this select menu. This will limit which channels the user can select.
Channel types to add.
Add one or more pre-selected channels to this select menu. These channels will be selected by default.
Mentionable Menus
Add one or more pre-selected roles to this select menu. These roles will be selected by default.
Add one or more pre-selected users to this select menu. These users will be selected by default.
Role Menus
Add one or more pre-selected roles to this select menu. These roles will be selected by default.
String Menus
Add an option to this select menu.
Translatable option label, shown to users on Discord.
String value, which is what you'll actually receive when users interact with the select menu.
Translatable additional option description, shown to users on Discord.
Whether this option should be selected by default.
User Menus
Add one or more pre-selected users to this select menu. These users will be selected by default.
Component Contexts
All component action { } blocks use a corresponding context object as the receiver, allowing you to retrieve any
relevant data and respond to component interactions.
Class representing a basic component context, with functionality common to all contexts.
Generic type representing the event that triggered this interaction.
Component object corresponding with this interaction.
Object representing the event that triggered this interaction.
Data cache map shared with this command's checks.
The channel this component interaction happened in.
The guild this component interaction happened in, or null if it happened in a DM.
The guild member that caused this component interaction, or null if
this component interaction happened in a DM.
The Discord message this component belongs to.
The Discord user that caused this component interaction.
Select Menus
Select menus use a context type extending the above, with some small additions:
- Channel Menus:
- A
selectedproperty containing the selected channels.
- A
- Mentionable Menus:
- A
selectedproperty containing the IDs of all selected users and roles. - A
selectedRolesproperty containing the selected roles. - A
selectedUsersproperty containing the selected users.
- A
- Role Menus:
- A
selectedproperty containing the selected roles.
- A
- String Menus:
- A
selectedproperty containing the selected option strings.
- A
- String Menus:
- A
selectedproperty containing the selected users.
- A
Utilities
Interface representing components that support emojis.
Set this component's emoji.
Emoji to set.
Convenience function for applying an arbitrary ComponentContainer to the message you're currently creating/editing.
ComponentContainer to apply.
