diff --git a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/LavaKord.kt b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/LavaKord.kt index e9b865f2..5bae3655 100644 --- a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/LavaKord.kt +++ b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/LavaKord.kt @@ -38,19 +38,20 @@ public interface LavaKord : CoroutineScope, EventSource { * * @param serverUri the uri to connect to * @param password the lavalink node password - * @param name a optional name for the node + * @param name an optional name for the node */ - public fun addNode(serverUri: String, password: String, name: String? = null): Unit = - addNode(Url(serverUri), password, name) + public fun addNode(serverUri: String, password: String, name: String? = null, session: String? = null): Unit = + addNode(Url(serverUri), password, name, session) /** * Adds a new node to this cluster. * * @param serverUri the uri to connect to * @param password the lavalink node password - * @param name a optional name for the node + * @param name an optional name for the node + * @param session an optional session to resume */ - public fun addNode(serverUri: Url, password: String, name: String? = null) + public fun addNode(serverUri: Url, password: String, name: String? = null, session: String? = null) /** * Creates and returns a new [rest-only node][RestNode]. @@ -62,7 +63,7 @@ public interface LavaKord : CoroutineScope, EventSource { public fun createRestNode(serverUri: Url, password: String, name: String? = null): RestNode /** - * Removes a node from the cluster by it's [name]. + * Removes a node from the cluster by its [name]. */ public fun removeNode(name: String) diff --git a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/AbstractLavakord.kt b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/AbstractLavakord.kt index 3cbdf256..764dc412 100644 --- a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/AbstractLavakord.kt +++ b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/AbstractLavakord.kt @@ -149,7 +149,7 @@ public abstract class AbstractLavakord internal constructor( } } - override fun addNode(serverUri: Url, password: String, name: String?) { + override fun addNode(serverUri: Url, password: String, name: String?, session: String?) { if (name != null) { check(!nodesMap.containsKey(name)) { "Name is already in use" } } @@ -160,7 +160,7 @@ public abstract class AbstractLavakord internal constructor( nodesMap[finalName] = node launch { node.check() - node.connect() + node.connect(initialSessionId = session) } } @@ -178,7 +178,7 @@ public abstract class AbstractLavakord internal constructor( } /** - * Forwards an voice server update event to Lavalink. + * Forwards a voice server update event to Lavalink. */ protected suspend fun forwardVoiceEvent( link: Link, @@ -203,7 +203,7 @@ public abstract class AbstractLavakord internal constructor( } /** Called on websocket connect without resuming */ - internal suspend fun onNewSession(node: Node) { + internal fun onNewSession(node: Node) { if (!options.link.autoReconnect) return linksMap.values.filter { it.node == node }.forEach { launch { diff --git a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/NodeImpl.kt b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/NodeImpl.kt index 7cd86d1a..d21ac39f 100644 --- a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/NodeImpl.kt +++ b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/NodeImpl.kt @@ -52,7 +52,7 @@ internal class NodeImpl( override val host: Url, override val name: String, override val authenticationHeader: String, - override val lavakord: AbstractLavakord, + override val lavakord: AbstractLavakord ) : Node { private val resumeTimeout = lavakord.options.link.resumeTimeout @@ -101,9 +101,11 @@ internal class NodeImpl( } @OptIn(DelicateCoroutinesApi::class) - internal suspend fun connect(resume: Boolean = false) { + internal suspend fun connect(doResume: Boolean = false, initialSessionId: String? = null) { + val resume = doResume || initialSessionId != null + sessionId = initialSessionId ?: sessionId session = try { - connect(resume) { + connect(resume || initialSessionId != null) { addUrl() timeout { requestTimeoutMillis = HttpTimeoutConfig.INFINITE_TIMEOUT_MS