All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.freya022.botcommands.api.components.Buttons.kt Maven / Gradle / Ivy

Go to download

A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.

There is a newer version: 3.0.0-alpha.18
Show newest version
package io.github.freya022.botcommands.api.components

import io.github.freya022.botcommands.api.components.annotations.RequiresComponents
import io.github.freya022.botcommands.api.components.builder.button.ButtonFactory
import io.github.freya022.botcommands.api.components.utils.ButtonContent
import io.github.freya022.botcommands.api.core.service.annotations.BService
import io.github.freya022.botcommands.api.utils.EmojiUtils
import io.github.freya022.botcommands.internal.components.controller.ComponentController
import net.dv8tion.jda.api.entities.emoji.Emoji
import net.dv8tion.jda.api.interactions.components.buttons.Button
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle
import javax.annotation.CheckReturnValue

/**
 * Factory for buttons, see [Components] for more details.
 *
 * ## Examples
 * ### Persistent button (Kotlin)
 * ```kt
 * @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)
 * ```kt
 * @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)
 * ```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)
 * ```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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy