【问题标题】:How to make a command case insensitive in discord.py如何在 discord.py 中使命令不区分大小写
【发布时间】:2018-06-15 15:53:19
【问题描述】:

如何在不为不同的大写字母添加许多别名的情况下使命令不区分大小写,如下所示:

@bot.command(pass_context = True, name = 'test', aliases=['Test', 'tEst', 'teSt', 'tesT', 'TEst', 'TeSt', 'TesT', 'tESt', 'tEsT'])
async def test(self, ctx):
    #do stuff      

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    我个人对 discord.py 并不熟悉,可能对此有误。

    在我看来,不区分大小写不是 discord.py 的一个特性,而且将来也不会成为一个特性,according to this Github thread。我引用:

    我不会在本地添加这个。然而,在支持这一点的重写中,用户端很简单:

    async def on_message(self, message):
      ctx = await self.get_context(message)
      if ctx.prefix is not None:
        ctx.command = self.commands.get(ctx.invoked_with.lower())
        await self.invoke(ctx)
    

    所以在我看来,您可以像上面一样提供自己的on_message,您应该很高兴。

    【讨论】:

      【解决方案2】:

      在重写分支上,commands.Bot 接受 case_insensitive 参数

      bot = commands.Bot(command_prefix='!', case_insensitive=True)
      

      请注意,使用此功能时会有性能损失。

      【讨论】:

      • 您是否需要导入任何其他模块才能使其正常工作,因为它无法正常工作?
      • 性能损失多少?
      【解决方案3】:

      你也可以使用
      if message.content.lower().startswith('command'): 不一定要以

      开头

      【讨论】:

        【解决方案4】:

        当我的第一个不和谐机器人不知道“case_insensitive”属性时,我发现了一种奇怪的方法。 我在“on_message”函数中使用了它。

        await bot.process_commands(msg.content.lower())
        

        您可以使用 commands.process_commands() 代替,因为您在 Cog 中。

        【讨论】:

          猜你喜欢
          • 2018-11-04
          • 1970-01-01
          • 2021-06-01
          • 2021-04-12
          • 1970-01-01
          • 1970-01-01
          • 2021-04-12
          • 2021-01-01
          • 2021-05-20
          相关资源
          最近更新 更多