Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
axiangcoding opened this issue May 4, 2024 · 2 comments
Open

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

axiangcoding opened this issue May 4, 2024 · 2 comments

Comments

@axiangcoding
Copy link

axiangcoding commented May 4, 2024

Describe the bug
根据 #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.

@TWT233
Copy link
Owner

TWT233 commented May 5, 2024

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

@axiangcoding
Copy link
Author

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants