机器人在独处时不会离开语音通道



我有一个为我和朋友播放音乐的个人音乐机器人,我只有播放和离开命令,这两个命令都能完美工作,但我想知道是否可以对机器人进行一点升级,使其在单独使用时自动离开语音通道。在阅读了一些文档后,我以为我得到了它,但我的这段代码根本不起作用,这几乎就像python出于某种原因忽略了它,所以我想我在这里错过了一些东西。。。所以我想知道我的代码是否有原因:

@client.event
async def on_voice_state_update(member):
voice_state = member.guild.voice_client
if len(voice_state.channel.members) == 1:
await voice_state.disconnect()

不会工作,我没有收到任何错误消息,实际上什么都没发生。这一切都应该在这里吗?

此事件缺少一些参数。只需添加beforeafter,就可以了。

完整事件可能是:

@client.event
async def on_voice_state_update(member, before, after):
voice_state = member.guild.voice_client
if voice_state is not None and len(voice_state.channel.members) == 1:
# If the bot is connected to a channel and the bot is the only one in the channel
await voice_state.disconnect() # Disconnect the bot from the channel

你可以在这里的文档中看到更多:

  • on_voice_state_update

最新更新