【发布时间】:2021-11-16 11:05:48
【问题描述】:
在 discord.py 1.7.3 上,当我想确保命令在给定参数时出错时,我会这样做:
@commands.command(name='hi')
@commands.is_owner()
async def hi(self, ctx, *, arg=None):
if arg:
raise commands.TooManyArguments
else:
await ctx.send(f'hi')
我想创建一个在文档中描述的检查装饰器,让它更优雅一点:https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#checks
所以我做到了:
def has_args():
def predicate(ctx):
print(ctx.args, ctx.kwargs)
if len(ctx.args) > 2 or kwargs:
raise commands.TooManyArguments
return True
return commands.check(predicate)
...
@commands.command(name='hi')
@commands.is_owner()
@has_args()
async def hi(self, ctx, *, arg=None):
await ctx.send(f'hi')
但 ctx.args 和 ctx.kwargs 始终为空,无论我传递给命令的参数数量如何(又名 {prefix}hi 1 2 3 产生与 {prefix}hi 相同的打印)
有没有更好的方法来做到这一点?我错过了什么?
【问题讨论】:
标签: python discord.py python-decorators