How to make a command case insensitive in discord.py

橙三吉。 提交于 2019-11-28 08:55:30

问题


How would one make a command case-insensitive without adding many aliases for different capitalizations like this:

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

回答1:


On the rewrite branch, commands.Bot accepts a case_insensitive parameter

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

Note that there is a performance loss when using this feature.




回答2:


I am not personally familiar with discord.py, and may be wrong about this.

It looks to me like case-insensitivity is not a feature of discord.py and will not become one in the future, according to this Github thread. And I quote:

I won't add this natively. However in the rewrite supporting this is simple from the user end:

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)

So it looks to me like you can just provide your own on_message as above, and you should be good to go.




回答3:


you could also use
if message.content.lower().startswith('command'): doesnt necessarily have to be startswith



来源:https://stackoverflow.com/questions/48120312/how-to-make-a-command-case-insensitive-in-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!