Skip to content

Commit

Permalink
Change voice connection handling
Browse files Browse the repository at this point in the history
- Rely on player state for CONNECTED state
- Do not destroy player on disconnect
- Improved debug logging in some places
  • Loading branch information
freyacodes committed Nov 27, 2023
1 parent a119255 commit 32c40cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public abstract class AbstractLink(node: Node, final override val guildId: ULong
abstract override val lavakord: AbstractLavakord
override var lastChannelId: ULong? = null
override var state: State = State.NOT_CONNECTED
set(value) {
if (field == value) return
LOG.debug { "$this: $state -> $value" }
field = value
}
private var cachedVoiceState: VoiceState? = null

override suspend fun onDisconnected() {
state = State.NOT_CONNECTED
node.destroyPlayer(guildId)
cachedVoiceState = null
}

Expand All @@ -39,9 +43,7 @@ public abstract class AbstractLink(node: Node, final override val guildId: ULong

try {
(player as WebsocketPlayer).recreatePlayer(node as NodeImpl, voiceState)
if (voiceState != null) {
state = State.CONNECTED
}
LOG.debug { "$this: recreated player on $node" }
} catch (e: Exception) {
state = State.NOT_CONNECTED
throw e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dev.arbjerg.lavalink.protocol.v4.Stats
import dev.arbjerg.lavalink.protocol.v4.toOmissible
import dev.schlaubi.lavakord.Plugin
import dev.schlaubi.lavakord.audio.Event
import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.lavakord.rest.getInfo
import dev.schlaubi.lavakord.rest.getVersion
Expand Down Expand Up @@ -184,13 +185,23 @@ internal class NodeImpl(
LOG.warn(e) {"Could not parse event"}
}
when (event) {
is Message.PlayerUpdateEvent -> (lavakord.getLink(event.guildId).player as WebsocketPlayer)
.provideState(event.state)
is Message.PlayerUpdateEvent -> {
val link = lavakord.getLink(event.guildId) as AbstractLink

if (event.state.connected && link.state == Link.State.CONNECTING) {
link.state = Link.State.CONNECTING
} else if (!event.state.connected && link.state == Link.State.DISCONNECTING) {
link.state = Link.State.NOT_CONNECTED
}

(link.player as WebsocketPlayer).provideState(event.state)
}

is Message.EmittedEvent.WebSocketClosedEvent -> {
// These codes represent an invalid session
// See https://discord.com/developers/docs/topics/opcodes-and-status-codes#voice-voice-close-event-codes
if (event.code == 4004 || event.code == 4006 || event.code == 4009 || event.code == 4014) {
LOG.debug { "Node $name received close code ${event.code} for guild ${event.guildId}" }
lavakord.getLink(event.guildId).onDisconnected()
}
}
Expand Down

0 comments on commit 32c40cc

Please sign in to comment.