(Python) Discord 机器人与语音聊天断开连接
(Python) Discord bot disconnect from a voice chat
我需要知道如何使 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)
我需要代码来断开与语音通道的连接
您需要从 await client.join_voice_channel(voice_channel)
返回的语音客户端对象。这个对象有一个方法 disconnect()
可以让你做到这一点。客户端还有一个属性 voice_clients
,其中 returns 是所有已连接语音客户端的可迭代项 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
命令中,我们遍历了机器人连接的所有语音客户端,以便找到该服务器的语音通道。一旦发现,断开连接。如果 none,则机器人未连接到该服务器中的语音通道。
我需要知道如何使 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)
我需要代码来断开与语音通道的连接
您需要从 await client.join_voice_channel(voice_channel)
返回的语音客户端对象。这个对象有一个方法 disconnect()
可以让你做到这一点。客户端还有一个属性 voice_clients
,其中 returns 是所有已连接语音客户端的可迭代项 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
命令中,我们遍历了机器人连接的所有语音客户端,以便找到该服务器的语音通道。一旦发现,断开连接。如果 none,则机器人未连接到该服务器中的语音通道。