不和谐机器人加入/离开

2024-04-24 22:20:51 发布

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

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

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


Tags: inclientsendasyncifdefbotchannel
1条回答
网友
1楼 · 发布于 2024-04-24 22:20:51

这应该起作用:

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

相关问题 更多 >