你好,我试图在 await bot.send_message 中添加 {0.subcommand_passed},但我收到错误消息,因此如何添加以捕获回复中传递的错误命令。
因此,如果成员键入 ?helol 而不是 ?hello,Bot 应该回复 helol is a wrong command
@bot.event
async def on_command_error(error, ctx):
if isinstance(error, commands.CommandNotFound):
await bot.send_message(ctx.message.channel, "**Wrong command ** " + ctx.invoked_with)
并且在子命令中,如果成员键入 ?cool girl 而不是 ?cool boy,Bot 应该回复 girl is a wrong sub command
@bot.group(pass_context=True)
async def cool(ctx):
if ctx.invoked_subcommand is None:
if ctx.subcommand_passed:
await bot.say("**Wrong sub command: **{}".format(ctx.subcommand_passed))
else:
await bot.say("**Subcommand required**. {0.author.mention}".format(ctx.message))
@cool.command(pass_context=True)
async def boy(ctx):
msg = "Hello ...".format(ctx.message)
await bot.say(msg)
请您参考如下方法:
使用Context.subcommand_passed获取尝试的子命令的文本。
@bot.group(pass_context=True)
async def cool(ctx):
if ctx.invoked_subcommand is None:
if ctx.subcommand_passed:
await bot.say("**Wrong sub command: **{}".format(ctx.subcommand_passed))
else:
await bot.say("Subcommand required")
@cool.command(pass_context=True, name="bot")
async def _bot(ctx):
msg = "Hello ...".format(ctx.message)
await bot.say(msg)
无法识别的顶级命令将引发 CommandNotFound ,您必须在 on_command_error 中处理它。我相信它仍然应该填充 ctx.command使用尝试的命令,但我现在无法检查。您也可以尝试 ctx.invoked_with
@bot.event
async def on_command_error(error, ctx):
if isinstance(error, commands.CommandNotFound):
await bot.send_message(ctx.message.channel, "**Wrong command ** " + ctx.command)
