如何在命令名中使用空格? [英] How to use command name with spaces?

查看:91
本文介绍了如何在命令名中使用空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当python bot中的命令之间有空格时,如何使bot运行。我知道我们可以使用子命令或 on_message 来做到这一点,但是还有别的选择可以为仅选定的命令而不是为所有命令做到这一点。 b

以下代码将不起作用。

  @ bot.command(pass_context = True)
异步def手机(ctx):
msg = Pong。{0.author.mention}。format(ctx.message)
等待bot.say(msg)

所以我尝试使用别名,但仍然无法使用。

  @ bot.command(pass_context = True,别名= ['手机'])
异步def手机(ctx):
msg = Pong。{ 0.author.mention}。format(ctx.message)
等待bot.say(msg)


解决方案

严格地说,你不能。由于discord.py的命令名称以空格结尾(如views.py中所定义)。但是,有一些选项:重新编写discord.py视图如何处理消息(我不推荐这样做),使用 on_message message .content.startswith 或使用组。



由于 on_message 很简单使用,我将向您展示如何破解 语法以允许命令名称带有空格。

 类chain_command:
def __init __(self,name,** kwargs):
names = name.split()
self.last = names [-1]
self.names = iter(names [:-1])$ ​​b $ b self.kwargs = kwargs

@staticmethod
异步def null():
return

def __call __(self,func):
来自functools import减少
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 (手机,pass_context = Tr ue)
async def mobile_phones(ctx):
msg = Pong。 {0.author.mention}。format(ctx.message)
等待bot.say(msg)

不一致:

 我:< prefix>手机
机器人:Pong。


How to make bot works when there is a space between commands in python bot. I know we can do that using sub-command or on_message but is there any another option to do that for only selected commands not for all commands.

The following code will not work.

@bot.command(pass_context=True)
async def mobile phones(ctx):
    msg = "Pong. {0.author.mention}".format(ctx.message)
    await bot.say(msg)

So I tried using alias but still it won't working.

@bot.command(pass_context=True, aliases=['mobile phones'])
async def phones(ctx):
    msg = "Pong. {0.author.mention}".format(ctx.message)
    await bot.say(msg)

解决方案

Strictly to say, you can't. Since discord.py's command names ends with space, as defined in views.py. There are, however, a few options: re write how discord.py views handle messages (I wouldn't recommend this), use on_message and message.content.startswith, or use groups.

Since on_message is fairly straight forward to use, I will instead show you how you can "hack" the group syntax to allow command name with spaces.

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)

In discord:

me: <prefix>mobile phones
bot: Pong. @me

这篇关于如何在命令名中使用空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆