Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Discord.py:如何在play命令上实现ytsearch?_Python_Discord_Discord.py_Bots_Youtube Dl - Fatal编程技术网

Python Discord.py:如何在play命令上实现ytsearch?

Python Discord.py:如何在play命令上实现ytsearch?,python,discord,discord.py,bots,youtube-dl,Python,Discord,Discord.py,Bots,Youtube Dl,嗨,伙计们,我创建了这个音乐机器人,我想在播放按钮上实现ytsearch,就像机器人“Rythm”。目前,它只能通过url播放音乐,但我希望我的机器人能够使用相同的命令从url和关键字(比如!play never Toll Gill you)播放音乐,但我不知道如何做到这一点。你们能帮帮我吗 musics= {} ytdl = youtube_dl.YoutubeDL() class Video: def __init__(self, link): video = yt

嗨,伙计们,我创建了这个音乐机器人,我想在播放按钮上实现ytsearch,就像机器人“Rythm”。目前,它只能通过url播放音乐,但我希望我的机器人能够使用相同的命令从url和关键字(比如!play never Toll Gill you)播放音乐,但我不知道如何做到这一点。你们能帮帮我吗

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)
编辑: 因此,在triying添加ytdl_选项后,它会给我这个错误,即使没有--noplaylist,它也会给我同样的错误(我triying要播放的音乐是眩目的灯光)


可能想看看。将
资源
更改为
搜索
,然后单击
列表(按关键字)
。您将在上面获得示例代码


要使其正常工作,首先需要执行此操作。

创建
YoutubeDL
对象时,可以通过设置
default\u search
选项来指定其默认搜索行为

引自(我的):

将此前缀用于非限定URL。例如,“gvsearch2:”为youtube dl“大苹果”从谷歌视频下载两个视频使用值“auto”让youtube dl猜测(“auto_warning”在猜测时发出警告)。“错误”只是抛出一个错误。默认值“fixup_error”修复损坏的URL,但如果不可能进行搜索,则会发出错误

ytdl_选项={'default_search':'auto'}
ytdl=youtube_dl.YoutubeDL(ytdl_选项)
您需要处理完成搜索的情况,因为返回的格式与提供有效URL时不同

导入youtube\u dl
ytdl_选项={
'noplaylist':True,
“默认搜索”:“自动”
}
ytdl=youtube_dl.YoutubeDL(ytdl_选项)
链接='测试搜索'
视频=ytdl.提取信息(链接,下载=False)
如果视频中有“条目”:
视频格式=视频['entries'][0][“格式”][0]
视频中的elif“格式”:
视频格式=视频[“格式”][0]
url=视频[“网页url”]
流url=视频格式[“url”]

我尝试了这个,但它给了我一个关键错误:“格式”。我该怎么办?你能告诉我你在尝试什么以及你得到的确切错误吗?我用我的错误编辑了我的帖子,希望你能帮助我。
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'