From c6aa71ed2e8e53f5c6a1d676ce9615357b3372d1 Mon Sep 17 00:00:00 2001 From: Mihai Bocioroaga Date: Mon, 18 Oct 2021 12:34:17 +0100 Subject: [PATCH 1/4] Added search if Play command is used without a URL --- .../arbjerg/ukulele/command/PlayCommand.kt | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt b/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt index fd1229a..05c280a 100644 --- a/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt +++ b/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt @@ -11,6 +11,8 @@ import dev.arbjerg.ukulele.features.HelpContext import dev.arbjerg.ukulele.jda.Command import dev.arbjerg.ukulele.jda.CommandContext import net.dv8tion.jda.api.Permission +import java.net.URL +import java.lang.Exception import org.springframework.stereotype.Component @Component @@ -20,8 +22,14 @@ class PlayCommand( ) : Command("play", "p") { override suspend fun CommandContext.invoke() { if (!ensureVoiceChannel()) return - val identifier = argumentText - apm.loadItem(identifier, Loader(this, player, identifier)) + var identifier = argumentText + if (checkValidUrl(identifier)) { + apm.loadItem(identifier, Loader(this, player, identifier)) + } + else { + identifier = "ytsearch:$identifier" + apm.loadItem(identifier, Loader(this, player, identifier)) + } } fun CommandContext.ensureVoiceChannel(): Boolean { @@ -48,6 +56,16 @@ class PlayCommand( return ourVc != null } + fun checkValidUrl(url: String): Boolean { + return try { + URL(url).toURI() + true + } + catch (e: Exception) { + false + } + } + class Loader( private val ctx: CommandContext, private val player: Player, @@ -63,8 +81,15 @@ class PlayCommand( } override fun playlistLoaded(playlist: AudioPlaylist) { - player.add(*playlist.tracks.toTypedArray()) - ctx.reply("Added `${playlist.tracks.size}` tracks from `${playlist.name}`") + if (identifier.startsWith("ytsearch:")) { + val track = playlist.tracks[0] // Pick first search result from Lavaplayer ytsearch playlist + player.add(track) + ctx.reply("Added `${track.info.title}`") + } + else { + player.add(*playlist.tracks.toTypedArray()) + ctx.reply("Added `${playlist.tracks.size}` tracks from `${playlist.name}`") + } } override fun noMatches() { From 8c17320ec31094849cccb91ac5cd9692a0656698 Mon Sep 17 00:00:00 2001 From: James O'Cull Date: Mon, 25 Oct 2021 21:28:00 +0000 Subject: [PATCH 2/4] Restrict Gradle's memory usage for building on low memory hosts --- gradle.properties | 1 + 1 file changed, 1 insertion(+) create mode 100644 gradle.properties diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..233bfac --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +org.gradle.jvmargs=-Xmx256m From 7c65302843e395102cc22890c20b250e00e179b0 Mon Sep 17 00:00:00 2001 From: James O'Cull Date: Sat, 12 Mar 2022 19:54:31 -0500 Subject: [PATCH 3/4] Simplify URL validation per reviewer comments --- .../kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt b/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt index 17e06c0..74d5c9e 100644 --- a/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt +++ b/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt @@ -62,13 +62,8 @@ class PlayCommand( } fun checkValidUrl(url: String): Boolean { - return try { - URL(url).toURI() - true - } - catch (e: Exception) { - false - } + return url.startsWith("http://") + || url.startsWith("https://"); } inner class Loader( From f379beb2d92c14e04212413370841a50b28670c0 Mon Sep 17 00:00:00 2001 From: James O'Cull Date: Sat, 12 Mar 2022 20:01:00 -0500 Subject: [PATCH 4/4] Simplify and clean up search handling code --- .../dev/arbjerg/ukulele/command/PlayCommand.kt | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt b/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt index 74d5c9e..e66e1df 100644 --- a/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt +++ b/src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt @@ -12,8 +12,6 @@ import dev.arbjerg.ukulele.features.HelpContext import dev.arbjerg.ukulele.jda.Command import dev.arbjerg.ukulele.jda.CommandContext import net.dv8tion.jda.api.Permission -import java.net.URL -import java.lang.Exception import org.springframework.stereotype.Component @Component @@ -26,15 +24,12 @@ class PlayCommand( if (!ensureVoiceChannel()) return var identifier = argumentText - players.get(guild, guildProperties).lastChannel = channel - - if (checkValidUrl(identifier)) { - apm.loadItem(identifier, Loader(this, player, identifier)) - } - else { + if (!checkValidUrl(identifier)) { identifier = "ytsearch:$identifier" - apm.loadItem(identifier, Loader(this, player, identifier)) } + + players.get(guild, guildProperties).lastChannel = channel + apm.loadItem(identifier, Loader(this, player, identifier)) } fun CommandContext.ensureVoiceChannel(): Boolean { @@ -63,7 +58,7 @@ class PlayCommand( fun checkValidUrl(url: String): Boolean { return url.startsWith("http://") - || url.startsWith("https://"); + || url.startsWith("https://") } inner class Loader(