diff --git a/src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt b/src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt index fd2d5e4..6e0c005 100644 --- a/src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt +++ b/src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt @@ -63,7 +63,6 @@ class LavalinkNode( var available: Boolean = false internal set - // TODO: cache player per link instead? /** * A local player cache, allows us to not call the rest client every time we need a player. */ @@ -99,7 +98,7 @@ class LavalinkNode( return rest.getPlayers() .map { it.players.map { pl -> pl.toLavalinkPlayer(this) } } - .doOnNext { + .doOnSuccess { it.forEach { player -> playerCache[player.guildId] = player } @@ -123,7 +122,7 @@ class LavalinkNode( return rest.getPlayer(guildId) .map { it.toLavalinkPlayer(this) } .onErrorResume { createOrUpdatePlayer(guildId) } - .doOnNext { + .doOnSuccess { // Update the player internally upon retrieving it. playerCache[it.guildId] = it } @@ -155,8 +154,8 @@ class LavalinkNode( if (!available) return Mono.error(IllegalStateException("Node is not available")) return rest.destroyPlayer(guildId) - .doOnNext { - playerCache.remove(guildId) + .doOnSuccess { + removeCachedPlayer(guildId) } } diff --git a/src/main/kotlin/dev/arbjerg/lavalink/client/PlayerUpdateBuilder.kt b/src/main/kotlin/dev/arbjerg/lavalink/client/PlayerUpdateBuilder.kt index a9d133a..9b1b9a7 100644 --- a/src/main/kotlin/dev/arbjerg/lavalink/client/PlayerUpdateBuilder.kt +++ b/src/main/kotlin/dev/arbjerg/lavalink/client/PlayerUpdateBuilder.kt @@ -135,7 +135,7 @@ class PlayerUpdateBuilder internal constructor(private val node: LavalinkNode, p override fun subscribe(actual: CoreSubscriber) { node.rest.updatePlayer(build(), guildId, noReplace) .map { it.toLavalinkPlayer(node) } - .doOnNext { + .doOnSuccess { // Update player in cache node.playerCache[guildId] = it } diff --git a/src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt b/src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt index 0bb555c..4821d22 100644 --- a/src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt +++ b/src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt @@ -81,10 +81,10 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos when (event) { is Message.EmittedEvent.TrackStartEvent -> { - node.playerCache[event.guildId.toLong()]?.track = event.track.toCustom() + node.getCachedPlayer(event.guildId.toLong())?.track = event.track.toCustom() } is Message.EmittedEvent.TrackEndEvent -> { - node.playerCache[event.guildId.toLong()]?.track = null + node.getCachedPlayer(event.guildId.toLong())?.track = null } else -> {} } diff --git a/src/main/kotlin/dev/arbjerg/lavalink/libraries/jda/JDAVoiceUpdateListener.kt b/src/main/kotlin/dev/arbjerg/lavalink/libraries/jda/JDAVoiceUpdateListener.kt index 7993528..4a241a8 100644 --- a/src/main/kotlin/dev/arbjerg/lavalink/libraries/jda/JDAVoiceUpdateListener.kt +++ b/src/main/kotlin/dev/arbjerg/lavalink/libraries/jda/JDAVoiceUpdateListener.kt @@ -22,7 +22,7 @@ class JDAVoiceUpdateListener(private val lavalink: LavalinkClient) : VoiceDispat override fun onVoiceStateUpdate(update: VoiceDispatchInterceptor.VoiceStateUpdate): Boolean { val channel = update.channel val link = lavalink.getLinkIfCached(update.guildIdLong) ?: return false - val player = link.node.playerCache[update.guildIdLong] ?: return false + val player = link.node.getCachedPlayer(update.guildIdLong) ?: return false val playerState = player.state if (channel == null) { diff --git a/src/test/kotlin/testScript.kt b/src/test/kotlin/testScript.kt index 5beff1a..233514a 100644 --- a/src/test/kotlin/testScript.kt +++ b/src/test/kotlin/testScript.kt @@ -84,19 +84,19 @@ fun main() { fun registerNode(client: LavalinkClient) { listOf( - /*client.addNode( + client.addNode( "Testnode", URI.create("ws://localhost:2333"), "youshallnotpass", RegionGroup.EUROPE - ),*/ + ), - client.addNode( + /*client.addNode( "Mac-mini", URI.create("ws://192.168.1.139:2333/bepis"), "youshallnotpass", RegionGroup.US - ) + )*/ ) .forEach { node -> node.on() @@ -171,6 +171,11 @@ private fun handleSlash(lavalink: LavalinkClient, event: SlashCommandInteraction // Disconnecting automatically destroys the player event.jda.directAudioController.disconnect(event.guild!!) event.reply("Leaving your channel!").queue() + + val guildId = event.guild!!.idLong + val link = lavalink.getLink(guildId) + + println(link.node.playerCache) } "play" -> {