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
Functions
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.
Creates a danger button factory with the label provided.
Creates a danger button factory with the emoji provided.
Removes the component data stored by the framework of the provided components.
Removes the component data stored by the framework of the provided components.
Removes the component data stored by the framework of the provided components.
Removes the component data stored by the framework of the provided components.
Removes the component data stored by the framework of the provided components.
Removes the component data stored by the framework of the provided components.
Removes the component data stored by the framework of the provided components.
Removes the component data stored by the framework of the provided components.
Gets an existing reference to a ComponentRateLimitReference.
Creates a group of components.
Creates a danger button factory with the label provided.
Creates a danger button factory with the emoji provided.
Creates a button factory with the style, label and emoji provided by the ButtonContent.
Creates a button factory with the style and label provided.
Creates a button factory with the style and emoji provided.
Creates a button factory with the style, label and emoji provided.
Creates a primary button factory with the label provided.
Creates a primary button factory with the emoji provided.
Creates a secondary button factory with the label provided.
Creates a secondary button factory with the emoji provided.
Creates a success button factory with the label provided.
Creates a success button factory with the emoji provided.