Skip to content

Commit

Permalink
Only create player if response is 404
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Feb 8, 2024
1 parent e3682c9 commit 0bf628d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dev.arbjerg.lavalink.client.protocol.toCustom
import dev.arbjerg.lavalink.client.protocol.toLavalinkLoadResult
import dev.arbjerg.lavalink.internal.LavalinkRestClient
import dev.arbjerg.lavalink.internal.LavalinkSocket
import dev.arbjerg.lavalink.internal.error.RestException
import dev.arbjerg.lavalink.internal.fromRawJson
import dev.arbjerg.lavalink.internal.loadbalancing.Penalties
import dev.arbjerg.lavalink.internal.toLavalinkPlayer
Expand Down Expand Up @@ -120,6 +121,7 @@ class LavalinkNode(

/**
* Gets the player from the guild id. If the player is not cached, it will be retrieved from the server.
* If the player does not exist on the node, it will be created.
*
* @param guildId The guild id of the player.
*
Expand All @@ -134,8 +136,14 @@ class LavalinkNode(

return rest.getPlayer(guildId)
.map { it.toLavalinkPlayer(this) }
// TODO: check for 404 status code, if not 404, don't create
.onErrorResume { createOrUpdatePlayer(guildId) }
// Create the player if it doesn't exist on the node.
.onErrorResume {
if (it is RestException && it.code == 404) {
createOrUpdatePlayer(guildId)
} else {
it.toMono()
}
}
.doOnSuccess {
// Update the player internally upon retrieving it.
playerCache[it.guildId] = it
Expand Down

0 comments on commit 0bf628d

Please sign in to comment.