簡體   English   中英

Discord.py 最新的 VoiceChannel.disconnect() 不起作用

[英]Discord.py latest VoiceChannel.disconnect() doesn't work

我一直在嘗試使用discord.py編寫一個不和諧的機器人。 VoiceChannel.connect()工作原理與文檔中所述一樣,但是VoiceChannel.disconnect()不起作用,即使它在文檔中有所說明。 我需要用來讓機器人離開語音通道。 下面是代碼。

    elif message.content == ("/bind"):
        channel = message.author.voice.channel
        vc = await channel.connect()


    elif message.content == ("/unbind"):
        channel = message.author.voice.channel
        vc = await channel.disconnect()

我得到的錯誤是:

AttributeError: 'VoiceChannel' 對象沒有屬性 'disconnect'

我也在使用 @client.event 而不是 @client.command

錯誤很明顯, VoiceChannel沒有disconnect

您正在尋找的是VoiceClient.disconnect()

您的代碼正在做的是在初始channel.connect()上創建一個VoiceClient對象並將其分配給變量vc 您可以使用await vc.disconnect()斷開與語音通道的連接。

另一種選擇是使用Client.voice_client檢查機器人是否已連接到服務器/公會上的語音通道,如果是,則將其與其斷開連接。

import discord

client = discord.Client()


@client.event
async def on_message(message):
    if message.content == "/bind":
        channel = message.author.voice.channel
        vc = await channel.connect()

    elif message.content == "/unbind":
        for vc in client.voice_clients:
            if vc.guild == message.guild:
                await vc.disconnect()


client.run('token')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM