Discord.py重写从youtube播放音频到语音聊天

2024-04-27 17:29:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图得到一个机器人,可以加入一个语音聊天命令,然后播放一些从youtube视频提取的音频。我不知道如何做到这一点,我得到的所有代码似乎都不起作用。有人知道怎么做吗

@client.command() async def play(ctx):
   channel = ctx.message.author.voice.channel
   voice_client = await channel.connect()

   opts = {'format': 'bestaudio'}
   FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 - 
   reconnect_delay_max 5', 'options': '-vn'}

   with youtube_dl.YoutubeDL(opts) as ydl:
      song_info = ydl.extract_info('video', download=False)
      URL = song_info['formats'][0]['url']
   
   voice_client.play(FFmpegPCMAudio(URL, **FFMPEG_OPTIONS))

Tags: infoclienturlplaysongyoutubechannelffmpeg