Skip to content

Commit

Permalink
rename getLink to getOrCreateLink
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Feb 12, 2024
1 parent 533f8e1 commit 6c9bdc9
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void onDiscordVoiceServerUpdate(VoiceServerUpdateEvent event) {
VoiceRegion region = VoiceRegion.fromEndpoint(event.getEndpoint());

// You can omit the region parameter if you dont need region balancing.
Link link = lavalink.getLink(event.getGuildId(), region);
Link link = lavalink.getOrCreateLink(event.getGuildId(), region);

// Finally, tell lavalink to connect.
link.onVoiceServerUpdate(lavalinkVoiceState);
Expand Down
15 changes: 14 additions & 1 deletion src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,21 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {
* @param guildId The id of the guild
* @param region (not currently used) The target voice region of when to select a node
*/
@Deprecated(
message = "Method name unclear",
replaceWith = ReplaceWith("getOrCreateLink(guildId, region)")
)
@JvmOverloads
fun getLink(guildId: Long, region: VoiceRegion? = null): Link {
fun getLink(guildId: Long, region: VoiceRegion? = null) = getOrCreateLink(guildId, region)

/**
* Get or crate a link between a guild and a node.
*
* @param guildId The id of the guild
* @param region (not currently used) The target voice region of when to select a node
*/
@JvmOverloads
fun getOrCreateLink(guildId: Long, region: VoiceRegion? = null): Link {
if (!linkMap.containsKey(guildId)) {
val bestNode = loadBalancer.selectNode(region)
linkMap[guildId] = Link(guildId, bestNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fun GatewayDiscordClient.installVoiceHandler(lavalink: LavalinkClient): Disposab
)

val region = VoiceRegion.fromEndpoint(update.endpoint!!)
val link = lavalink.getLink(update.guildId.asLong(), region)
val link = lavalink.getOrCreateLink(update.guildId.asLong(), region)

link.onVoiceServerUpdate(state)
Mono.empty<Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JDAVoiceUpdateListener(private val lavalink: LavalinkClient) : VoiceDispat
update.sessionId
)
val region = VoiceRegion.fromEndpoint(update.endpoint)
val link = lavalink.getLink(update.guildIdLong, region)
val link = lavalink.getOrCreateLink(update.guildIdLong, region)

link.onVoiceServerUpdate(state)
}
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/JavaJDAExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
joinHelper(event);
break;
case "stop":
this.client.getLink(event.getGuild().getIdLong())
this.client.getOrCreateLink(event.getGuild().getIdLong())
.updatePlayer(
(update) -> update.setTrack(null).setPaused(false)
)
Expand All @@ -155,7 +155,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
event.reply("Leaving your channel!").queue();
break;
case "pause":
this.client.getLink(event.getGuild().getIdLong())
this.client.getOrCreateLink(event.getGuild().getIdLong())
.getPlayer()
.flatMap((player) -> player.setPaused(!player.getPaused()))
.subscribe((player) -> {
Expand All @@ -175,7 +175,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even

final String identifier = event.getOption("identifier").getAsString();
final long guildId = guild.getIdLong();
final Link link = this.client.getLink(guildId);
final Link link = this.client.getOrCreateLink(guildId);

link.loadItem(identifier).subscribe(new FunctionalLoadResultHandler(
// Track loaded
Expand Down Expand Up @@ -239,7 +239,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
break;
}
case "custom-request": {
final Link link = this.client.getLink(event.getGuild().getIdLong());
final Link link = this.client.getOrCreateLink(event.getGuild().getIdLong());

link.getNode().customRequest(
(builder) -> builder.get().path("/version").header("Accept", "text/plain")
Expand All @@ -255,7 +255,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
break;
}
case "custom-json-request": {
final Link link = this.client.getLink(event.getGuild().getIdLong());
final Link link = this.client.getOrCreateLink(event.getGuild().getIdLong());
link.getNode().customJsonRequest(com.github.topi314.lavasearch.protocol.SearchResult.Companion.serializer(),
(builder) -> builder.path("/v4/loadsearch?query=ytsearch%3Anever%20gonna%20give%20you%20up").get()
).doOnSuccess((loadResult -> {
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/d4jTestScript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private fun handleSlash(lavalink: LavalinkClient, event: ChatInputInteractionEve
.flatMap(ApplicationCommandInteractionOption::getValue)
.map(ApplicationCommandInteractionOptionValue::asString)
.get()
val link = lavalink.getLink(event.interaction.guildId.get().asLong())
val link = lavalink.getOrCreateLink(event.interaction.guildId.get().asLong())
link.getPlayer().block()!!.setIdentifier(input)
.subscribe()
event.reply("Playing!!").subscribe()
Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/testScript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private fun handleSlash(lavalink: LavalinkClient, event: SlashCommandInteraction
when (event.fullCommandName) {
"custom-request" -> {
val guildId = event.guild!!.idLong
val link = lavalink.getLink(guildId)
val link = lavalink.getOrCreateLink(guildId)

link.node.customRequest {
it.get()
Expand All @@ -157,7 +157,7 @@ private fun handleSlash(lavalink: LavalinkClient, event: SlashCommandInteraction

"custon-json-request" -> {
val guildId = event.guild!!.idLong
val link = lavalink.getLink(guildId)
val link = lavalink.getOrCreateLink(guildId)

link.node.customJsonRequest<TopiSearchResult>{
it.get().path("/v4/loadtracks?identifier=ytsearch%3Anever%20gonna%20give%20you%20up")
Expand Down Expand Up @@ -189,7 +189,7 @@ private fun handleSlash(lavalink: LavalinkClient, event: SlashCommandInteraction
event.reply("Leaving your channel!").queue()

val guildId = event.guild!!.idLong
val link = lavalink.getLink(guildId)
val link = lavalink.getOrCreateLink(guildId)

println(link.node.playerCache)
}
Expand All @@ -203,7 +203,7 @@ private fun handleSlash(lavalink: LavalinkClient, event: SlashCommandInteraction

val identifier = event.getOption("identifier")!!.asString
val guildId = event.guild!!.idLong
val link = lavalink.getLink(guildId)
val link = lavalink.getOrCreateLink(guildId)

link.loadItem(identifier).subscribe loadItem@ { item ->
when (item) {
Expand Down
14 changes: 7 additions & 7 deletions testbot/src/main/java/me/duncte123/testbot/JDAListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
joinHelper(event);
break;
case "stop":
this.client.getLink(guild.getIdLong())
this.client.getOrCreateLink(guild.getIdLong())
.updatePlayer(
(update) -> update.setTrack(null).setPaused(false)
)
Expand All @@ -83,7 +83,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
event.reply("Leaving your channel!").queue();
break;
case "now-playing": {
final var link = this.client.getLink(guild.getIdLong());
final var link = this.client.getOrCreateLink(guild.getIdLong());
final var player = link.getCachedPlayer();

if (player == null) {
Expand Down Expand Up @@ -111,7 +111,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
break;
}
case "pause":
this.client.getLink(guild.getIdLong())
this.client.getOrCreateLink(guild.getIdLong())
.getPlayer()
.flatMap((player) -> player.setPaused(!player.getPaused()))
.subscribe((player) -> {
Expand All @@ -120,7 +120,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
break;
case "karaoke on": {
final long guildId = guild.getIdLong();
final Link link = this.client.getLink(guildId);
final Link link = this.client.getOrCreateLink(guildId);

link.createOrUpdatePlayer()
.setFilters(
Expand All @@ -136,7 +136,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
}
case "karaoke off": {
final long guildId = guild.getIdLong();
final Link link = this.client.getLink(guildId);
final Link link = this.client.getOrCreateLink(guildId);

link.createOrUpdatePlayer()
.setFilters(
Expand All @@ -159,14 +159,14 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even

final String identifier = event.getOption("identifier").getAsString();
final long guildId = guild.getIdLong();
final Link link = this.client.getLink(guildId);
final Link link = this.client.getOrCreateLink(guildId);

link.loadItem(identifier).subscribe(new AudioLoader(link, event));

break;
}
case "custom-request": {
final Link link = this.client.getLink(guild.getIdLong());
final Link link = this.client.getOrCreateLink(guild.getIdLong());

link.getNode().customRequest(
(builder) -> builder.get().path("/version").header("Accept", "text/plain")
Expand Down

0 comments on commit 6c9bdc9

Please sign in to comment.