Python 如何将命令名与空格一起使用?

Python 如何将命令名与空格一起使用?,python,discord,discord.py,Python,Discord,Discord.py,如何使bot在pythonbot中的命令之间有一个空格时工作。我知道我们可以使用sub命令或消息上的来执行此操作,但是否有其他选项可以仅对选定命令执行此操作,而不是对所有命令执行此操作 以下代码将不起作用 @bot.command(pass_context=True) async def mobile phones(ctx): msg = "Pong. {0.author.mention}".format(ctx.message) await bot.say(msg) 所以我尝

如何使bot在pythonbot中的命令之间有一个空格时工作。我知道我们可以使用sub命令或消息上的
来执行此操作,但是否有其他选项可以仅对选定命令执行此操作,而不是对所有命令执行此操作

以下代码将不起作用

@bot.command(pass_context=True)
async def mobile phones(ctx):
    msg = "Pong. {0.author.mention}".format(ctx.message)
    await bot.say(msg)
所以我尝试使用alias,但仍然无法使用

@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视图如何处理消息(我不建议这样做),在消息上使用
message.content.startswith
,或者使用组

由于消息上的
使用起来相当简单,因此我将向您展示如何“破解”组
语法以允许命令名带有空格

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
me:手机
机器人:庞@我

这是一种不太复杂的方法,但是您可以将args作为命令名本身传递!因此,在您的示例中,
移动电话
可以在其上使用arg

@client.command
async def mobile(ctx, phones = None)
  if phones != "phones":
    return
  await ctx.send("Yay it works")
   

我不确定这是否可能。我很确定,在考虑命令之前,解析出您试图调用的命令会分割输入。您可以做的就是在您的
on_message
事件中添加
if message.content.startswith(“!mobile phones”)
。也许你也可以从那里调用这个命令,但我不确定它是如何工作的。