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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy