如何使用 youtubedl 的搜索功能而不是 url 让我的不和谐机器人播放音乐? (Python)

Posted

技术标签:

【中文标题】如何使用 youtubedl 的搜索功能而不是 url 让我的不和谐机器人播放音乐? (Python)【英文标题】:How do I make my discord bot play music by using youtubedl's search function instead of url? (Python) 【发布时间】:2021-11-17 13:07:30 【问题描述】:

我希望它搜索视频并播放,我该如何更改以下代码来实现呢?每次使用 ytdl 中的 ytsearch 函数时,我发现它只搜索标题的第一个单词并下载它,但是它稍后会导致错误并且什么也不做。

@commands.command()
    async def play(self, ctx, url):
        if ctx.author.voice is None:
            await ctx.send("You are not in a voice channel!")
        voice_channel = ctx.author.voice.channel
        if ctx.voice_client is None:
            await voice_channel.connect()
        else:
            await ctx.voice_client.move_to(voice_channel)

        ctx.voice_client.stop()
        FFMPEG_OPTIONS = 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'
        YDL_OPTIONS = 'format':"bestaudio", 'default_search':"ytsearch"
        vc = ctx.voice_client

        with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
            info = ydl.extract_info(url, download=False)
            if 'entries' in info:
              url2 = info["entries"][0]["formats"][0]
            elif 'formats' in info:
              url2 = info["formats"][0]['url']
            source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
            vc.play(source)

这是错误信息:

Ignoring exception in command play:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/runner/HandmadeLivelyLines/music.py", line 44, in play
    source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 387, in from_probe
    return cls(source, bitrate=bitrate, codec=codec, **kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 324, in __init__
    super().__init__(source, executable=executable, args=args, **subprocess_kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 138, in __init__
    self._process = self._spawn_process(args, **kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 144, in _spawn_process
    process = subprocess.Popen(args, creationflags=CREATE_NO_WINDOW, **subprocess_kwargs)
  File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1639, in _execute_child
    self.pid = _posixsubprocess.fork_exec(
TypeError: expected str, bytes or os.PathLike object, not dict

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: expected str, bytes or os.PathLike object, not dict

谢谢。

【问题讨论】:

url2 = info["entries"][0]["formats"][0]['url'] 而不是 url2 = info["entries"][0]["formats"][0] 有效!谢谢! 但是,它似乎只搜索标题的第一个单词,有什么方法可以让它搜索整个字符串而不是第一个单词? async def play(self, ctx, *, url): 非常感谢,现在可以正常使用了! 【参考方案1】:

解决 URL 问题:

url2 = info["entries"][0]["formats"][0]['url']

以及搜索查询中只包含一个单词的问题:

async def play(self, ctx, *, url):

【讨论】:

以上是关于如何使用 youtubedl 的搜索功能而不是 url 让我的不和谐机器人播放音乐? (Python)的主要内容,如果未能解决你的问题,请参考以下文章

请问 ncbi搜索到的mRNA序列是cDNA?是反义or正义?为啥mRNA序列中是T而不是U

Pyspark reduce 将接受某些功能,而不是其他功能

如何在 JPA 存储库中准确搜索数字而不是使用 LIKE?

如何在没有环视的情况下在 RegEx 中捕获“搜索”而不是“/搜索”?

我们如何使用 NSPredicate 搜索所有记录而不是设置 fetchController nil?

如何使用 ag-grid 自定义过滤器组件构建查找搜索功能