我试图制造一个不和谐的个人音乐机器人,因为groovy和rythm被关闭了。我想还不错,但我对ytdl有问题。输入"-play“和一个url就像预期的那样工作,但我不能输入"-play‘歌曲名称’”。输入"-play示例“将给出以下内容:
[download] Downloading playlist: example
[youtube:search] query "example": Downloading page 1
[youtube:search] playlist example: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] CLXt3yh2g0s: Downloading webpage
Ignoring exception in command play:
[download] Finished downloading playlist: example
Traceback (most recent call last):
File "C:\Users\Dennis\PycharmProjects\groovy's true successor\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\Dennis\PycharmProjects\groovy's true successor\voice.py", line 53, in play
url2 = info['formats'][0]['url']
KeyError: 'formats'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Dennis\PycharmProjects\groovy's true successor\venv\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Dennis\PycharmProjects\groovy's true successor\venv\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Dennis\PycharmProjects\groovy's true successor\venv\lib\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: KeyError: 'formats'我对编码相当陌生,所以我很抱歉,如果有一些奇怪的事情要理解。
好的,所以:用url输入-play很好,但是输入带有歌曲名称的-play就不行了。它只搜索第一个单词,下载第一个搜索结果,然后“崩溃”。
例如,"-play Rick -永不放弃“,只搜索"Rick”,然后它说了一些关于KeyError的内容:'formats‘,这是我的代码:
@client.command()
async def play(ctx, url):
channel = ctx.author.voice.channel
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
pass
else:
await channel.connect()
ffmpeg_opts = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
ydl_opts = {'format': "bestaudio/best", 'default_search': 'auto'}
vc = ctx.voice_client
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
url2 = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2, **ffmpeg_opts)
vc.play(source)发布于 2022-01-09 11:03:10
#takes the entire text instead of just the first word
async def play(ctx, *,url):
#i would remove 'default_search': 'auto' and do this
ydl_opts = {'format': "bestaudio/best"}
vc = ctx.voice_client
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
url = ydl.extract_info("ytsearch:%s" % name, download = False)['entries'][0]发布于 2022-07-07 00:59:41
我遇到了这个问题,在做了几个小时的工作后,我发现"extract_info“在从搜索中提取而不是从url中提取时返回不同的字典。
通过使用一个简单的if检查来区分url和search,我解决了这个问题:
#check if it is a valid link
if ("youtube.com" in url or "youtu.be" in url):
#run the normal code after
info = ydl.extract_info(url, download=False)
url2 = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
#if it's not a valid link but a search
else:
info = ydl.extract_info(url, download=False)
url2 = info['entries'][0]['webpage_url']这个新值的"url2“在搜索部分将是对视频的链接,你应该能够把它分回与正常链接相同的东西。
https://stackoverflow.com/questions/69237607
复制相似问题