Buttons

Factory for buttons, see Components for more details.

Examples

Persistent button (Kotlin)

@Command
class SlashSayAgainPersistent : ApplicationCommand() {
@JDASlashCommand(name = "say_again", subcommand = "persistent", description = "Sends a button to send a message again")
suspend fun onSlashSayAgain(
event: GuildSlashEvent,
@SlashOption @Length(max = Button.LABEL_MAX_LENGTH - 6) sentence: String,
buttons: Buttons
) {
// A button that always works, even after a restart
val persistentSaySentenceButton = buttons.secondary("Say '$sentence'").persistent {
// Make sure only the caller can use the button
constraints += event.user

// In Kotlin, you can use callable references,
// which enables you to use persistent callbacks in a type-safe manner
bindTo(::onSaySentenceClick, sentence)
}

event.reply("This button always works")
.addActionRow(persistentSaySentenceButton)
.await()
}

@JDAButtonListener // No need for a name if you use the type-safe bindTo extensions
suspend fun onSaySentenceClick(event: ButtonEvent, sentence: String) {
event.reply_(sentence, ephemeral = true).await()
}
}

Ephemeral button (Kotlin)

@Command
class SlashSayAgainEphemeral : ApplicationCommand() {
@JDASlashCommand(name = "say_again", subcommand = "ephemeral", description = "Sends a button to send a message again")
suspend fun onSlashSayAgain(
event: GuildSlashEvent,
@SlashOption @Length(max = Button.LABEL_MAX_LENGTH - 6) sentence: String,
buttons: Buttons
) {
// A button, which gets invalidated after restart, here it gets deleted after a timeout of 10 seconds
// We have to use lateinit as the button is used in a callback
lateinit var temporarySaySentenceButton: Button
temporarySaySentenceButton = buttons.primary("Say '$sentence'").ephemeral {
// Make sure only the caller can use the button
constraints += event.user

// The code to run when the button gets clicked
bindTo { buttonEvent -> buttonEvent.reply_(sentence, ephemeral = true).await() }

// Disables this button after 10 seconds
timeout(10.seconds) {
val newRow = row(temporarySaySentenceButton.asDisabled())
event.hook.editOriginalComponents(newRow).await() // Coroutines!
}
}

event.reply("This button expires ${TimeFormat.RELATIVE.after(10.seconds)}")
.addActionRow(temporarySaySentenceButton)
.await()
}
}

Persistent button (Java)

@Command
public class SlashSayAgainPersistent extends ApplicationCommand {
private static final String SAY_SENTENCE_HANDLER_NAME = "SlashSayAgainPersistent: saySentenceButton";

@JDASlashCommand(name = "say_again", subcommand = "persistent", description = "Sends a button to send a message again")
public void onSlashSayAgain(
GuildSlashEvent event,
@SlashOption @Length(max = Button.LABEL_MAX_LENGTH - 6) String sentence,
Buttons buttons
) {
// A button that always works, even after a restart
final var persistentSaySentenceButton = buttons.secondary("Say '" + sentence + "'").persistent()
// Make sure only the caller can use the button
.addUsers(event.getUser())
// The method annotated with a JDAButtonListener of the same name will get called,
// with the sentence as the argument
.bindTo(SAY_SENTENCE_HANDLER_NAME, sentence)
.build();

event.reply("This button always works")
.addActionRow(persistentSaySentenceButton)
.queue();
}

@JDAButtonListener(SAY_SENTENCE_HANDLER_NAME)
public void onSaySentenceClick(ButtonEvent event, String sentence) {
event.reply(sentence).setEphemeral(true).queue();
}
}

Ephemeral button (Java)

@Command
public class SlashSayAgainEphemeral extends ApplicationCommand {
@JDASlashCommand(name = "say_again", subcommand = "ephemeral", description = "Sends a button to send a message again")
public void onSlashSayAgain(
GuildSlashEvent event,
@SlashOption @Length(max = Button.LABEL_MAX_LENGTH - 6) String sentence,
Buttons buttons
) {
// A button, which gets invalidated after restart, here it gets deleted after a timeout of 10 seconds
AtomicReference<Button> temporaryButtonRef = new AtomicReference<>();
final var temporarySaySentenceButton = buttons.primary("Say '" + sentence + "'").ephemeral()
// Make sure only the caller can use the button
.addUsers(event.getUser())
// The code to run when the button gets clicked
.bindTo(buttonEvent -> buttonEvent.reply(sentence).setEphemeral(true).queue())
// Disables this button after 10 seconds
.timeout(Duration.ofSeconds(10), () -> {
final var newRow = ActionRow.of(temporaryButtonRef.get().asDisabled());
event.getHook().editOriginalComponents(newRow).queue();
})
.build();
temporaryButtonRef.set(temporarySaySentenceButton); // We have to do this to get the button in our timeout handler

event.reply("This button expires " + TimeFormat.RELATIVE.after(Duration.ofSeconds(10)))
.addActionRow(temporarySaySentenceButton)
.queue();
}
}

See also

Properties

Link copied to clipboard

Functions

Link copied to clipboard

Creates a reference to a rate limiter previously declared by a RateLimitProvider, alongside a discriminator which differentiates this component from others under the same group.

Link copied to clipboard
@CheckReturnValue
fun danger(label: String): ButtonFactory

Creates a danger button factory with the provided label.

@CheckReturnValue
fun danger(emoji: Emoji): ButtonFactory
@CheckReturnValue
fun danger(label: String?, emoji: Emoji?): ButtonFactory

Creates a danger button factory with the provided emoji.

Link copied to clipboard
suspend fun deleteComponents(vararg components: IdentifiableComponent)

Removes the component data stored by the framework of the provided components.

Link copied to clipboard
suspend fun deleteComponentsByIds(vararg ids: Int)

Removes the component data stored by the framework of the provided components.

Link copied to clipboard
@JvmName(name = "deleteComponentsByIds")
fun deleteComponentsByIdsJava(vararg ids: Int)
@JvmName(name = "deleteComponentsByIds")
fun deleteComponentsByIdsJava(ids: Collection<Int>)

Removes the component data stored by the framework of the provided components.

Link copied to clipboard
@JvmName(name = "deleteComponents")
fun deleteComponentsJava(vararg components: IdentifiableComponent)
@JvmName(name = "deleteComponents")
fun deleteComponentsJava(components: Collection<IdentifiableComponent>)

Removes the component data stored by the framework of the provided components.

Link copied to clipboard
suspend fun deleteJdaComponents(components: Collection<ICustomId>)
suspend fun deleteJdaComponents(vararg components: ICustomId)

Removes the component data stored by the framework of the provided components.

Link copied to clipboard
@JvmName(name = "deleteJdaComponents")
fun deleteJdaComponentsJava(components: Collection<ICustomId>)
@JvmName(name = "deleteJdaComponents")
fun deleteJdaComponentsJava(vararg components: ICustomId)

Removes the component data stored by the framework of the provided components.

Link copied to clipboard

Removes the component data stored by the framework of the provided components.

Link copied to clipboard
@JvmName(name = "deleteRows")
fun deleteRowsJava(components: Collection<MessageTopLevelComponent>)

Removes the component data stored by the framework of the provided components.

Link copied to clipboard
suspend fun deleteTree(tree: ComponentTree<*>)
Link copied to clipboard
@JvmName(name = "deleteRows")
suspend fun deleteTreeJava(tree: ComponentTree<*>)
Link copied to clipboard

Gets an existing reference to a ComponentRateLimitReference.

Link copied to clipboard
@CheckReturnValue
fun group(vararg components: IGroupHolder): ComponentGroupFactory

Creates a group of components.

Link copied to clipboard
@CheckReturnValue
fun link(url: String, label: String): Button

Creates a link button factory with the provided label.

@CheckReturnValue
fun link(url: String, emoji: Emoji): Button

Creates a link button factory with the provided emoji.

@CheckReturnValue
fun link(url: String, label: String?, emoji: Emoji?): Button

Creates a link button factory with the provided label and emoji.

Link copied to clipboard
@CheckReturnValue
fun of(content: ButtonContent): ButtonFactory

Creates a button factory with the style, label and emoji provided by the ButtonContent.

@CheckReturnValue
fun of(style: ButtonStyle, label: String): ButtonFactory

Creates a button factory with the provided style and label.

@CheckReturnValue
fun of(style: ButtonStyle, emoji: Emoji): ButtonFactory

Creates a button factory with the provided style and emoji.

@CheckReturnValue
fun of(style: ButtonStyle, label: String?, emoji: Emoji?, disabled: Boolean = false): ButtonFactory

Creates a button factory with the provided style, label and emoji.

Link copied to clipboard
@CheckReturnValue
fun primary(label: String): ButtonFactory

Creates a primary button factory with the provided label.

@CheckReturnValue
fun primary(emoji: Emoji): ButtonFactory
@CheckReturnValue
fun primary(label: String?, emoji: Emoji?): ButtonFactory

Creates a primary button factory with the provided emoji.

Link copied to clipboard
@CheckReturnValue
fun secondary(label: String): ButtonFactory

Creates a secondary button factory with the provided label.

@CheckReturnValue
fun secondary(emoji: Emoji): ButtonFactory
@CheckReturnValue
fun secondary(label: String?, emoji: Emoji?): ButtonFactory

Creates a secondary button factory with the provided emoji.

Link copied to clipboard
@CheckReturnValue
fun success(label: String): ButtonFactory

Creates a success button factory with the provided label.

@CheckReturnValue
fun success(emoji: Emoji): ButtonFactory
@CheckReturnValue
fun success(label: String?, emoji: Emoji?): ButtonFactory

Creates a success button factory with the provided emoji.