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

io.github.freya022.botcommands.internal.components.ComponentHandler.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.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})"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy