如何在 discord.py 中使命令不区分大小写
How to make a command case insensitive in discord.py
如何在不为不同大小写添加许多别名的情况下使命令不区分大小写,如下所示:
@bot.command(pass_context = True, name = 'test', aliases=['Test', 'tEst', 'teSt', 'tesT', 'TEst', 'TeSt', 'TesT', 'tESt', 'tEsT'])
async def test(self, ctx):
#do stuff
我个人对discord.py不是很熟悉,这方面可能有误。
在我看来,不区分大小写不是 discord.py 的特性,将来也不会成为 according to this Github thread。我引用:
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)
所以在我看来你可以像上面那样提供你自己的 on_message
,你应该可以开始了。
在重写分支上,commands.Bot
接受一个 case_insensitive
参数
bot = commands.Bot(command_prefix='!', case_insensitive=True)
请注意,使用此功能会降低性能。
你也可以使用
if message.content.lower().startswith('command'):
不一定要以
开头
我在制作第一个不知道“case_insensitive”属性 的 discord 机器人时发现了一种奇怪的方法。
我在我的“on_message”函数中使用了它。
await bot.process_commands(msg.content.lower())
您可以使用 commands.process_commands()
,因为您在 Cog 中。
如何在不为不同大小写添加许多别名的情况下使命令不区分大小写,如下所示:
@bot.command(pass_context = True, name = 'test', aliases=['Test', 'tEst', 'teSt', 'tesT', 'TEst', 'TeSt', 'TesT', 'tESt', 'tEsT'])
async def test(self, ctx):
#do stuff
我个人对discord.py不是很熟悉,这方面可能有误。
在我看来,不区分大小写不是 discord.py 的特性,将来也不会成为 according to this Github thread。我引用:
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)
所以在我看来你可以像上面那样提供你自己的 on_message
,你应该可以开始了。
在重写分支上,commands.Bot
接受一个 case_insensitive
参数
bot = commands.Bot(command_prefix='!', case_insensitive=True)
请注意,使用此功能会降低性能。
你也可以使用
if message.content.lower().startswith('command'):
不一定要以
我在制作第一个不知道“case_insensitive”属性 的 discord 机器人时发现了一种奇怪的方法。 我在我的“on_message”函数中使用了它。
await bot.process_commands(msg.content.lower())
您可以使用 commands.process_commands()
,因为您在 Cog 中。