Discord Bot 加入/离开

时间:2021-02-03 16:35:35

标签: python discord bots

@Bot.command()

async def join(ctx):

    if (ctx.author.voice):

        channel = ctx.author.voice.channel

        await channel.connect()

        await ctx.send('Bot joined')

    else:
        await ctx.send("You must be in a voice channel first so I can join it.")
@Bot.command()

async def leave(

        ctx):
    if (ctx.voice_client):
        await ctx.guild.voice_client.disconnect()
        await ctx.send('Bot left')
    else:
        await ctx.send("I'm not in a voice channel, use the join command to make me join")

我正在为我的机器人使用此代码。机器人可以加入但不能离开,它会在发送命令时忽略离开。

1 个答案:

答案 0 :(得分:0)

这应该有效:

@Bot.command(description="joins a voice channel")
    async def join(ctx):
        if ctx.author.voice is None or ctx.author.voice.channel is None:
            return await ctx.send('You need to be in a voice channel to use this command!')

        voice_channel = ctx.author.voice.channel
        if ctx.voice_client is None:
            vc = await voice_channel.connect()
        else:
            await ctx.voice_client.move_to(voice_channel)
            vc = ctx.voice_client

@Bot.command(description="stops and disconnects the bot from voice")
    async def leave(ctx):
        if ctx.voice_client is None:
            await ctx.send("I'm not in a voice channel, use the join command to make me join")
        else:
            await ctx.voice_client.disconnect()
            await ctx.send('Bot left')