python - 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
解决方案
你打错字了。您忘记了f
在每个“”之前,print()
如果您使用 {},所有函数应该如下所示:
print(f"Command '{ctx.command}' Not Found\n")
在所有打印功能中添加f
每个“”之前,您的问题将得到解决!
推荐阅读
- excel - VBA:如何将 Cell.Address() 传递给可用的 sheet.range.select 格式,以便我可以将新值放入单元格
- python - 使用 argparse 将逗号分隔的多个整数值作为输入,并检查列表中是否存在这些值
- d3.js - D3 数据 join() 调用输入而不是更新
- css - 更改嵌套 div 中复选框的颜色
- ios - 从 iOS Objective-C 框架访问 Bundle 资源(字体、图像)
- c++ - QDirIterator (Windows) 应该不区分大小写,但不区分大小写
- spring-boot - 从其他类继承时出现 Hibernate JPA 错误
- kotlin - 为 mongodb 的 micronaut 设置测试容器
- z3 - Z3 创建两个具有相同元素但顺序不同的 intVector
- python - 英特尔实感摄像头拍摄的 RGB 图像较暗(使用 python 代码)