Skip to content

Commit

Permalink
Fix exception when connecting to WS
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Dec 7, 2024
1 parent bf154f0 commit ef95a95
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.schlaubi.lavakord.audio
import dev.schlaubi.lavakord.LavaKord
import dev.schlaubi.lavakord.audio.Link.State
import dev.schlaubi.lavakord.audio.player.Player
import kotlinx.coroutines.CoroutineScope

/**
* Representation of a link between a Guild and a Lavalink node.
Expand All @@ -14,7 +15,7 @@ import dev.schlaubi.lavakord.audio.player.Player
* @property guildId the id of the Guild this [Link] is connected to
* @property lastChannelId the id of the last channel this Link is connected to
*/
public interface Link {
public interface Link : CoroutineScope {
public val node: Node
public val player: Player
public val lavakord: LavaKord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.Link.State
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.lavakord.audio.player.Player
import dev.schlaubi.lavakord.audio.player.node
import dev.schlaubi.lavakord.rest.destroyPlayer
import dev.schlaubi.lavakord.rest.updatePlayer
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlin.coroutines.CoroutineContext

/**
* Abstract implementation of [Link].
*/
public abstract class AbstractLink(node: Node, final override val guildId: ULong) : Link {

override val coroutineContext: CoroutineContext = node.lavakord.coroutineContext + SupervisorJob()
final override var node: Node = node
private set

override val player: Player = WebsocketPlayer(node as NodeImpl, guildId)
override val player: Player by lazy { WebsocketPlayer(this, guildId) }
abstract override val lavakord: AbstractLavakord
override var lastChannelId: ULong? = null
override var state: State = State.NOT_CONNECTED
Expand All @@ -42,7 +45,7 @@ public abstract class AbstractLink(node: Node, final override val guildId: ULong
state = if (voiceState != null) State.CONNECTING else State.NOT_CONNECTED

try {
(player as WebsocketPlayer).recreatePlayer(node as NodeImpl, voiceState)
(player as WebsocketPlayer).recreatePlayer(voiceState)
LOG.debug { "$this: recreated player on $node" }
} catch (e: Exception) {
state = State.NOT_CONNECTED
Expand All @@ -59,6 +62,7 @@ public abstract class AbstractLink(node: Node, final override val guildId: ULong
node.destroyPlayer(guildId)
lavakord.removeDestroyedLink(this)
state = State.DESTROYED
cancel()
}

internal suspend fun onVoiceServerUpdate(update: VoiceState) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package dev.schlaubi.lavakord.audio.internal

import dev.arbjerg.lavalink.protocol.v4.*
import dev.schlaubi.lavakord.audio.Event
import dev.schlaubi.lavakord.audio.TrackEndEvent
import dev.schlaubi.lavakord.audio.TrackStartEvent
import dev.schlaubi.lavakord.audio.on
import dev.schlaubi.lavakord.audio.*
import dev.schlaubi.lavakord.audio.player.Equalizer
import dev.schlaubi.lavakord.audio.player.Filters
import dev.schlaubi.lavakord.audio.player.PlayOptions
import dev.schlaubi.lavakord.audio.player.Player
import dev.schlaubi.lavakord.rest.models.FiltersObject
import dev.schlaubi.lavakord.rest.models.toLavalink
import dev.schlaubi.lavakord.rest.updatePlayer
import kotlinx.atomicfu.AtomicBoolean
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
Expand All @@ -22,13 +18,11 @@ import kotlinx.datetime.Instant
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Player {
internal var node: NodeImpl = node
private set
internal class WebsocketPlayer(private val link: Link, internal val guildId: ULong) : Player, CoroutineScope by link {
override var playingTrack: Track? = null
override val coroutineScope: CoroutineScope
get() = node.coroutineScope
override var paused: Boolean = false
override val coroutineScope: CoroutineScope
get() = this
private var lastPosition: Duration = 0.milliseconds
private var updateTime: Instant = Instant.DISTANT_PAST
override val positionDuration: Duration
Expand Down Expand Up @@ -57,7 +51,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
}

override val events: Flow<Event>
get() = node.events.filter { it.guildId == guildId }
get() = link.lavakord.events.filter { it.guildId == guildId }

init {
on(consumer = ::handleNewTrack)
Expand All @@ -76,7 +70,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
playOptionsBuilder: PlayOptions.() -> Unit
) {
val options = PlayOptions().apply(playOptionsBuilder)
node.updatePlayer(
link.node.updatePlayer(
guildId, options.noReplace, PlayerUpdate(
encodedTrack = track.toOmissible(),
identifier = identifier.toOmissible(),
Expand Down Expand Up @@ -104,7 +98,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
}

override suspend fun stopTrack() {
node.updatePlayer(
link.node.updatePlayer(
guildId,
request = PlayerUpdate(encodedTrack = Omissible(null))
)
Expand All @@ -113,7 +107,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl

override suspend fun pause(doPause: Boolean) {
if (paused == doPause) return
node.updatePlayer(
link.node.updatePlayer(
guildId,
request = PlayerUpdate(paused = doPause.toOmissible())
)
Expand All @@ -123,7 +117,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
override suspend fun seekTo(position: Long) {
checkNotNull(playingTrack) { "Not currently playing anything" }

node.updatePlayer(
link.node.updatePlayer(
guildId,
request = PlayerUpdate(position = position.toOmissible())
)
Expand All @@ -138,12 +132,11 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
lastPosition = state.position.milliseconds
}

internal suspend fun recreatePlayer(node: NodeImpl, voiceState: VoiceState?) {
this.node = node
internal suspend fun recreatePlayer(voiceState: VoiceState?) {
val position = if (playingTrack == null) null else positionDuration.inWholeMilliseconds

isRecreating.value = true
node.updatePlayer(
link.node.updatePlayer(
guildId, noReplace = false, PlayerUpdate(
encodedTrack = playingTrack?.encoded.toOmissible(),
identifier = Omissible.omitted(),
Expand Down

0 comments on commit ef95a95

Please sign in to comment.