嗨,伙计们,我创建了这个音乐机器人,我想实现游戏按钮上的ytsearch,就像bot "Rythm“一样。目前,它只能用url播放音乐,但我希望我的机器人能够使用相同的命令从url和关键字(比如!play永远不会放弃你)播放,但是我不知道如何做到这一点。你们能帮帮我吗?
musics= {}
ytdl = youtube_dl.YoutubeDL()
class Video:
def __init__(self, link):
video = ytdl.extract_info(link, download = False)
video_format = video["formats"][0]
self.url =video["webpage_url"]
self.stream_url =video_format["url"]
def play_song(client, queue, song):
source= discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(song.stream_url
, before_options = "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"))
def next(_):
if len(queue)>0:
new_song= queue[0]
del queue[0]
play_song(client, queue, new_song)
else:
asyncio.run_coroutine_threadsafe(client.disconnect(), bot.loop)
client.play(source, after=next)
@bot.command()
async def play (ctx, url):
client = ctx.guild.voice_client
if client and client.channel:
video = Video(url)
musics[ctx.guild].append(video)
else:
channel = ctx.author.voice.channel
video = Video(url)
musics[ctx.guild] = []
client = await channel.connect()
await ctx.send(f"Playing : {video.url}")
play_song(client, musics[ctx.guild], video)编辑:所以在尝试添加ytdl_options之后,它会给我带来这个错误,即使没有
ytdl_options = {
'noplaylist': True,
'default_search': 'auto'
}
ytdl = youtube_dl.YoutubeDL(ytdl_options)[download] Downloading playlist: blinding
[youtube:search] query "blinding": Downloading page 1
[youtube:search] playlist blinding: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] 4NRXx6U8ABQ: Downloading webpage
[youtube] Downloading just video 4NRXx6U8ABQ because of --no-playlist
[download] Finished downloading playlist: blinding
Ignoring exception in command play:
Traceback (most recent call last):
File "C:\Users\pietr\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\pietr\Desktop\Discord bot\Discord bot", line 152, in play
video = Video(url)
File "c:\Users\pietr\Desktop\Discord bot\Discord bot", line 93, in __init__
video_format = video["formats"][0]
KeyError: 'formats'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\pietr\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\pietr\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\pietr\AppData\Local\Programs\Python\Python39\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'发布于 2021-04-20 08:25:30
创建YoutubeDL对象时,可以通过设置default_search选项来指定其默认搜索行为。
引用自这里 (强调地雷):
对不合格的URL使用此前缀。例如,"gvsearch2:“从youtube-dl”大苹果“的google视频中下载了两段视频。使用值"auto“让youtube-dl猜测 ("auto_warning”在猜测时发出警告)。“错误”只是抛出一个错误。默认值"fixup_error“修复损坏的URL,但如果不可能进行搜索,则会发出错误。
ytdl_options = {'default_search': 'auto'}
ytdl = youtube_dl.YoutubeDL(ytdl_options)您需要处理完成搜索的情况,因为返回的格式与提供有效URL时的格式不同。
import youtube_dl
ytdl_options = {
'noplaylist': True,
'default_search': 'auto'
}
ytdl = youtube_dl.YoutubeDL(ytdl_options)
link = 'test search'
video = ytdl.extract_info(link, download = False)
if 'entries' in video:
video_format = video['entries'][0]["formats"][0]
elif 'formats' in video:
video_format = video["formats"][0]
url = video["webpage_url"]
stream_url = video_format["url"]发布于 2021-04-20 01:17:55
可能会想看看Youtube API。将resource更改为search,然后单击list (by keyword)。你会得到它的样本代码。
为了使它正常工作,您首先需要获得授权凭证。
https://stackoverflow.com/questions/67169349
复制相似问题