有没有办法列出某些机器人命令的参数?
Is there a way to list arguments of certain bot commands?
我正在尝试为命令生成一条错误消息,因为当您没有包含所有参数时,它会将它们回显给您,例如:
@commands.command()
async def foo(self, ctx, arg1, arg2):
await ctx.send("You passed: {} and {}".format(arg1, arg2))
我知道如果我 运行 >foo
本身,它会 return "arg1 is a required argument that is missing",但是我希望它 return 所有这些,例如 Usage: >foo <arg1> <arg2>
我也知道我总是可以使用这样的东西对其进行硬编码:
@foo.error
async def foo_error(self, ctx, error):
await ctx.send("``Usage: >foo <arg1> <arg2>``")
但我想要它,所以它可以自动对输入的任何命令执行此操作,所以我不必像这样执行
您可以使用 *argv
作为参数,例如 def foo(self, *args):
,然后检查 args 是否具有正确的长度
async def foo(self, *args):
if len(args) != 2:
await ctx.send("``Usage: >foo <arg1> <arg2>``")
else:
# Do something else
您可以检查错误是否是 MissingRequiredArgument
, and if it is send the help text associated with the command. You can use cog_command_error
到 运行 来自 cog 的所有错误的错误处理程序:
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def foo(self, ctx, arg1, arg2):
await ctx.send("You passed: {} and {}".format(arg1, arg2))
async def cog_command_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
message = f"Params are {', '.join(ctx.command.clean_params)}, you are missing {error.param}"
await ctx.send(message)
else:
raise error
您可以使用 Command
的一些其他属性来代替 help
,例如 brief
或 clean_params
,具体取决于您希望输出的内容看起来像。
我正在尝试为命令生成一条错误消息,因为当您没有包含所有参数时,它会将它们回显给您,例如:
@commands.command()
async def foo(self, ctx, arg1, arg2):
await ctx.send("You passed: {} and {}".format(arg1, arg2))
我知道如果我 运行 >foo
本身,它会 return "arg1 is a required argument that is missing",但是我希望它 return 所有这些,例如 Usage: >foo <arg1> <arg2>
我也知道我总是可以使用这样的东西对其进行硬编码:
@foo.error
async def foo_error(self, ctx, error):
await ctx.send("``Usage: >foo <arg1> <arg2>``")
但我想要它,所以它可以自动对输入的任何命令执行此操作,所以我不必像这样执行
您可以使用 *argv
作为参数,例如 def foo(self, *args):
,然后检查 args 是否具有正确的长度
async def foo(self, *args):
if len(args) != 2:
await ctx.send("``Usage: >foo <arg1> <arg2>``")
else:
# Do something else
您可以检查错误是否是 MissingRequiredArgument
, and if it is send the help text associated with the command. You can use cog_command_error
到 运行 来自 cog 的所有错误的错误处理程序:
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def foo(self, ctx, arg1, arg2):
await ctx.send("You passed: {} and {}".format(arg1, arg2))
async def cog_command_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
message = f"Params are {', '.join(ctx.command.clean_params)}, you are missing {error.param}"
await ctx.send(message)
else:
raise error
您可以使用 Command
的一些其他属性来代替 help
,例如 brief
或 clean_params
,具体取决于您希望输出的内容看起来像。