【问题标题】:(Python) Discord bot disconnect from a voice chat(Python) Discord 机器人与语音聊天断开连接
【发布时间】:2017-11-20 14:32:32
【问题描述】:

我需要知道如何让 Discord 机器人与语音频道断开连接。目前这是我必须加入语音频道的代码

@client.command(pass_context=True)
async def joinvoice(ctx):
    #"""Joins your voice channel"""
    author = ctx.message.author
    voice_channel = author.voice_channel
    await client.join_voice_channel(voice_channel)

我需要代码来断开与语音通道的连接

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您需要从await client.join_voice_channel(voice_channel) 返回的语音客户端对象。这个对象有一个方法disconnect(),它允许你这样做。客户端还有一个属性voice_clients,它返回所有已连接语音客户端的可迭代对象,如at the docs 所示。考虑到这一点,我们可以添加一个名为 leavevoice 的命令(或任何你想调用的命令)。

    @client.command(pass_context=True)
    async def joinvoice(ctx):
        #"""Joins your voice channel"""
        author = ctx.message.author
        voice_channel = author.voice_channel
        vc = await client.join_voice_channel(voice_channel)
    
    @client.command(pass_context = True)
    async def leavevoice(ctx):
        for x in client.voice_clients:
            if(x.server == ctx.message.server):
                return await x.disconnect()
    
        return await client.say("I am not connected to any voice channel on this server!")
    

    leavevoice 命令中,我们遍历了机器人连接到的所有语音客户端,以便找到该服务器的语音通道。找到后,断开连接。如果没有,则机器人未连接到该服务器中的语音通道。

    【讨论】:

    • 当我使用你的代码加入它时,它会起作用,然后当我输入 s.leavevoice 时它会输出。 (我没有连接到此服务器上的任何语音通道!)即使它在我的语音通道中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    相关资源
    最近更新 更多