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

How to make a bot join voice channels discord.py

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

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

但是我得到了错误: AttributeError: 'NoneType' object has no attribute 'channel'

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

有人可以帮忙吗?

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

@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

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

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

我不是 discord.py 专家,但可以尝试其中的一些 link:

How a discord bot can join a voice channel in discord rewrite? https://github.com/Rapptz/discord.py/issues/585

我认为第 3 个 link 更适合你。