mirror of
https://github.com/KRTirtho/spotube.git
synced 2026-08-05 19:59:51 +00:00
fix: enhance Discord RPC connection management with cooldown and error handling
This commit is contained in:
parent
032d9d174b
commit
e5993b8ba1
@ -30,6 +30,7 @@ import kotlinx.coroutines.flow.combine
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.koin.core.component.KoinComponent
|
import org.koin.core.component.KoinComponent
|
||||||
import uniffi.compose_app.DiscordRpcClient
|
import uniffi.compose_app.DiscordRpcClient
|
||||||
|
import kotlin.time.Clock
|
||||||
|
|
||||||
class DiscordRpcService(
|
class DiscordRpcService(
|
||||||
private val audioPlayer: AudioPlayerInterface,
|
private val audioPlayer: AudioPlayerInterface,
|
||||||
@ -43,6 +44,9 @@ class DiscordRpcService(
|
|||||||
|
|
||||||
private val clientId = "1176718791388975124"
|
private val clientId = "1176718791388975124"
|
||||||
private var rpcClient: DiscordRpcClient? = null
|
private var rpcClient: DiscordRpcClient? = null
|
||||||
|
private var lastConnectionAttempt: Long = 0
|
||||||
|
private var connectionFailed: Boolean = false
|
||||||
|
private val connectionCooldownMs = 30_000L // 30 seconds cooldown between connection attempts
|
||||||
|
|
||||||
init {
|
init {
|
||||||
start()
|
start()
|
||||||
@ -106,17 +110,48 @@ class DiscordRpcService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun ensureConnected() {
|
private fun ensureConnected() {
|
||||||
if (rpcClient != null) return
|
val client = rpcClient
|
||||||
|
if (client != null) {
|
||||||
|
try {
|
||||||
|
if (client.isConnected()) return
|
||||||
|
logger.w { "Discord RPC connection lost, will retry after cooldown" }
|
||||||
|
client.disconnect()
|
||||||
|
rpcClient = null
|
||||||
|
connectionFailed = true
|
||||||
|
lastConnectionAttempt = currentTimeMillis()
|
||||||
|
return
|
||||||
|
} catch (e: Exception) {
|
||||||
|
logger.w { "Discord RPC connection check failed, will retry after cooldown" }
|
||||||
|
rpcClient = null
|
||||||
|
connectionFailed = true
|
||||||
|
lastConnectionAttempt = currentTimeMillis()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check cooldown period
|
||||||
|
val now = currentTimeMillis()
|
||||||
|
if (connectionFailed && (now - lastConnectionAttempt) < connectionCooldownMs) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
lastConnectionAttempt = now
|
||||||
try {
|
try {
|
||||||
val client = DiscordRpcClient(clientId)
|
val newClient = DiscordRpcClient(clientId)
|
||||||
client.connect()
|
newClient.connect()
|
||||||
rpcClient = client
|
rpcClient = newClient
|
||||||
|
connectionFailed = false
|
||||||
logger.i { "Connected to Discord RPC" }
|
logger.i { "Connected to Discord RPC" }
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logger.e(e) { "Failed to connect to Discord RPC" }
|
connectionFailed = true
|
||||||
|
logger.d { "Discord not available, will retry in ${connectionCooldownMs / 1000}s" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun currentTimeMillis(): Long {
|
||||||
|
return Clock.System.now().toEpochMilliseconds()
|
||||||
|
}
|
||||||
|
|
||||||
private fun updatePresence(
|
private fun updatePresence(
|
||||||
title: String,
|
title: String,
|
||||||
artist: String,
|
artist: String,
|
||||||
@ -125,8 +160,9 @@ class DiscordRpcService(
|
|||||||
positionMs: Long,
|
positionMs: Long,
|
||||||
durationMs: Long,
|
durationMs: Long,
|
||||||
) {
|
) {
|
||||||
|
val client = rpcClient ?: return
|
||||||
try {
|
try {
|
||||||
rpcClient?.updatePresence(
|
client.updatePresence(
|
||||||
title = title,
|
title = title,
|
||||||
artist = artist,
|
artist = artist,
|
||||||
album = album,
|
album = album,
|
||||||
@ -135,15 +171,22 @@ class DiscordRpcService(
|
|||||||
durationMs = durationMs,
|
durationMs = durationMs,
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logger.e(e) { "Failed to update Discord RPC presence" }
|
rpcClient = null
|
||||||
|
connectionFailed = true
|
||||||
|
lastConnectionAttempt = currentTimeMillis()
|
||||||
|
logger.d { "Discord RPC update failed, will retry after cooldown" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun clearPresence() {
|
private fun clearPresence() {
|
||||||
|
val client = rpcClient ?: return
|
||||||
try {
|
try {
|
||||||
rpcClient?.clearPresence()
|
client.clearPresence()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logger.e(e) { "Failed to clear Discord RPC presence" }
|
rpcClient = null
|
||||||
|
connectionFailed = true
|
||||||
|
lastConnectionAttempt = currentTimeMillis()
|
||||||
|
logger.d { "Discord RPC clear failed, will retry after cooldown" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user