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
@CheckReturnValue
fun danger(label: String): ButtonFactory

Creates a danger button factory with the label provided.

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

Creates a danger button factory with the emoji provided.

Link copied to clipboard
suspend fun deleteComponents(vararg components: IdentifiableComponent)
Link copied to clipboard
suspend fun deleteComponentsByIds(vararg ids: Int)
Link copied to clipboard
@JvmName(name = "deleteComponentsByIds")
fun deleteComponentsByIdsJava(vararg ids: Int)
@JvmName(name = "deleteComponentsByIds")
fun deleteComponentsByIdsJava(ids: Collection<Int>)
Link copied to clipboard
@JvmName(name = "deleteComponents")
fun deleteComponentsJava(vararg components: IdentifiableComponent)
@JvmName(name = "deleteComponents")
fun deleteComponentsJava(components: Collection<IdentifiableComponent>)
Link copied to clipboard
suspend fun deleteRows(components: Collection<LayoutComponent>)
Link copied to clipboard
@JvmName(name = "deleteRows")
fun deleteRowsJava(components: Collection<LayoutComponent>)
Link copied to clipboard
@CheckReturnValue
fun group(vararg components: IGroupHolder): ComponentGroupFactory
Link copied to clipboard
@CheckReturnValue
fun link(url: String, label: String): Button

Creates a danger button factory with the label provided.

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

Creates a danger button factory with the emoji provided.

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 style and label provided.

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

Creates a button factory with the style and emoji provided.

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

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

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

Creates a primary button factory with the label provided.

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

Creates a primary button factory with the emoji provided.

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

Creates a secondary button factory with the label provided.

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

Creates a secondary button factory with the emoji provided.

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

Creates a success button factory with the label provided.

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

Creates a success button factory with the emoji provided.