Why is search not working with "ytsearch:" in lavaplayer, JDA

200 Views Asked by At

I am creating a music bot for discord in Java, lavaplayer. I'm trying to find a track by title using "ytsearch:" Any input in /play will return noMatches. At the same time, downloading music from the link works fine. I am using the latest JDA (5.0.0-beta.9) and lavaplayer (1.3.77). I tried to copy the code from the manuals, but what works for the authors with a similar version does not work for me.

My code:

  • /play
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
     if (event.getName().equals("play")) {
                event.getGuild().getAudioManager().openAudioConnection(event.getMember().getVoiceState().getChannel());

                String name = event.getOption("name").getAsString();
                try {
                    new URI(name);
                } catch (URISyntaxException e) {
                    name = "ytsearch:" + name;
                }
                PlayerManager playerManager = PlayerManager.get();
                event.reply("Playing").setEphemeral(true).submit();
                playerManager.play(event.getGuild(), name);
        }
}
1

There are 1 best solutions below

0
Николай On BEST ANSWER

The problem is that the try block never threw the expected error. Working code:

 public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
     if (event.getName().equals("play")) {               
           event.getGuild().getAudioManager().openAudioConnection(event.getMember().getVoiceState().getChannel());
           String name = event.getOption("name").getAsString();
           try {
               new URL(name);
           } catch (MalformedURLException e) {
               name = "ytsearch:" + name;
           }
           PlayerManager playerManager = PlayerManager.get();
           playerManager.play(event.getGuild(), name);
    }