Skip to content

Commit

Permalink
Use onSuccess instead of onNext for rest handlers
Browse files Browse the repository at this point in the history
Fixes #17
  • Loading branch information
duncte123 committed Jan 18, 2024
1 parent 7b7fc55 commit fc31fc9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class PlayerUpdateBuilder internal constructor(private val node: LavalinkNode, p
override fun subscribe(actual: CoreSubscriber<in LavalinkPlayer>) {
node.rest.updatePlayer(build(), guildId, noReplace)
.map { it.toLavalinkPlayer(node) }
.doOnNext {
.doOnSuccess {
// Update player in cache
node.playerCache[guildId] = it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions src/test/kotlin/testScript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrackStartEvent>()
Expand Down Expand Up @@ -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" -> {
Expand Down

0 comments on commit fc31fc9

Please sign in to comment.