From 6c9bdc9820aa71d9226468ed0a47a32ed58f8353 Mon Sep 17 00:00:00 2001 From: duncte123 Date: Mon, 12 Feb 2024 12:16:20 +0100 Subject: [PATCH] rename getLink to getOrCreateLink --- README.md | 2 +- .../dev/arbjerg/lavalink/client/LavalinkClient.kt | 15 ++++++++++++++- .../libraries/discord4j/D4JVoiceUpdateHandler.kt | 2 +- .../libraries/jda/JDAVoiceUpdateListener.kt | 2 +- src/test/java/JavaJDAExample.java | 10 +++++----- src/test/kotlin/d4jTestScript.kt | 2 +- src/test/kotlin/testScript.kt | 8 ++++---- .../java/me/duncte123/testbot/JDAListener.java | 14 +++++++------- 8 files changed, 34 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 5d5024d..a433b89 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkClient.kt b/src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkClient.kt index 0327804..2d6007a 100644 --- a/src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkClient.kt +++ b/src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkClient.kt @@ -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) diff --git a/src/main/kotlin/dev/arbjerg/lavalink/libraries/discord4j/D4JVoiceUpdateHandler.kt b/src/main/kotlin/dev/arbjerg/lavalink/libraries/discord4j/D4JVoiceUpdateHandler.kt index 53ecabe..46c2097 100644 --- a/src/main/kotlin/dev/arbjerg/lavalink/libraries/discord4j/D4JVoiceUpdateHandler.kt +++ b/src/main/kotlin/dev/arbjerg/lavalink/libraries/discord4j/D4JVoiceUpdateHandler.kt @@ -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() diff --git a/src/main/kotlin/dev/arbjerg/lavalink/libraries/jda/JDAVoiceUpdateListener.kt b/src/main/kotlin/dev/arbjerg/lavalink/libraries/jda/JDAVoiceUpdateListener.kt index cb3812e..a5d7bcd 100644 --- a/src/main/kotlin/dev/arbjerg/lavalink/libraries/jda/JDAVoiceUpdateListener.kt +++ b/src/main/kotlin/dev/arbjerg/lavalink/libraries/jda/JDAVoiceUpdateListener.kt @@ -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) } diff --git a/src/test/java/JavaJDAExample.java b/src/test/java/JavaJDAExample.java index 542ddd7..68a31e4 100644 --- a/src/test/java/JavaJDAExample.java +++ b/src/test/java/JavaJDAExample.java @@ -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) ) @@ -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) -> { @@ -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 @@ -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") @@ -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 -> { diff --git a/src/test/kotlin/d4jTestScript.kt b/src/test/kotlin/d4jTestScript.kt index cd9eff1..fa6354f 100644 --- a/src/test/kotlin/d4jTestScript.kt +++ b/src/test/kotlin/d4jTestScript.kt @@ -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() diff --git a/src/test/kotlin/testScript.kt b/src/test/kotlin/testScript.kt index 5cc5d49..abf845b 100644 --- a/src/test/kotlin/testScript.kt +++ b/src/test/kotlin/testScript.kt @@ -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() @@ -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{ it.get().path("/v4/loadtracks?identifier=ytsearch%3Anever%20gonna%20give%20you%20up") @@ -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) } @@ -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) { diff --git a/testbot/src/main/java/me/duncte123/testbot/JDAListener.java b/testbot/src/main/java/me/duncte123/testbot/JDAListener.java index 50193f1..3702f27 100644 --- a/testbot/src/main/java/me/duncte123/testbot/JDAListener.java +++ b/testbot/src/main/java/me/duncte123/testbot/JDAListener.java @@ -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) ) @@ -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) { @@ -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) -> { @@ -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( @@ -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( @@ -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")