python - 如何使用 youtubedl 的搜索功能而不是 url 让我的不和谐机器人播放音乐?(Python)
问题描述
我希望它搜索视频并播放它,我该如何更改以下代码来实现呢?每次我在 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
谢谢。
解决方案
要解决 URL 问题:
url2 = info["entries"][0]["formats"][0]['url']
并且只有一个词被纳入搜索查询的问题:
async def play(self, ctx, *, url):
推荐阅读
- javascript - 如何将 uids 添加为子项?(火力基地 rtdb)
- javascript - 尝试在 Django 中学习 javascript 集成
- flutter - 无法通过命令提示符创建新的 Flutter 项目
- javascript - FB api graphql : 计算在 facebook 中与好友的消息数量
- soot - 什么是局部变量的 DU-UD Web?
- r - 向量内的向量并相互比较.R
- java - 在 Netbeans 8.2 RC 中为我的 Web 项目运行 JUnit 测试时出错说:需要在环境或系统属性中指定类名
- python-module - 运行python模块的Wifi互联网问题
- r - 将掩码变量(在“...”中)传递给 group_by() 时有条件地覆盖默认参数
- javascript - 如何使用 docker compose 更改 docker 容器内的文件夹权限?