Discord.py 机器人离开语音频道

Discord.py bot leaving voice channel

我一直在制作一个不和谐的机器人,它进入 vc 播放音频然后离开,但我似乎无法让离开的部分工作。这是我的代码:

# Discord specific import
import discord
from discord.ext import commands
import asyncio

Client = discord.Client()
client = commands.Bot(command_prefix="..")

@client.command(pass_context=True)
async def dan(ctx):
    author = ctx.message.author
    channel = author.voice_channel
    vc = await client.join_voice_channel(channel)
    player = vc.create_ffmpeg_player('dan.mp3', after=lambda: print('done'))
    player.start()
    player.disconnect()

client.run('token')

我没有收到任何错误,但同时机器人没有与 vc 断开连接,我尝试将 'player' 更改为 'client','Client' 和 'client.voice'

按照 here in the docs, since Client.join_voice_channel(channel) creates a VoiceClient 所述尝试 vc.disconnect()。另外我建议不要有像

这样的冗余变量

author = ctx.message.author

channel = author.voice_channel

什么时候可以vc = await client.join_voice_channel(ctx.message.author.voice_channel)

此外,另一个冗余变量是 Client = discord.Client(),因为您不会在任何地方使用它,而是使用 commands.Bot 实例,所以最好删除它。

player.disconnect()是一个协程,你应该在它之前使用await关键字。

await player.disconnect()

我的问题是:

  1. 我需要使用:await vc.disconnect()
  2. 我的websockets版本太高,需要低于v4.0.0

希望这可以帮助人们解决我的问题