io.github.freya022.botcommands.internal.components.handler.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.handler
import io.github.freya022.botcommands.api.core.utils.simpleNestedName
import io.github.freya022.botcommands.internal.components.LifetimeType
import net.dv8tion.jda.api.entities.ISnowflake
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent
sealed interface ComponentHandler {
val lifetimeType: LifetimeType
}
class PersistentHandler private constructor(val handlerName: String, val userData: List) : ComponentHandler {
override val lifetimeType: LifetimeType = LifetimeType.PERSISTENT
operator fun component1() = handlerName
operator fun component2() = userData
override fun toString(): String {
return "PersistentHandler(handlerName='$handlerName')"
}
internal companion object {
internal fun create(handlerName: String, userData: List): PersistentHandler {
return PersistentHandler(handlerName, processArgs(userData))
}
internal fun fromData(handlerName: String, userData: List): PersistentHandler {
return PersistentHandler(handlerName, userData)
}
private fun processArgs(args: List): List = args.map { arg ->
when (arg) {
null -> null
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})"
}
}