Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added search if Play command is used without a URL #32

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/main/kotlin/dev/arbjerg/ukulele/command/PlayCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified a lot. Using exception handling as control flow is an antipattern.

val identifier = if(argumentText.startsWith("http")) argumentText else "ytsearch:$identifier"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was initially my solution, but I feel like it's a bit of a cheat and a more robust solution would cover the unlikely but still possible scenario where someone tries to play something starting with 'http'. Worth thinking about.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more robust solution would be argumentText.startsWith("http://") || argumentText.startsWith("https://")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't argue with that now 😄

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 {
Expand All @@ -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,
Expand All @@ -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
mihai-bocioroaga marked this conversation as resolved.
Show resolved Hide resolved
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() {
Expand Down