如何让机器人加入语音频道 discord.py

Leg*_*ing 3 python python-3.x discord.py discord.py-rewrite

我正在使用 discord.py 创建音乐机器人,但我无法将机器人连接到语音通道。我使用 Cog 将音乐功能与其他功能区分开来。

@commands.command()
async def join_voice(self, ctx):
    channel = ctx.author.voice.channel
    print(channel.id)
    await self.client.VoiceChannel.connect()
Run Code Online (Sandbox Code Playgroud)

但我收到错误: AttributeError: 'NoneType' object has no attribute 'channel'

我已经浏览了所有文档和此处的所有类似问题,但仍然没有解决方案!

有人可以帮忙吗?

Dig*_*gy. 8

你真的很接近!你唯一需要改变的是:

@commands.command()
async def join_voice(self, ctx):
    connected = ctx.author.voice
    if connected:
        await connected.channel.connect() #  Use the channel instance you put into a variable
Run Code Online (Sandbox Code Playgroud)

您所做的是获取 VoiceChannel 类对象,而不是用户连接到的实际 VoiceChannel 实例。这就是您的错误所在,因为它试图找到一个不存在的语音通道。

很高兴看到进步,继续加油!