[英]Make bot disconnect from all voice channels in all guilds? Discord.py
因此,我制作了一個 discord 機器人,當它發現自己在通話中時,它就會斷開與語音通道的連接。 這就是我的做法:(注意,我使用的是齒輪)
@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
這很好用。 但是,如果我在重新啟動代碼時忘記離開通話,我會在機器人重新啟動時留在頻道中(如預期的那樣)。 當我斷開自己的通話時,問題就來了。 機器人停留在其中,僅輸出此錯誤:( (line 37, if voiceClient.channel:= None:) AttributeError: 'NoneType' object has no attribute 'channel'
也許這是因為它不知道它還在頻道中? 我很困惑...謝謝你的時間。 :-)
您可以更輕松地查詢語音通道。 此外,您需要以不同的方式請求事件,即使您的("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.members
的len
。 您可以在Discord 開發人員門戶中執行此操作,並且需要將意圖“導入”到您的代碼中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.