From 8c94a0010cf5a38bd8ddc0740a4c39d61faaf8ae Mon Sep 17 00:00:00 2001 From: Oscar Shaw Date: Mon, 22 Dec 2025 19:54:26 +0800 Subject: [PATCH] fix(core): improve error handling of command parser and sync (#4161) --- astrbot/core/star/command_management.py | 18 ++++++++++++------ astrbot/core/star/star_manager.py | 6 +++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/astrbot/core/star/command_management.py b/astrbot/core/star/command_management.py index a0b125d33..190ef2000 100644 --- a/astrbot/core/star/command_management.py +++ b/astrbot/core/star/command_management.py @@ -4,7 +4,7 @@ from collections import defaultdict from dataclasses import dataclass, field from typing import Any -from astrbot.core import db_helper +from astrbot.core import db_helper, logger from astrbot.core.db.po import CommandConfig from astrbot.core.star.filter.command import CommandFilter from astrbot.core.star.filter.command_group import CommandGroupFilter @@ -192,12 +192,18 @@ def _collect_descriptors(include_sub_commands: bool) -> list[CommandDescriptor]: """收集指令,按需包含子指令。""" descriptors: list[CommandDescriptor] = [] for handler in star_handlers_registry: - desc = _build_descriptor(handler) - if not desc: + try: + desc = _build_descriptor(handler) + if not desc: + continue + if not include_sub_commands and desc.is_sub_command: + continue + descriptors.append(desc) + except Exception as e: + logger.warning( + f"解析指令处理函数 {handler.handler_full_name} 失败,跳过该指令。原因: {e!s}" + ) continue - if not include_sub_commands and desc.is_sub_command: - continue - descriptors.append(desc) return descriptors diff --git a/astrbot/core/star/star_manager.py b/astrbot/core/star/star_manager.py index 1f9f95ae5..c142c0e9b 100644 --- a/astrbot/core/star/star_manager.py +++ b/astrbot/core/star/star_manager.py @@ -631,7 +631,11 @@ class PluginManager: # 清除 pip.main 导致的多余的 logging handlers for handler in logging.root.handlers[:]: logging.root.removeHandler(handler) - await sync_command_configs() + try: + await sync_command_configs() + except Exception as e: + logger.error(f"同步指令配置失败: {e!s}") + logger.error(traceback.format_exc()) if not fail_rec: return True, None