【问题标题】:Make bot disconnect from all voice channels in all guilds? Discord.py让机器人与所有公会的所有语音频道断开连接?不和谐.py
【发布时间】:2021-05-24 18:54:13
【问题描述】:

所以我制作了一个不和谐机器人,当它发现自己在通话中时,它就会断开与语音频道的连接。我就是这样做的:(注意,我使用的是齿轮)

@commands.Cog.listener("on_voice_state_update")
  async def voiceStateUpdate(self, member, before, after):
    voiceClient = discord.utils.get(self.bot.voice_clients, guild = member.guild) # Bot "voice_client"

    if voiceClient.channel != None:
      if len(voiceClient.channel.members) == 1:
        await voiceClient.disconnect() # Disconnect

这很好用。但是,如果我在 restart 我的代码时忘记离开通话,我会在我的机器人重新启动时留在频道中(如预期的那样)。当我断开自己的通话时,问题就来了。机器人停留在里面,只输出这个错误:(line 37, if voiceClient.channel != None:) AttributeError: 'NoneType' object has no attribute 'channel'

也许这是因为它不知道它还在频道中? 我很困惑...谢谢你的时间。 :-)

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

    您可以更轻松地查询语音频道。此外,即使您的("on_voice_state_update") 是正确的,您也需要以不同的方式请求事件。

    如果机器人重新启动,它会立即或在几秒/分钟后离开所有语音通道,因此您无需担心。

    看看以下事件:

    @commands.Cog.listener()
    async def on_voice_state_update(self, member, before, after):
        voice_state = member.guild.voice_client # Get the member in the channel
        if voice_state is not None and len(voice_state.channel.members) == 1: # If bot is alone
            await voice_state.disconnect() # Disconnect
    

    请注意,您必须启用members Intent 才能获得channel.memberslen。您可以在 Discord Developer Portal 中执行此操作,并且需要将 Intent(s)“导入”到您的代码中。

    【讨论】:

    • 好的,我只需要等待机器人离开。也感谢您的提示! :)
    • @DoudGeorges 是的,这是正确的。您当然可以编写一个函数,让它在您重新启动代码时离开语音通道,但我不知道这是否有任何好处。如果重启bot/code,暂时无法在语音通道中与bot交互。
    猜你喜欢
    • 2020-09-01
    • 1970-01-01
    • 2021-04-13
    • 2021-09-30
    • 1970-01-01
    • 2020-10-15
    • 2019-06-18
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多