首页 > 解决方案 > Discord.py - {ctx.command} 不显示输入的命令

问题描述

所以,我是 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

标签: pythondiscord.py

解决方案


你打错字了。您忘记了f在每个“”之前,print()如果您使用 {},所有函数应该如下所示:

print(f"Command '{ctx.command}' Not Found\n")

在所有打印功能中添加f每个“”之前,您的问题将得到解决!


推荐阅读