Skip to content

Commit

Permalink
Don't create player when missing on player update
Browse files Browse the repository at this point in the history
Also move stack traces to trace level
  • Loading branch information
duncte123 committed Feb 8, 2024
1 parent 8e3b493 commit a61885d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 36 deletions.
21 changes: 0 additions & 21 deletions src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -399,27 +399,6 @@ class LavalinkNode(
*/
fun getCachedPlayer(guildId: Long): LavalinkPlayer? = playerCache[guildId]

/**
* Gets a player from the local cache. If the player is not in the local cache, it will be created.
*
* @param guildId The guild id of the player.
*
* @return The local player. This player may not exist on the [LavalinkNode] yet.
*/
internal fun getOrAssumePlayer(guildId: Long): LavalinkPlayer {
val cachedPlayer = playerCache[guildId]

if (cachedPlayer == null) {
val newPlayer = newPlayer(this, guildId.toString())

playerCache[guildId] = newPlayer

return newPlayer
}

return cachedPlayer
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
10 changes: 0 additions & 10 deletions src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ import dev.arbjerg.lavalink.client.protocol.toCustom
import dev.arbjerg.lavalink.protocol.v4.*
import kotlin.math.min

internal fun newPlayer(node: LavalinkNode, guildId: String) = LavalinkPlayer(node, Player(
guildId = guildId,
track = null,
volume = 100,
paused = false,
state = PlayerState(0, 0, false, -1),
voice = VoiceState("", "", ""),
filters = Filters()
))

/**
* Represents a player that is tied to a guild.
*/
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos
val update = event as Message.PlayerUpdateEvent
val idLong = update.guildId.toLong()

// Create a local player on the node if we don't have one.
// There probably is an edge-case where this will happen.
node.getOrAssumePlayer(idLong).state = update.state
node.getCachedPlayer(idLong)?.state = update.state
node.lavalink.getLinkIfCached(idLong)?.state = if (update.state.connected) {
LinkState.CONNECTED
} else {
Expand Down Expand Up @@ -144,7 +142,8 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos
}

is ConnectException -> {
logger.error("Failed to connect to WS of ${node.name} (${node.baseUri}), retrying in ${reconnectInterval / 1000} seconds", t)
logger.error("Failed to connect to WS of ${node.name} (${node.baseUri}), retrying in ${reconnectInterval / 1000} seconds")
logger.trace("Failed to connect to WS of ${node.name} (${node.baseUri}), retrying in ${reconnectInterval / 1000} seconds", t)
}

is SocketException -> {
Expand All @@ -154,7 +153,8 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos
return
}

logger.error("Socket error on ${node.name}, reconnecting in ${reconnectInterval / 1000} seconds", t)
logger.error("Socket error on ${node.name}, reconnecting in ${reconnectInterval / 1000} seconds")
logger.trace("Socket error on ${node.name}, reconnecting in ${reconnectInterval / 1000} seconds", t)
node.available = false
open = false
}
Expand Down

0 comments on commit a61885d

Please sign in to comment.