io.github.freya022.botcommands.internal.components.ComponentHandler.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of BotCommands Show documentation
Show all versions of BotCommands Show documentation
A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.
package io.github.freya022.botcommands.internal.components
import io.github.freya022.botcommands.api.core.utils.simpleNestedName
import net.dv8tion.jda.api.entities.ISnowflake
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent
sealed interface ComponentHandler {
val lifetimeType: LifetimeType
}
class PersistentHandler(val handlerName: String, userData: List) : ComponentHandler {
override val lifetimeType: LifetimeType = LifetimeType.PERSISTENT
val userData: List = processArgs(userData)
operator fun component1() = handlerName
operator fun component2() = userData
override fun toString(): String {
return "PersistentHandler(handlerName='$handlerName')"
}
private fun processArgs(args: List): List = args.map { arg ->
when (arg) {
is ISnowflake -> arg.id
else -> arg.toString()
}
}
}
class EphemeralHandler(val handler: suspend (T) -> Unit) : ComponentHandler {
override val lifetimeType: LifetimeType = LifetimeType.EPHEMERAL
override fun toString(): String {
return "EphemeralHandler(handler=${handler::class.simpleNestedName})"
}
}