如何让 discord.py 的机器人在我的语音聊天中播放音乐?

时间:2021-07-09 08:58:38

标签: python discord discord.py bots

我安装了 youtube 模块。当我说 !play_song Like a g6 时,它出现了错误,因为它在语音聊天中没有连接,但它是。

@bot.command(name='play_song', help='To play song')
async def play(ctx,url):
    try :
        server = ctx.message.guild
        voice_channel = server.voice_client

        async with ctx.typing():
            filename = await YTDLSource.from_url(url, loop=bot.loop)
            voice_channel.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source=filename))
        await ctx.send('**Now playing:** {}'.format(filename))
    except:
        await ctx.send("The bot is not connected to a voice channel.")

1 个答案:

答案 0 :(得分:1)

我也不知道如何解决这个问题,但是如果您需要来自 yt 的播放音乐命令,您可以在此处使用我的代码,只需更改说明和命令名称即可。

"parameters": [
  {
      "in": "body",
      "name": "file",
      "description": "file",
      "required": false,
      "schema": {
          "type": "array",
          "items": {
              "type": "file"
          }
      }
  },
  ...
]

此命令将使用yt_dl直接从youtube下载音乐,然后将下载的音乐存储在您的bot目录中,下载完成后,您的bot会将下载的文件转换为mp3来处理歌曲,稍等片刻,并自动播放。此命令只能播放一首歌曲,您需要断开机器人与 vc 的连接才能播放另一首音乐。