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

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

      <tfoot id='d4ILY'></tfoot>

        <legend id='d4ILY'><style id='d4ILY'><dir id='d4ILY'><q id='d4ILY'></q></dir></style></legend>

        Discord.py 拼写检查命令

        时间:2023-10-11
            <tbody id='DTDDx'></tbody>
          <tfoot id='DTDDx'></tfoot>

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

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

                <i id='DTDDx'><tr id='DTDDx'><dt id='DTDDx'><q id='DTDDx'><span id='DTDDx'><b id='DTDDx'><form id='DTDDx'><ins id='DTDDx'></ins><ul id='DTDDx'></ul><sub id='DTDDx'></sub></form><legend id='DTDDx'></legend><bdo id='DTDDx'><pre id='DTDDx'><center id='DTDDx'></center></pre></bdo></b><th id='DTDDx'></th></span></q></dt></tr></i><div id='DTDDx'><tfoot id='DTDDx'></tfoot><dl id='DTDDx'><fieldset id='DTDDx'></fieldset></dl></div>
                • 本文介绍了Discord.py 拼写检查命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  最近,我查看了 Stack Overflow,发现了这段代码可以检查潜在的拼写错误:

                  Recently, I looked up Stack Overflow and found this code which can check for potential typos:

                  from difflib import SequenceMatcher
                  SequenceMatcher(None, "help", "hepl").ratio()
                  # Returns 0.75
                  

                  这适用于 inside bot 命令的代码.但是,我应该如何做到这一点,如果我在命令名称中打错字,它会更正并执行命令?

                  This works for code inside the bot command. However, how should I make it so, if I make a typo in the command name, it will correct it and execute the command?

                  推荐答案

                  from difflib import SequenceMatcher
                  
                  @bot.event
                  async def on_command_error(ctx, error):
                      if not isinstance(error, commands.CommandNotFound):
                          return
                  
                      message = ctx.message  # later overwrite the attributes
                  
                      used_prefix = ctx.prefix  # the prefix used
                      used_command = message.content.split()[0][len(used_prefix):]  # getting the command, `!foo a b c` -> `foo`
                  
                      available_commands = [cmd.name for cmd in bot.commands]
                      matches = {  # command name: ratio
                          cmd: SequenceMatcher(None, cmd, used_command).ratio()
                          for cmd in available_commands
                      }
                  
                      command = max(matches.items(), key=lambda item: item[1])[0]  # the most similar command
                  
                      try:
                          arguments = message.content.split(" ", 1)[1]
                      except IndexError:
                          arguments = ""  # command didn't take any arguments
                  
                      new_content = f"{used_prefix}{command} {arguments}".strip()
                      message.content = new_content  # overwriting the "original" message
                  
                      await bot.process_commands(message)  # processing commands with the new, updated message
                  

                  这篇关于Discord.py 拼写检查命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何检查 X 分钟内是否没有消息发送?(discord.py) 下一篇:TypeError:禁止从 URL 继承类

                  相关文章

                  最新文章

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

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

                      <bdo id='L2Nxw'></bdo><ul id='L2Nxw'></ul>
                    <legend id='L2Nxw'><style id='L2Nxw'><dir id='L2Nxw'><q id='L2Nxw'></q></dir></style></legend>
                  1. <tfoot id='L2Nxw'></tfoot>