khl.py icon indicating copy to clipboard operation
khl.py copied to clipboard

设置单个命令的exc_handlers会导致其他命令报错

Open axiangcoding opened this issue 1 year ago • 2 comments

Describe the bug 根据 https://github.com/TWT233/khl.py/issues/231 这里的描述,我在其中一个命令中注册了通用错误处理器,但是在另外一个命令中,在正确执行命令后,又必定匹配到 khl.command.exception.Exceptions.Lexer.NotMatched 的错误,导致回复异常消息

To Reproduce 最小demo为:

async def common_error_handler(cmd: command.Command, exc: Exception, msg: Message):
    logger.error(f"Command {cmd.name} raised an exception: {exc}")
    if isinstance(exc, Exceptions.Handler.ArgLenNotMatched):
        cm = CardTemplate.warning_card(
            card_title="参数长度错误",
            main_modules=[
                Module.Section("请检查命令是否正确"),
                Module.Section("如果你不知道应该如何使用,请查看文档"),
            ],
        )
    elif isinstance(exc, ArgParseError):
        cm = CardTemplate.warning_card(
            card_title="参数解析错误",
            main_modules=[
                Module.Section(f"**错误**:{exc}"),
                Module.Section("如果你不知道应该如何使用,请查看文档"),
            ],
        )
    else:
        cm = CardTemplate.danger_card(
            card_title="发生了一个未知错误",
            main_modules=[
                Module.Section(f"发生了一个未知错误:{type(exc)} {exc}"),
                Module.Section("请重试。如果无法解决,请联系开发者"),
            ],
        )
    await msg.reply(cm.get_card_message())


default_exc_handlers: dict[Any, TypeEHandler] = {
    Exception: common_error_handler,
}

@bot.command(
      name="直播",
      aliases=["live"],
      exc_handlers=common.default_exc_handlers,
  )
async def set_live(message: PublicMessage, cmd: str, room_id: str, notify_role: str = ""):
  ...

@bot.command(
      name="帮助",
      aliases=["help"],
  )
async def help(msg: Message):
    ...

此时,我调用 /prefix 帮助时,会回复两个命令,一个是正常的帮助回复,一个是通用错误处理器里的错误,并得到日志:

Expected behavior 在单个命令中设置exc_handlers不会影响到其他命令

Logs/Screenshots image

报错是

2024-05-04 13:38:50.882 | ERROR    | wtbot.kook.commands.common:common_error_handler:12 - Command 直播 raised an exception: 

Environment

  • Python version: 3.11
  • khl.py version: 0.3.17
  • OS: Windows, Linux

Additional context Add any other context about the problem here.

axiangcoding avatar May 04 '24 05:05 axiangcoding

我看下是不是哪里没有deep copy导致重复使用了

TWT233 avatar May 05 '24 17:05 TWT233

我看下是不是哪里没有deep copy导致重复使用了

这个问题是否有进展?大概会是哪里的问题?

axiangcoding avatar May 11 '24 06:05 axiangcoding