我需要知道如何让 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/