Skip to content

Commit ef95a95

Browse files
committed
Fix exception when connecting to WS
1 parent bf154f0 commit ef95a95

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/Link.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dev.schlaubi.lavakord.audio
33
import dev.schlaubi.lavakord.LavaKord
44
import dev.schlaubi.lavakord.audio.Link.State
55
import dev.schlaubi.lavakord.audio.player.Player
6+
import kotlinx.coroutines.CoroutineScope
67

78
/**
89
* Representation of a link between a Guild and a Lavalink node.
@@ -14,7 +15,7 @@ import dev.schlaubi.lavakord.audio.player.Player
1415
* @property guildId the id of the Guild this [Link] is connected to
1516
* @property lastChannelId the id of the last channel this Link is connected to
1617
*/
17-
public interface Link {
18+
public interface Link : CoroutineScope {
1819
public val node: Node
1920
public val player: Player
2021
public val lavakord: LavaKord

core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/AbstractLink.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ import dev.schlaubi.lavakord.audio.Link
77
import dev.schlaubi.lavakord.audio.Link.State
88
import dev.schlaubi.lavakord.audio.Node
99
import dev.schlaubi.lavakord.audio.player.Player
10-
import dev.schlaubi.lavakord.audio.player.node
1110
import dev.schlaubi.lavakord.rest.destroyPlayer
1211
import dev.schlaubi.lavakord.rest.updatePlayer
12+
import kotlinx.coroutines.SupervisorJob
13+
import kotlinx.coroutines.cancel
14+
import kotlin.coroutines.CoroutineContext
1315

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

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

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

4447
try {
45-
(player as WebsocketPlayer).recreatePlayer(node as NodeImpl, voiceState)
48+
(player as WebsocketPlayer).recreatePlayer(voiceState)
4649
LOG.debug { "$this: recreated player on $node" }
4750
} catch (e: Exception) {
4851
state = State.NOT_CONNECTED
@@ -59,6 +62,7 @@ public abstract class AbstractLink(node: Node, final override val guildId: ULong
5962
node.destroyPlayer(guildId)
6063
lavakord.removeDestroyedLink(this)
6164
state = State.DESTROYED
65+
cancel()
6266
}
6367

6468
internal suspend fun onVoiceServerUpdate(update: VoiceState) {

core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/WebsocketPlayer.kt

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package dev.schlaubi.lavakord.audio.internal
22

33
import dev.arbjerg.lavalink.protocol.v4.*
4-
import dev.schlaubi.lavakord.audio.Event
5-
import dev.schlaubi.lavakord.audio.TrackEndEvent
6-
import dev.schlaubi.lavakord.audio.TrackStartEvent
7-
import dev.schlaubi.lavakord.audio.on
4+
import dev.schlaubi.lavakord.audio.*
85
import dev.schlaubi.lavakord.audio.player.Equalizer
96
import dev.schlaubi.lavakord.audio.player.Filters
107
import dev.schlaubi.lavakord.audio.player.PlayOptions
118
import dev.schlaubi.lavakord.audio.player.Player
129
import dev.schlaubi.lavakord.rest.models.FiltersObject
1310
import dev.schlaubi.lavakord.rest.models.toLavalink
1411
import dev.schlaubi.lavakord.rest.updatePlayer
15-
import kotlinx.atomicfu.AtomicBoolean
1612
import kotlinx.atomicfu.atomic
1713
import kotlinx.coroutines.CoroutineScope
1814
import kotlinx.coroutines.flow.Flow
@@ -22,13 +18,11 @@ import kotlinx.datetime.Instant
2218
import kotlin.time.Duration
2319
import kotlin.time.Duration.Companion.milliseconds
2420

25-
internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Player {
26-
internal var node: NodeImpl = node
27-
private set
21+
internal class WebsocketPlayer(private val link: Link, internal val guildId: ULong) : Player, CoroutineScope by link {
2822
override var playingTrack: Track? = null
29-
override val coroutineScope: CoroutineScope
30-
get() = node.coroutineScope
3123
override var paused: Boolean = false
24+
override val coroutineScope: CoroutineScope
25+
get() = this
3226
private var lastPosition: Duration = 0.milliseconds
3327
private var updateTime: Instant = Instant.DISTANT_PAST
3428
override val positionDuration: Duration
@@ -57,7 +51,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
5751
}
5852

5953
override val events: Flow<Event>
60-
get() = node.events.filter { it.guildId == guildId }
54+
get() = link.lavakord.events.filter { it.guildId == guildId }
6155

6256
init {
6357
on(consumer = ::handleNewTrack)
@@ -76,7 +70,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
7670
playOptionsBuilder: PlayOptions.() -> Unit
7771
) {
7872
val options = PlayOptions().apply(playOptionsBuilder)
79-
node.updatePlayer(
73+
link.node.updatePlayer(
8074
guildId, options.noReplace, PlayerUpdate(
8175
encodedTrack = track.toOmissible(),
8276
identifier = identifier.toOmissible(),
@@ -104,7 +98,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
10498
}
10599

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

114108
override suspend fun pause(doPause: Boolean) {
115109
if (paused == doPause) return
116-
node.updatePlayer(
110+
link.node.updatePlayer(
117111
guildId,
118112
request = PlayerUpdate(paused = doPause.toOmissible())
119113
)
@@ -123,7 +117,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
123117
override suspend fun seekTo(position: Long) {
124118
checkNotNull(playingTrack) { "Not currently playing anything" }
125119

126-
node.updatePlayer(
120+
link.node.updatePlayer(
127121
guildId,
128122
request = PlayerUpdate(position = position.toOmissible())
129123
)
@@ -138,12 +132,11 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
138132
lastPosition = state.position.milliseconds
139133
}
140134

141-
internal suspend fun recreatePlayer(node: NodeImpl, voiceState: VoiceState?) {
142-
this.node = node
135+
internal suspend fun recreatePlayer(voiceState: VoiceState?) {
143136
val position = if (playingTrack == null) null else positionDuration.inWholeMilliseconds
144137

145138
isRecreating.value = true
146-
node.updatePlayer(
139+
link.node.updatePlayer(
147140
guildId, noReplace = false, PlayerUpdate(
148141
encodedTrack = playingTrack?.encoded.toOmissible(),
149142
identifier = Omissible.omitted(),

0 commit comments

Comments
 (0)