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

main.gateway.handler.GatewayEventHandler.kt Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package dev.kord.voice.gateway.handler

import dev.kord.voice.gateway.VoiceEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import mu.KotlinLogging

private val logger = KotlinLogging.logger("[Handler]")

internal abstract class GatewayEventHandler(
    val flow: Flow,
    val name: String
) {
    open suspend fun start() {}

    suspend inline fun  CoroutineScope.on(crossinline block: suspend (T) -> Unit) {
        flow.filterIsInstance().onEach {
            try {
                block(it)
            } catch (exception: Exception) {
                logger.error(exception) { "[$name]" }
            }
        }.launchIn(this)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy