当 python bot 中的命令之间有空格时,如何使 bot 工作。我知道我们可以使用子命令或on_message
但是是否还有其他选项可以仅对选定的命令而不是对所有命令执行此操作。
下面的代码将不起作用。
@bot.command(pass_context=True)
async def mobile phones(ctx):
msg = "Pong. {0.author.mention}".format(ctx.message)
await bot.say(msg)
所以我尝试使用别名,但仍然不起作用。
@bot.command(pass_context=True, aliases=['mobile phones'])
async def phones(ctx):
msg = "Pong. {0.author.mention}".format(ctx.message)
await bot.say(msg)
严格来说,你不能。由于discord.py 的命令名称以空格结尾,如views.py 中所定义。然而,有一些选择:重新编写discord.py视图如何处理消息(我不推荐这样做),使用on_message
and message.content.startswith
,或使用组。
Since on_message
使用起来相当简单,我将向您展示如何“破解”group
允许命令名称带有空格的语法。
class chain_command:
def __init__(self, name, **kwargs):
names = name.split()
self.last = names[-1]
self.names = iter(names[:-1])
self.kwargs = kwargs
@staticmethod
async def null():
return
def __call__(self, func):
from functools import reduce
return reduce(lambda x, y: x.group(y)(self.null), self.names, bot.group(next(self.names))(self.null)).command(self.last, **self.kwargs)(func)
@chain_command("mobile phones", pass_context=True)
async def mobile_phones(ctx):
msg = "Pong. {0.author.mention}".format(ctx.message)
await bot.say(msg)
不和谐:
me: <prefix>mobile phones
bot: Pong. @me
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)