亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果機器人單獨存在于語音通道中,則使機器人斷開連接discord.py

如果機器人單獨存在于語音通道中,則使機器人斷開連接discord.py

呼喚遠方 2023-07-05 16:24:33
如果機器人單獨在語音通道上,有什么方法可以斷開機器人的連接嗎?每次有人離開 vc 時是否會觸發任何事件或類似的事件來完成這項工作?
查看完整描述

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())

可能有更好的答案,但這也可以解決您的問題。


查看完整回答
反對 回復 2023-07-05
?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

對于可能遇到同樣問題并想要答案的人


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


查看完整回答
反對 回復 2023-07-05
  • 2 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號