2 回答
TA贡献1829条经验 获得超7个赞
我不太了解语音频道,但您可以使用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())
可能有更好的答案,但这也可以解决您的问题。
TA贡献1725条经验 获得超7个赞
对于可能遇到同样问题并想要答案的人
GUILD_VC_TIMER = {}
# this event runs when user leave / join / defen / mute
@bot.event
async def on_voice_state_update(member, before, after):
# if event is triggered by the bot? return
if member.id == bot.user.id:
return
# when before.channel != None that means user has left a channel
if before.channel != None:
voice = discord.utils.get(bot.voice_clients , channel__guild__id = before.channel.guild.id)
# 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
if voice == None:
return
# if VC left by the user is not equal to the VC that bot is in? then return
if voice.channel.id != before.channel.id:
return
# if VC has only 1 member (including the bot)
if len(voice.channel.members) <= 1:
GUILD_VC_TIMER[before.channel.guild.id] = 0
while True:
print("Time" , str(GUILD_VC_TIMER[before.channel.guild.id]) , "Total Members" , str(len(voice.channel.members)))
await asyncio.sleep(1)
GUILD_VC_TIMER[before.channel.guild.id] += 1
# if vc has more than 1 member or bot is already disconnectd ? break
if len(voice.channel.members) >= 2 or not voice.is_connected():
break
# if bot has been alone in the VC for more than 60 seconds ? disconnect
if GUILD_VC_TIMER[before.channel.guild.id] >= 60:
await voice.disconnect()
return
添加回答
举报