• <bdo id='LoxSR'></bdo><ul id='LoxSR'></ul>

      <small id='LoxSR'></small><noframes id='LoxSR'>

      <tfoot id='LoxSR'></tfoot><legend id='LoxSR'><style id='LoxSR'><dir id='LoxSR'><q id='LoxSR'></q></dir></style></legend>
    1. <i id='LoxSR'><tr id='LoxSR'><dt id='LoxSR'><q id='LoxSR'><span id='LoxSR'><b id='LoxSR'><form id='LoxSR'><ins id='LoxSR'></ins><ul id='LoxSR'></ul><sub id='LoxSR'></sub></form><legend id='LoxSR'></legend><bdo id='LoxSR'><pre id='LoxSR'><center id='LoxSR'></center></pre></bdo></b><th id='LoxSR'></th></span></q></dt></tr></i><div id='LoxSR'><tfoot id='LoxSR'></tfoot><dl id='LoxSR'><fieldset id='LoxSR'></fieldset></dl></div>

      1. Python discord bot 离开语音频道

        时间:2023-10-11
          • <bdo id='METZR'></bdo><ul id='METZR'></ul>
            <legend id='METZR'><style id='METZR'><dir id='METZR'><q id='METZR'></q></dir></style></legend>
              <tbody id='METZR'></tbody>
            <tfoot id='METZR'></tfoot>
                  <i id='METZR'><tr id='METZR'><dt id='METZR'><q id='METZR'><span id='METZR'><b id='METZR'><form id='METZR'><ins id='METZR'></ins><ul id='METZR'></ul><sub id='METZR'></sub></form><legend id='METZR'></legend><bdo id='METZR'><pre id='METZR'><center id='METZR'></center></pre></bdo></b><th id='METZR'></th></span></q></dt></tr></i><div id='METZR'><tfoot id='METZR'></tfoot><dl id='METZR'><fieldset id='METZR'></fieldset></dl></div>

                • <small id='METZR'></small><noframes id='METZR'>

                  本文介绍了Python discord bot 离开语音频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我已经让机器人加入我的语音服务器,如下所示

                  I've made bot to join my voice server like below

                   if message.content.startswith("?join"):
                      channel = message.author.voice.channel
                      await channel.connect()
                      await message.channel.send('bot joined')
                  

                  但我不能让机器人离开频道.. 我该如何编写代码来做到这一点?和有什么区别

                  but i can't make bot to leave the channel.. how can i code to do it?? and what is the difference between

                  @bot.event
                   async def on_message(message):
                   if message.content.startswith('~'):
                  

                  @bot.command()
                  async def ~(ctx):
                  

                  推荐答案

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

                  You can do both of these commands (the join and leave channel commands) in two ways, one is by using on_message, and the other is by using @bot.commands. It's is best to use bot.command instead of on_message for commands as bot.commands has more features plus I think it's fast after all it was built for commands. So I'll rewrite both of your commands using bot.command and also put using on_message there incase you don't want to use bot.command. According to your message, I'm assuming ? is your prefix

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

                  这篇关于Python discord bot 离开语音频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Discord.py 使用 wait_for 检查用户输入 下一篇:如何在 discord.py 中获取所有成员列表?

                  相关文章

                  最新文章

                      <small id='G7Zfr'></small><noframes id='G7Zfr'>

                      <legend id='G7Zfr'><style id='G7Zfr'><dir id='G7Zfr'><q id='G7Zfr'></q></dir></style></legend>
                      • <bdo id='G7Zfr'></bdo><ul id='G7Zfr'></ul>

                      <tfoot id='G7Zfr'></tfoot>
                    1. <i id='G7Zfr'><tr id='G7Zfr'><dt id='G7Zfr'><q id='G7Zfr'><span id='G7Zfr'><b id='G7Zfr'><form id='G7Zfr'><ins id='G7Zfr'></ins><ul id='G7Zfr'></ul><sub id='G7Zfr'></sub></form><legend id='G7Zfr'></legend><bdo id='G7Zfr'><pre id='G7Zfr'><center id='G7Zfr'></center></pre></bdo></b><th id='G7Zfr'></th></span></q></dt></tr></i><div id='G7Zfr'><tfoot id='G7Zfr'></tfoot><dl id='G7Zfr'><fieldset id='G7Zfr'></fieldset></dl></div>