Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.freya022.bot.commands.slash

import dev.freya02.botcommands.jda.ktx.components.row
import dev.freya02.botcommands.jda.ktx.messages.deleteDelayed
import io.github.freya022.bot.commands.ban.BanService
import io.github.freya022.bot.resolvers.localize
Expand Down Expand Up @@ -79,7 +80,7 @@ class SlashBan(private val buttons: Buttons, private val banService: BanService)
}

event.replyLocalizedEphemeral(localizationContext, "outputs.confirmationMessage", "userMention" to target.asMention)
.addActionRow(cancelButton, confirmButton)
.addComponents(row(cancelButton, confirmButton))
.queue()

val componentEvent: ButtonEvent = try {
Expand All @@ -91,13 +92,13 @@ class SlashBan(private val buttons: Buttons, private val banService: BanService)
}

when (componentEvent.componentId) {
cancelButton.id -> {
cancelButton.customId -> {
logger.debug { "Ban cancelled for ${target.id}" }
componentEvent.replaceLocalized(localizationContext, "outputs.cancelled").queue()

//Cancel logic
}
confirmButton.id -> {
confirmButton.customId -> {
logger.debug { "Ban confirmed for ${target.id}, $timeframe of messages were deleted, reason: '$reason'" }

componentEvent.replaceLocalized(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import io.github.freya022.botcommands.api.commands.application.slash.annotations.SlashOption;
import io.github.freya022.botcommands.api.components.Buttons;
import io.github.freya022.botcommands.api.components.annotations.RequiresComponents;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.components.actionrow.ActionRow;
import net.dv8tion.jda.api.components.buttons.Button;
import net.dv8tion.jda.api.utils.TimeFormat;

import java.time.Duration;
Expand Down Expand Up @@ -40,7 +40,7 @@ public void onSlashSayAgain(
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)
.addComponents(ActionRow.of(temporarySaySentenceButton))
.queue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import io.github.freya022.botcommands.api.components.annotations.JDAButtonListener;
import io.github.freya022.botcommands.api.components.annotations.RequiresComponents;
import io.github.freya022.botcommands.api.components.event.ButtonEvent;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.components.actionrow.ActionRow;
import net.dv8tion.jda.api.components.buttons.Button;

@Command
@RequiresComponents
Expand All @@ -35,7 +36,7 @@ public void onSlashSayAgain(
.build();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import io.github.freya022.botcommands.api.components.EntitySelectMenu;
import io.github.freya022.botcommands.api.components.SelectMenus;
import io.github.freya022.botcommands.api.components.annotations.RequiresComponents;
import net.dv8tion.jda.api.components.actionrow.ActionRow;
import net.dv8tion.jda.api.components.selections.EntitySelectMenu.SelectTarget;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.selections.EntitySelectMenu.SelectTarget;
import net.dv8tion.jda.api.utils.TimeFormat;

import java.time.Duration;
Expand Down Expand Up @@ -47,7 +47,7 @@ public void onSlashSelectRole(
temporarySelectMenuRef.set(roleMenu);

event.reply("This select menu expires " + TimeFormat.RELATIVE.after(Duration.ofSeconds(10)))
.addActionRow(roleMenu)
.addComponents(ActionRow.of(roleMenu))
.queue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
import io.github.freya022.botcommands.api.components.annotations.JDASelectMenuListener;
import io.github.freya022.botcommands.api.components.annotations.RequiresComponents;
import io.github.freya022.botcommands.api.components.event.EntitySelectEvent;
import net.dv8tion.jda.api.components.actionrow.ActionRow;
import net.dv8tion.jda.api.components.selections.EntitySelectMenu.SelectTarget;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.interactions.components.selections.EntitySelectMenu.SelectTarget;

import java.util.concurrent.ThreadLocalRandom;

Expand All @@ -36,7 +37,7 @@ public void onSlashSelectRole(
.build();

event.reply("This select menu always works")
.addActionRow(roleMenu)
.addComponents(ActionRow.of(roleMenu))
.queue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.github.freya022.botcommands.api.commands.application.slash.annotations
import io.github.freya022.botcommands.api.commands.application.slash.annotations.SlashOption
import io.github.freya022.botcommands.api.components.Buttons
import io.github.freya022.botcommands.api.components.annotations.RequiresComponents
import net.dv8tion.jda.api.interactions.components.buttons.Button
import net.dv8tion.jda.api.components.buttons.Button
import net.dv8tion.jda.api.utils.TimeFormat
import kotlin.time.Duration.Companion.seconds

Expand Down Expand Up @@ -43,7 +43,7 @@ class SlashSayAgainEphemeral : ApplicationCommand() {
}

event.reply("This button expires ${TimeFormat.RELATIVE.after(10.seconds)}")
.addActionRow(temporarySaySentenceButton)
.addComponents(row(temporarySaySentenceButton))
.await()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package doc.kotlin.examples.commands.slash

import dev.freya02.botcommands.jda.ktx.components.row
import dev.freya02.botcommands.jda.ktx.coroutines.await
import dev.freya02.botcommands.jda.ktx.messages.reply_
import io.github.freya022.botcommands.api.commands.annotations.Command
Expand All @@ -15,7 +16,7 @@ import io.github.freya022.botcommands.api.components.annotations.JDAButtonListen
import io.github.freya022.botcommands.api.components.annotations.RequiresComponents
import io.github.freya022.botcommands.api.components.builder.bindWith
import io.github.freya022.botcommands.api.components.event.ButtonEvent
import net.dv8tion.jda.api.interactions.components.buttons.Button
import net.dv8tion.jda.api.components.buttons.Button

@Command
@RequiresComponents
Expand All @@ -38,7 +39,7 @@ class SlashSayAgainPersistent : ApplicationCommand() {
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package doc.kotlin.examples.commands.slash

import dev.freya02.botcommands.jda.ktx.components.row
import dev.freya02.botcommands.jda.ktx.coroutines.await
import dev.freya02.botcommands.jda.ktx.durations.after
import io.github.freya022.botcommands.api.commands.annotations.Command
Expand All @@ -9,9 +10,9 @@ import io.github.freya022.botcommands.api.commands.application.slash.annotations
import io.github.freya022.botcommands.api.components.EntitySelectMenu
import io.github.freya022.botcommands.api.components.SelectMenus
import io.github.freya022.botcommands.api.components.annotations.RequiresComponents
import net.dv8tion.jda.api.components.actionrow.ActionRow
import net.dv8tion.jda.api.components.selections.EntitySelectMenu.SelectTarget
import net.dv8tion.jda.api.entities.Role
import net.dv8tion.jda.api.interactions.components.ActionRow
import net.dv8tion.jda.api.interactions.components.selections.EntitySelectMenu.SelectTarget
import net.dv8tion.jda.api.utils.TimeFormat
import kotlin.random.Random
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -45,7 +46,7 @@ class SlashSelectRoleEphemeral : ApplicationCommand() {
temporarySelectMenu = roleMenu

event.reply("This select menu expires ${TimeFormat.RELATIVE.after(10.seconds)}")
.addActionRow(roleMenu)
.addComponents(row(roleMenu))
.await()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package doc.kotlin.examples.commands.slash

import dev.freya02.botcommands.jda.ktx.components.row
import dev.freya02.botcommands.jda.ktx.coroutines.await
import io.github.freya022.botcommands.api.commands.annotations.Command
import io.github.freya022.botcommands.api.commands.application.ApplicationCommand
Expand All @@ -12,8 +13,8 @@ import io.github.freya022.botcommands.api.components.annotations.JDASelectMenuLi
import io.github.freya022.botcommands.api.components.annotations.RequiresComponents
import io.github.freya022.botcommands.api.components.builder.bindWith
import io.github.freya022.botcommands.api.components.event.EntitySelectEvent
import net.dv8tion.jda.api.components.selections.EntitySelectMenu.SelectTarget
import net.dv8tion.jda.api.entities.Role
import net.dv8tion.jda.api.interactions.components.selections.EntitySelectMenu.SelectTarget
import kotlin.random.Random

@Command
Expand All @@ -33,7 +34,7 @@ class SlashSelectRolePersistent : ApplicationCommand() {
}

event.reply("This select menu always works")
.addActionRow(roleMenu)
.addComponents(row(roleMenu))
.await()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import io.github.freya022.botcommands.api.components.annotations.JDASelectMenuLi
import io.github.freya022.botcommands.api.components.annotations.RequiresComponents
import io.github.freya022.botcommands.api.components.builder.bindWith
import io.github.freya022.botcommands.api.components.event.EntitySelectEvent
import net.dv8tion.jda.api.interactions.components.selections.EntitySelectMenu
import net.dv8tion.jda.api.components.selections.EntitySelectMenu

@Command
@RequiresComponents
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.freya022.botcommands.api.components

import dev.freya02.botcommands.jda.ktx.components.findAll
import dev.freya02.botcommands.jda.ktx.components.toDefaultComponentTree
import io.github.freya022.botcommands.api.commands.ratelimit.declaration.RateLimitProvider
import io.github.freya022.botcommands.api.components.builder.ITimeoutableComponent
import io.github.freya022.botcommands.api.components.builder.IUniqueComponent
Expand All @@ -8,8 +10,9 @@ import io.github.freya022.botcommands.api.components.ratelimit.ComponentRateLimi
import io.github.freya022.botcommands.api.core.BContext
import io.github.freya022.botcommands.internal.components.controller.ComponentController
import kotlinx.coroutines.runBlocking
import net.dv8tion.jda.api.interactions.components.ActionComponent
import net.dv8tion.jda.api.interactions.components.LayoutComponent
import net.dv8tion.jda.api.components.ActionComponent
import net.dv8tion.jda.api.components.MessageTopLevelComponent
import net.dv8tion.jda.api.components.tree.ComponentTree
import javax.annotation.CheckReturnValue

abstract class AbstractComponentFactory internal constructor(
Expand Down Expand Up @@ -103,7 +106,7 @@ abstract class AbstractComponentFactory internal constructor(
@JvmSynthetic
suspend fun deleteJdaComponents(components: Collection<ActionComponent>) =
components
.mapNotNull { it.id }
.mapNotNull { it.customId }
.mapNotNull { IdentifiableComponent.fromIdOrNull(it) }
.let { deleteComponents(it) }

Expand All @@ -116,7 +119,7 @@ abstract class AbstractComponentFactory internal constructor(
* and components from the same group will also be deleted according to the [timeout][ITimeoutableComponent.timeout] documentation.
*/
@JvmName("deleteRows")
fun deleteRowsJava(components: Collection<LayoutComponent>) = runBlocking { deleteRows(components) }
fun deleteRowsJava(components: Collection<MessageTopLevelComponent>) = runBlocking { deleteRows(components) }

/**
* Removes the component data stored by the framework of the provided components.
Expand All @@ -125,9 +128,23 @@ abstract class AbstractComponentFactory internal constructor(
* and components from the same group will also be deleted according to the [timeout][ITimeoutableComponent.timeout] documentation.
*/
@JvmSynthetic
suspend fun deleteRows(components: Collection<LayoutComponent>) =
components.flatMap { it.actionComponents }
.mapNotNull { it.id }
suspend fun deleteRows(components: Collection<MessageTopLevelComponent>) =
deleteTree(components.toDefaultComponentTree())


@JvmSynthetic
@JvmName("deleteRows")
suspend fun deleteTreeJava(tree: ComponentTree<*>) =
tree.findAll<ActionComponent>()
.mapNotNull { it.customId }
.mapNotNull { IdentifiableComponent.fromIdOrNull(it) }
.let { deleteComponents(it) }


@JvmSynthetic
suspend fun deleteTree(tree: ComponentTree<*>) =
tree.findAll<ActionComponent>()
.mapNotNull { it.customId }
.mapNotNull { IdentifiableComponent.fromIdOrNull(it) }
.let { deleteComponents(it) }

Expand Down Expand Up @@ -194,4 +211,4 @@ abstract class AbstractComponentFactory internal constructor(
fun getRateLimitReference(group: String, discriminator: String): ComponentRateLimitReference? {
return componentController.getRateLimitReference(group, discriminator)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.freya022.botcommands.api.components

import io.github.freya022.botcommands.api.components.event.ButtonEvent
import net.dv8tion.jda.api.components.buttons.Button as JDAButton
import net.dv8tion.jda.api.components.buttons.ButtonStyle
import net.dv8tion.jda.api.entities.SkuSnowflake
import net.dv8tion.jda.api.entities.emoji.Emoji
import net.dv8tion.jda.api.interactions.components.buttons.Button as JDAButton
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle

interface Button : JDAButton,
AwaitableComponent<ButtonEvent>,
Expand All @@ -13,12 +14,20 @@ interface Button : JDAButton,

override fun asEnabled(): Button = withDisabled(false)

override fun withUniqueId(uniqueId: Int): Button

override fun withCustomId(customId: String): Button

override fun withDisabled(disabled: Boolean): Button

override fun withEmoji(emoji: Emoji?): Button

override fun withLabel(label: String): Button

override fun withSku(sku: SkuSnowflake): Nothing =
throw UnsupportedOperationException("This type of button cannot contain SKUs")

@Deprecated("Replaced with withCustomId(id)", ReplaceWith("withCustomId(id)"))
override fun withId(id: String): Nothing =
throw UnsupportedOperationException("This type of button cannot contain custom IDs")

Expand All @@ -27,5 +36,8 @@ interface Button : JDAButton,

override fun withStyle(style: ButtonStyle): Button

override fun getId(): String
@Deprecated("Replaced with getCustomId()", ReplaceWith("getCustomId()"))
override fun getId(): String = getCustomId()

override fun getCustomId(): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import io.github.freya022.botcommands.api.components.builder.button.ButtonFactor
import io.github.freya022.botcommands.api.components.utils.ButtonContent
import io.github.freya022.botcommands.api.core.service.annotations.BService
import io.github.freya022.botcommands.internal.components.controller.ComponentController
import net.dv8tion.jda.api.components.buttons.Button
import net.dv8tion.jda.api.components.buttons.ButtonStyle
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

/**
Expand Down Expand Up @@ -393,4 +393,4 @@ class Buttons internal constructor(componentController: ComponentController) : A
@CheckReturnValue
fun link(url: String, label: String?, emoji: Emoji?): Button =
Button.of(ButtonStyle.LINK, url, label, emoji)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.freya022.botcommands.api.components

import io.github.freya022.botcommands.api.components.event.EntitySelectEvent
import net.dv8tion.jda.api.interactions.components.selections.EntitySelectMenu as JDAEntitySelectMenu
import net.dv8tion.jda.api.components.selections.EntitySelectMenu as JDAEntitySelectMenu

interface EntitySelectMenu : JDAEntitySelectMenu,
AwaitableComponent<EntitySelectEvent>,
Expand All @@ -11,7 +11,7 @@ interface EntitySelectMenu : JDAEntitySelectMenu,

override fun asDisabled(): EntitySelectMenu = withDisabled(true)

override fun withDisabled(disabled: Boolean): EntitySelectMenu
override fun withUniqueId(uniqueId: Int): EntitySelectMenu

override fun getId(): String
}
override fun withDisabled(disabled: Boolean): EntitySelectMenu
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.freya022.botcommands.api.components

import io.github.freya022.botcommands.internal.components.controller.ComponentController
import io.github.freya022.botcommands.internal.utils.throwArgument
import net.dv8tion.jda.api.interactions.components.ActionComponent
import net.dv8tion.jda.api.components.ActionComponent

interface IdentifiableComponent {
val internalId: Int
Expand All @@ -16,11 +16,11 @@ interface IdentifiableComponent {

@JvmStatic
fun fromComponent(component: ActionComponent): IdentifiableComponent =
fromId(component.id ?: throwArgument("This component has no ID"))
fromId(component.customId ?: throwArgument("This component has no ID"))

@JvmStatic
fun fromComponentOrNull(component: ActionComponent): IdentifiableComponent? =
fromIdOrNull(component.id ?: throwArgument("This component has no ID"))
fromIdOrNull(component.customId ?: throwArgument("This component has no ID"))

@JvmStatic
fun fromId(id: String): IdentifiableComponent {
Expand Down
Loading