How to use command name with spaces?(如何使用带空格的命令名称?)
问题描述
当python bot中的命令之间有空格时如何使bot工作.我知道我们可以使用 sub-command 或 on_message
来做到这一点,但是有没有其他选项可以只针对选定的命令而不是所有命令来做到这一点.
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.
以下代码将不起作用.
@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)
推荐答案
严格来说,你不能.由于 discord.py 的命令名称以空格结尾,如 views.py 中所定义.然而,有一些选择:重新编写 discord.py 视图如何处理消息(我不推荐这样做),使用 on_message
和 message.content.startswith
,或使用组.
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.
由于 on_message
使用起来相当简单,因此我将向您展示如何破解"group
语法以允许命令名称带有空格.
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)
不和谐:
me: <prefix>mobile phones
bot: Pong. @me
这篇关于如何使用带空格的命令名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用带空格的命令名称?
基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01