Skip to content

Commit ad9a687

Browse files
committed
Implement post-resume synchronisation
1 parent f5386ef commit ad9a687

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,31 @@ class LavalinkNode(
427427
lavalink.transferOrphansTo(this)
428428
}
429429

430+
internal fun synchronizeAfterResume() {
431+
getPlayers().subscribe { players ->
432+
val remoteGuildIds = players.map { it.guildId }
430433

434+
players.forEach { player ->
435+
playerCache[player.guildId] = player
436+
437+
val link = lavalink.getLinkIfCached(player.guildId) ?: return@forEach
438+
if (link.node != this) return@forEach
439+
440+
link.state = if (player.state.connected) {
441+
LinkState.CONNECTED
442+
} else {
443+
LinkState.DISCONNECTED
444+
}
445+
}
446+
447+
val missingIds = playerCache.keys().toList() - remoteGuildIds.toSet()
448+
missingIds.forEach { guildId ->
449+
playerCache.remove(guildId)
450+
val link = lavalink.getLinkIfCached(guildId) ?: return@forEach
451+
if (link.node == this) link.state = LinkState.DISCONNECTED
452+
}
453+
}
454+
}
431455

432456
override fun equals(other: Any?): Boolean {
433457
if (this === other) return true

src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos
8383
node.rest.getSession().subscribe { node.cachedSession = null }
8484
}
8585

86+
node.synchronizeAfterResume()
87+
8688
// Move players from older, unavailable nodes to ourselves.
8789
node.transferOrphansToSelf()
8890
}

0 commit comments

Comments
 (0)