python - (Python) Discord bot 从语音聊天中断开连接

标签 python discord discord.py

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

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

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

最佳答案

您需要从 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 命令中,我们遍历机器人连接的所有语音客户端,以便找到该服务器的语音 channel 。一旦发现,断开连接。如果没有,则机器人未连接到该服务器中的语音 channel 。

关于python - (Python) Discord bot 从语音聊天中断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44612538/

相关文章:

python - 从 python 脚本中获得 bash 自动完成功能

python - 在 numpy 数组中插入 NaN 值

python - 无法解决: TypeError: 'NoneType' object is not iterable

Discord 机器人在循环后离线

python - 创建一个记录主持人命令的记录器,类似于 'modlogs'

python - discord py音乐机器人停止播放

打包后 Python 子模块不可见

javascript - discord.js - 机器人超时

javascript - 为什么值为空?

python - Django继承: how to have one method for all subclasses?