使用Python中的Discord机器人加入+离开

时间:2020-06-14 11:06:25

标签: python discord

我已经执行了这两个命令,一个用于离开该机器人当前连接的通道,另一个用于加入命令发送者的通道。

问题在于它们都不起作用。我该如何解决?

@bot.command(pass_context=True)
async def leave(ctx):
    server = ctx.message.guild.voice_client
    await server.disconnect()

@bot.command(pass_context = True)
async def join(ctx):
    channel = ctx.message.author.voice.voice_channel
    await bot.join_voice_channel(channel)

1 个答案:

答案 0 :(得分:0)

似乎您正在使用旧文档或教程以及一些新文档。

一些changes from 0.16.x to rewrite

重写您的代码-在某些条件下检查作者是否在语音通道中

@bot.command()
async def leave(ctx):
    if ctx.guild.voice_client.is_connected(): # Checking that they're in a vc
        await ctx.guild.voice_client.disconnect()
    else:
        await ctx.send("Sorry, I'm not connected to a voice channel at the moment.")

@bot.command()
async def join(ctx):
    if ctx.author.voice:
        await ctx.author.voice.channel.connect() # This will error if the bot doesn't have sufficient permissions
    else:
        await ctx.send("You're not connected to a channel at the moment.")

参考: