Skip to content

Commit a6bb549

Browse files
committed
Allow for getting cached players
1 parent 63a1bd6 commit a6bb549

File tree

5 files changed

+66
-6
lines changed

5 files changed

+66
-6
lines changed

src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkClient.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package dev.arbjerg.lavalink.client
22

3-
import dev.arbjerg.lavalink.client.loadbalancing.builtin.DefaultLoadBalancer
43
import dev.arbjerg.lavalink.client.loadbalancing.ILoadBalancer
54
import dev.arbjerg.lavalink.client.loadbalancing.IRegionFilter
65
import dev.arbjerg.lavalink.client.loadbalancing.VoiceRegion
6+
import dev.arbjerg.lavalink.client.loadbalancing.builtin.DefaultLoadBalancer
77
import dev.arbjerg.lavalink.internal.ReconnectTask
88
import dev.arbjerg.lavalink.protocol.v4.Message
99
import reactor.core.Disposable
@@ -114,7 +114,6 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {
114114
if (!linkMap.containsKey(guildId)) {
115115
val bestNode = loadBalancer.selectNode(region)
116116
linkMap[guildId] = Link(guildId, bestNode)
117-
bestNode.playerCache[guildId] = newPlayer(bestNode, guildId.toString())
118117
}
119118

120119
return linkMap[guildId]!!

src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,27 @@ class LavalinkNode(
141141
}
142142
}
143143

144+
/**
145+
* Gets a player from the local cache. If the player is not in the local cache, it will be created.
146+
*
147+
* @param guildId The guild id of the player.
148+
*
149+
* @return The local player. This player may not exist on the [LavalinkNode] yet.
150+
*/
151+
fun getOrCreateCachedPlayer(guildId: Long): LavalinkPlayer {
152+
val cachedPlayer = playerCache[guildId]
153+
154+
if (cachedPlayer == null) {
155+
val newPlayer = newPlayer(this, guildId.toString())
156+
157+
playerCache[guildId] = newPlayer
158+
159+
return newPlayer
160+
}
161+
162+
return cachedPlayer
163+
}
164+
144165
fun updatePlayer(guildId: Long, updateConsumer: Consumer<PlayerUpdateBuilder>): Mono<LavalinkPlayer> {
145166
val update = createOrUpdatePlayer(guildId)
146167

@@ -386,7 +407,14 @@ class LavalinkNode(
386407
}
387408
}
388409

389-
internal fun getCachedPlayer(guildId: Long): LavalinkPlayer? = playerCache[guildId]
410+
/**
411+
* Get a [LavalinkPlayer] from the player cache.
412+
*
413+
* @return The cached player, or null if not yet cached.
414+
*
415+
* @see [getOrCreateCachedPlayer]
416+
*/
417+
fun getCachedPlayer(guildId: Long): LavalinkPlayer? = playerCache[guildId]
390418

391419
override fun equals(other: Any?): Boolean {
392420
if (this === other) return true

src/main/kotlin/dev/arbjerg/lavalink/client/Link.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,19 @@ class Link(
2222
internal set
2323

2424
/**
25-
* Gets the player for this link. If the player is not cached, it will be retrieved from the server.
25+
* Gets the player for this link. If the player is not cached, it will be retrieved or created on the server.
26+
*
27+
* If you just want a local player instead, you can use [getOrCreateCachedPlayer]
2628
*/
2729
fun getPlayer() = node.getPlayer(guildId)
2830

31+
/**
32+
* Gets the cached player for this link. If the player is not cached it will be created locally.
33+
*
34+
* To ensure a player also exist on the node, you can use [getPlayer]
35+
*/
36+
fun getOrCreateCachedPlayer() = node.getOrCreateCachedPlayer(guildId)
37+
2938
/**
3039
* Destroys the player for this link. This will also remove the link from the client.
3140
*/

src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos
7070
val update = event as Message.PlayerUpdateEvent
7171
val idLong = update.guildId.toLong()
7272

73-
node.getCachedPlayer(idLong)?.state = update.state
73+
// Create a local player on the node if we don't have one.
74+
// There probably is an edge-case where this will happen.
75+
node.getOrCreateCachedPlayer(idLong).state = update.state
7476
node.lavalink.getLinkIfCached(idLong)?.state = if (update.state.connected) {
7577
LinkState.CONNECTED
7678
} else {

testbot/src/main/java/me/duncte123/testbot/JDAListener.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public void onReady(@NotNull ReadyEvent event) {
3939
Commands.slash("join", "Join the voice channel you are in."),
4040
Commands.slash("leave", "Leaves the vc"),
4141
Commands.slash("stop", "Stops the current track"),
42-
Commands.slash("pause", "Pause or unpause the plauer"),
42+
Commands.slash("pause", "Pause or unpause the player"),
43+
Commands.slash("now-playing", "Shows what is currently playing"),
4344
Commands.slash("play", "Play a song")
4445
.addOption(
4546
OptionType.STRING,
@@ -75,6 +76,27 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
7576
event.getJDA().getDirectAudioController().disconnect(event.getGuild());
7677
event.reply("Leaving your channel!").queue();
7778
break;
79+
case "now-playing": {
80+
final var link = this.client.getLink(event.getGuild().getIdLong());
81+
final var player = link.getOrCreateCachedPlayer();
82+
final var track = player.getTrack();
83+
84+
if (track == null) {
85+
event.reply("Nothing playing currently!").queue();
86+
break;
87+
}
88+
89+
final var trackInfo = track.getInfo();
90+
91+
event.reply(
92+
"Currently playing: %s\nDuration: %s/%s".formatted(
93+
trackInfo.getTitle(),
94+
player.getPosition(),
95+
trackInfo.getLength()
96+
)
97+
).queue();
98+
break;
99+
}
78100
case "pause":
79101
this.client.getLink(event.getGuild().getIdLong())
80102
.getPlayer()

0 commit comments

Comments
 (0)