为了账号安全,请及时绑定邮箱和手机立即绑定

Python discord bot 离开语音频道

Python discord bot 离开语音频道

当年话下 2022-10-06 15:51:31
我已经让机器人加入我的语音服务器,如下所示 if message.content.startswith("?join"):    channel = message.author.voice.channel    await channel.connect()    await message.channel.send('bot joined')但我不能让机器人离开频道..我该如何编写代码来做到这一点?和有什么区别@bot.event async def on_message(message): if message.content.startswith('~'):和@bot.command()async def ~(ctx):
查看完整描述

2 回答

?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

您可以通过两种方式执行这两个命令(加入和离开频道命令),一种是使用 on_message,另一种是使用@bot.commands。最好使用 bot.command 而不是 on_message 作为命令,因为 bot.commands 具有更多功能,而且我认为它是为命令而构建的,它的速度很快。因此,我将使用 bot.command 重写您的两个命令,并在此处使用 on_message 以防您不想使用 bot.command。根据您的消息,我假设?是您的前缀


使用on_message

@bot.event

async def on_message(message):

    if (message.content.startswith('?join')):

        if (message.author.voice): # If the person is in a channel

            channel = message.author.voice.channel

            await channel.connect()

            await message.channel.send('Bot joined')

        else: #But is (s)he isn't in a voice channel

            await message.channel.send("You must be in a voice channel first so I can join it.")


    elif message.content.startswith('?~'): # Saying ?~ will make bot leave channel

        if (message.guild.voice_client): # If the bot is in a voice channel 

            await message.guild.voice_client.disconnect() # Leave the channel

            await message.channel.send('Bot left')

        else: # But if it isn't

            await message.channel.send("I'm not in a voice channel, use the join command to make me join")

    await bot.process_commands(message) # Always put this at the bottom of on_message to make commands work properly

使用bot.command

@bot.command()

async def join(ctx):

    if (ctx.author.voice): # If the person is in a channel

        channel = ctx.author.voice.channel

        await channel.connect()

        await ctx.send('Bot joined')

    else: #But is (s)he isn't in a voice channel

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


@bot.command(name = ["~"])

async def leave(ctx): # Note: ?leave won't work, only ?~ will work unless you change  `name = ["~"]` to `aliases = ["~"]` so both can work.

    if (ctx.voice_client): # If the bot is in a voice channel 

        await ctx.guild.voice_client.disconnect() # Leave the channel

        await ctx.send('Bot left')

    else: # But if it isn't

        await ctx.send("I'm not in a voice channel, use the join command to make me join")




查看完整回答
反对 回复 2022-10-06
?
守候你守候我

TA贡献1802条经验 获得超10个赞

保存通道连接,以便稍后断开连接。


voice = None

...

    if message.content.startswith("?join"):

        channel = message.author.voice.channel

        global voice = await channel.connect()

        await message.channel.send('bot joined')

    elif message.content.startswith("?join"):

        await self.voice.disconnect()

无论如何,尝试使用discord.ext.commands扩展。它使所有涉及命令的事情变得更容易。我还建议使用 cogs ( example ),因为您可以拥有一个包含所有与语音相关的类,并且您不需要全局变量。


查看完整回答
反对 回复 2022-10-06
  • 2 回答
  • 0 关注
  • 108 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信