refactor: 优化代码结构,简化条件判断和异常处理

This commit is contained in:
DrSmoothl
2026-03-06 12:00:00 +08:00
parent 2f21cd00bc
commit 1cd366bc09
8 changed files with 41 additions and 66 deletions

View File

@@ -42,8 +42,7 @@ class RegisteredComponent:
# 预编译命令正则(仅 command 类型)
self._compiled_pattern: re.Pattern | None = None
if component_type == "command":
pattern = metadata.get("command_pattern", "")
if pattern:
if pattern := metadata.get("command_pattern", ""):
try:
self._compiled_pattern = re.compile(pattern)
except re.error as e:
@@ -120,8 +119,7 @@ class ComponentRegistry:
comps = self._by_plugin.pop(plugin_id, [])
for comp in comps:
self._components.pop(comp.full_name, None)
type_dict = self._by_type.get(comp.component_type)
if type_dict:
if type_dict := self._by_type.get(comp.component_type):
type_dict.pop(comp.full_name, None)
return len(comps)
@@ -162,9 +160,7 @@ class ComponentRegistry:
) -> list[RegisteredComponent]:
"""按插件查询。"""
comps = self._by_plugin.get(plugin_id, [])
if enabled_only:
return [c for c in comps if c.enabled]
return list(comps)
return [c for c in comps if c.enabled] if enabled_only else list(comps)
def find_command_by_text(self, text: str) -> RegisteredComponent | None:
"""通过文本匹配命令正则,返回第一个匹配的 command 组件。"""