如果机器人单独存在于语音通道中,则使机器人断开连接discord.py

如果机器人单独在语音通道上,有什么方法可以断开机器人的连接吗?每次有人离开 vc 时是否会触发任何事件或类似的事件来完成这项工作?



呼唤远方
浏览 98回答 2
2回答

千巷猫影

我不太了解语音频道,但您可以使用VoiceChannel.members检查成员计数并进行如下任务循环async def check():    # define the voice channel    member_count = len(voice_channel.members)    if member_count == 1:        # leave the channel    await asyncio.sleep(30)client.loop.create_task(check())可能有更好的答案,但这也可以解决您的问题。

qq_遁去的一_1

对于可能遇到同样问题并想要答案的人GUILD_VC_TIMER = {}# this event runs when user leave / join / defen / mute&nbsp;@bot.eventasync def on_voice_state_update(member, before, after):&nbsp; &nbsp; # if event is triggered by the bot? return&nbsp; &nbsp; if member.id == bot.user.id:&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; # when before.channel != None that means user has left a channel&nbsp; &nbsp; if before.channel != None:&nbsp; &nbsp; &nbsp; &nbsp; voice = discord.utils.get(bot.voice_clients , channel__guild__id = before.channel.guild.id)&nbsp; &nbsp; &nbsp; &nbsp; # voice is voiceClient and if it's none? that means the bot is not in an y VC of the Guild that triggerd this event&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if voice == None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; # if VC left by the user is not equal to the VC that bot is in? then return&nbsp; &nbsp; &nbsp; &nbsp; if voice.channel.id != before.channel.id:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; # if VC has only 1 member (including the bot)&nbsp; &nbsp; &nbsp; &nbsp; if len(voice.channel.members) <= 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GUILD_VC_TIMER[before.channel.guild.id] = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Time" , str(GUILD_VC_TIMER[before.channel.guild.id]) , "Total Members" , str(len(voice.channel.members)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await asyncio.sleep(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GUILD_VC_TIMER[before.channel.guild.id] += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # if vc has more than 1 member or bot is already disconnectd ? break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(voice.channel.members) >= 2 or not voice.is_connected():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # if bot has been alone in the VC for more than 60 seconds ? disconnect&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if GUILD_VC_TIMER[before.channel.guild.id] >= 60:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await voice.disconnect()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python