所以,我是 discord.py 的新手,我正在尝试实现一个错误处理系统,该系统将在终端中显示 commandnotfound 、 missingrequiredarguments 等。问题是我希望它显示无法通过写入 print("Command '{ctx.command}' Not Found")
识别的输入命令,而是只输出 Command '{ctx.command}' Not Found
而不是 Command 'test' Not Found
。任何帮助将不胜感激!
我的代码:
async def on_command_error(ctx, error):
if isinstance(error, commands.errors.CommandNotFound):
print("Command '{ctx.command}' Not Found\n")
if isinstance(error, commands.errors.MissingRequiredArgument):
print("Command '{ctx}.command' Has Missing Required Arguments\n")
if isinstance(error, commands.errors.NoPrivateMessage):
try:
print("Command '{ctx.command}' Cannot Be Used In Private Messages")
except discord.HTTPException:
print("Command '{ctx.command}' Cannot Be Used In Private Messages (With HTTPExeption)")
if isinstance(error, commands.errors.DisabledCommand):
print("Command '{ctx.command}' Has Been Disabled")
elif isinstance(error, commands.BadArgument):
if ctx.command.qualified_name == 'tag list':
print("Command '{ctx.command}' Defined An Invalid Member")
else:
print("Other Error")
预期结果:
Command 'test' Has Missing Required Arguments
Command 'test' Cannot Be Used In Private Messages
Command 'test' Cannot Be Used In Private Messages (With HTTPExeption)
Command 'test' Has Been Disabled
Command 'test' Defined An Invalid Member
实际结果:
Command '{ctx.command}' Has Missing Required Arguments
Command '{ctx.command}' Cannot Be Used In Private Messages
Command '{ctx.command}' Cannot Be Used In Private Messages (With HTTPExeption)
Command '{ctx.command}' Has Been Disabled
Command '{ctx.command}' Defined An Invalid Member
答案 0 :(得分:0)
你打错了。您在每个 "" 之前忘记了 f
,如果您使用 {},则所有 print()
函数都应如下所示:
print(f"Command '{ctx.command}' Not Found\n")
在所有打印功能中的每个“”之前添加f
,您的问题将得到解决!!