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

io.github.freya022.botcommands.internal.components.builder.EphemeralTimeoutableComponentImpl.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.builder

import io.github.freya022.botcommands.api.components.Components
import io.github.freya022.botcommands.api.components.builder.IEphemeralTimeoutableComponent
import io.github.freya022.botcommands.internal.components.data.EphemeralTimeout
import io.github.freya022.botcommands.internal.utils.throwUser
import io.github.freya022.botcommands.internal.utils.toTimestampIfFinite
import kotlin.time.Duration

internal class EphemeralTimeoutableComponentImpl> internal constructor(
    override val instanceRetriever: InstanceRetriever
) : BuilderInstanceHolderImpl(),
    IEphemeralTimeoutableComponent {
    override var timeout: EphemeralTimeout? = Components.defaultTimeout.toTimestampIfFinite()?.let { EphemeralTimeout(it, null) }
        private set

    override fun noTimeout(): T = instance.also {
        timeout = null
    }

    override fun timeout(timeout: Duration): T = instance.also {
        val expirationTimestamp = timeout.toTimestampIfFinite() ?: throwUser("Timeout must be finite and positive")
        this.timeout = EphemeralTimeout(expirationTimestamp, null)
    }

    @JvmSynthetic
    override fun timeout(timeout: Duration, handler: suspend () -> Unit): T = instance.also {
        val expirationTimestamp = timeout.toTimestampIfFinite() ?: throwUser("Timeout must be finite and positive")
        this.timeout = EphemeralTimeout(expirationTimestamp, handler)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy