Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,44 @@ def __init__(

@self.bot.on_request()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): 建议提取一个共享的辅助协程,来封装四个事件处理器中通用的 try/except 和消息处理逻辑。

你可以在保留当前错误处理行为的同时,通过为这四个处理器提取一个共享的辅助函数,来消除重复代码和多余的嵌套。

例如:

async def _safe_handle(self, event: Event, context: str):
    try:
        abm = await self.convert_message(event)
        if not abm:
            return
        await self.handle_msg(abm)
    except BaseException as e:
        logger.error(f"Handle {context} message failed: {e}")
        return

然后各个处理器可以变为:

@self.bot.on_request()
async def request(event: Event):
    await self._safe_handle(event, "request")


@self.bot.on_notice()
async def notice(event: Event):
    await self._safe_handle(event, "notice")


@self.bot.on_message("group")
async def group(event: Event):
    await self._safe_handle(event, "group")


@self.bot.on_message("private")
async def private(event: Event):
    await self._safe_handle(event, "private")

这样可以保证:

  • 保留新的 try/except BaseException 语义;
  • 保留当 abm 为假值时跳过处理的行为;
  • 保留每个处理器各自的日志信息;

同时去掉了复制粘贴的代码块和每个处理器中的一层缩进,使文件更易阅读和维护。

Original comment in English

issue (complexity): Consider extracting a shared helper coroutine to wrap the common try/except and message-handling logic used by all four event handlers.

You can keep the new error‑handling behavior while removing the duplication and extra nesting by extracting a shared helper for the four handlers.

For example:

async def _safe_handle(self, event: Event, context: str):
    try:
        abm = await self.convert_message(event)
        if not abm:
            return
        await self.handle_msg(abm)
    except BaseException as e:
        logger.error(f"Handle {context} message failed: {e}")
        return

Then the handlers become:

@self.bot.on_request()
async def request(event: Event):
    await self._safe_handle(event, "request")


@self.bot.on_notice()
async def notice(event: Event):
    await self._safe_handle(event, "notice")


@self.bot.on_message("group")
async def group(event: Event):
    await self._safe_handle(event, "group")


@self.bot.on_message("private")
async def private(event: Event):
    await self._safe_handle(event, "private")

This preserves:

  • The new try/except BaseException semantics.
  • The behavior of skipping when abm is falsy.
  • The per‑handler log messages.

But it removes the copy‑pasted blocks and one indentation level in each handler, making the file easier to scan and maintain.

async def request(event: Event):
abm = await self.convert_message(event)
if abm:
try:
abm = await self.convert_message(event)
if not abm:
return
await self.handle_msg(abm)
except Exception as e:
logger.exception(f"Handle request message failed: {e}")
return

@self.bot.on_notice()
async def notice(event: Event):
abm = await self.convert_message(event)
if abm:
await self.handle_msg(abm)
try:
abm = await self.convert_message(event)
if abm:
await self.handle_msg(abm)
except Exception as e:
logger.exception(f"Handle notice message failed: {e}")
return

@self.bot.on_message("group")
async def group(event: Event):
abm = await self.convert_message(event)
if abm:
await self.handle_msg(abm)
try:
abm = await self.convert_message(event)
if abm:
await self.handle_msg(abm)
except Exception as e:
logger.exception(f"Handle group message failed: {e}")
return

@self.bot.on_message("private")
async def private(event: Event):
abm = await self.convert_message(event)
if abm:
await self.handle_msg(abm)
try:
abm = await self.convert_message(event)
if abm:
await self.handle_msg(abm)
except Exception as e:
logger.exception(f"Handle private message failed: {e}")
return

@self.bot.on_websocket_connection
def on_websocket_connection(_):
Expand Down Expand Up @@ -372,9 +389,10 @@ async def _convert_handle_message_event(

message_str += "".join(at_parts)
elif t == "markdown":
text = m["data"].get("markdown") or m["data"].get("content", "")
abm.message.append(Plain(text=text))
message_str += text
for m in m_group:
text = m["data"].get("markdown") or m["data"].get("content", "")
abm.message.append(Plain(text=text))
message_str += text
else:
for m in m_group:
try:
Expand Down