(Python)从语音通道断开不和谐bot

2024-04-26 12:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要知道如何使一个不和谐的机器人从语音频道断开。目前这是我必须加入语音频道的代码

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

我需要密码从语音频道上断开


Tags: 代码clienttrueasyncdefcontextchannel机器人
1条回答
网友
1楼 · 发布于 2024-04-26 12:27:22

您需要从await client.join_voice_channel(voice_channel)返回的语音客户端对象。这个对象有一个方法disconnect(),它允许您这样做。客户机还有一个属性voice_clients,该属性返回所有连接的语音客户机中的一个iterable,如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命令中,我们在bot连接的所有语音客户端上循环,以便找到该服务器的语音通道。一旦找到,断开连接。如果没有,则bot未连接到该服务器中的语音通道。

相关问题 更多 >