为什么在 lavaplayer、JDA 中使用 “ytsearch:” 时搜索不起作用?

huangapple go评论79阅读模式
英文:

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

问题

我正在使用Java和Lavaplayer为Discord创建一个音乐机器人。我尝试使用"ytsearch:"来根据标题查找音轨。在/play中的任何输入都会返回noMatches。与此同时,从链接下载音乐正常工作。我正在使用最新的JDA(5.0.0-beta.9)和Lavaplayer(1.3.77)。

我的代码如下:

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);
    }
}

请注意,这只是代码的一部分,它用于处理/play命令。如果您需要更多帮助,请提供更多上下文或问题的详细信息。

英文:

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

得分: 0

抱歉,你提供的代码部分已经是英文,无需翻译。如果你有其他需要翻译的文本,请随时提供。

英文:

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);
    }

huangapple
  • 本文由 发表于 2023年5月25日 01:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定