command简化,修复unregister的bug

This commit is contained in:
UnCLAS-Prommer
2025-07-25 13:39:27 +08:00
parent 5ca1269e48
commit 229d45083d
4 changed files with 14 additions and 25 deletions

View File

@@ -20,6 +20,7 @@ class HelloAction(BaseAction):
# === 基本信息(必须填写)=== # === 基本信息(必须填写)===
action_name = "hello_greeting" action_name = "hello_greeting"
action_description = "向用户发送问候消息" action_description = "向用户发送问候消息"
activation_type = ActionActivationType.ALWAYS # 始终激活
# === 功能描述(必须填写)=== # === 功能描述(必须填写)===
action_parameters = {"greeting_message": "要发送的问候消息"} action_parameters = {"greeting_message": "要发送的问候消息"}
@@ -44,8 +45,7 @@ class ByeAction(BaseAction):
action_description = "向用户发送告别消息" action_description = "向用户发送告别消息"
# 使用关键词激活 # 使用关键词激活
focus_activation_type = ActionActivationType.KEYWORD activation_type = ActionActivationType.KEYWORD
normal_activation_type = ActionActivationType.KEYWORD
# 关键词设置 # 关键词设置
activation_keywords = ["再见", "bye", "88", "拜拜"] activation_keywords = ["再见", "bye", "88", "拜拜"]
@@ -75,8 +75,6 @@ class TimeCommand(BaseCommand):
# === 命令设置(必须填写)=== # === 命令设置(必须填写)===
command_pattern = r"^/time$" # 精确匹配 "/time" 命令 command_pattern = r"^/time$" # 精确匹配 "/time" 命令
command_help = "查询当前时间"
command_examples = ["/time"]
async def execute(self) -> Tuple[bool, str, bool]: async def execute(self) -> Tuple[bool, str, bool]:
"""执行时间查询""" """执行时间查询"""

View File

@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Dict, Tuple, Optional, List from typing import Dict, Tuple, Optional
from src.common.logger import get_logger from src.common.logger import get_logger
from src.plugin_system.base.component_types import CommandInfo, ComponentType from src.plugin_system.base.component_types import CommandInfo, ComponentType
from src.chat.message_receive.message import MessageRecv from src.chat.message_receive.message import MessageRecv
@@ -26,9 +26,6 @@ class BaseCommand(ABC):
# 默认命令设置 # 默认命令设置
command_pattern: str = r"" command_pattern: str = r""
"""命令匹配的正则表达式""" """命令匹配的正则表达式"""
command_help: str = ""
"""命令帮助信息"""
command_examples: List[str] = []
def __init__(self, message: MessageRecv, plugin_config: Optional[dict] = None): def __init__(self, message: MessageRecv, plugin_config: Optional[dict] = None):
"""初始化Command组件 """初始化Command组件
@@ -228,6 +225,4 @@ class BaseCommand(ABC):
component_type=ComponentType.COMMAND, component_type=ComponentType.COMMAND,
description=cls.command_description, description=cls.command_description,
command_pattern=cls.command_pattern, command_pattern=cls.command_pattern,
command_help=cls.command_help,
command_examples=cls.command_examples.copy() if cls.command_examples else [],
) )

View File

@@ -140,13 +140,9 @@ class CommandInfo(ComponentInfo):
"""命令组件信息""" """命令组件信息"""
command_pattern: str = "" # 命令匹配模式(正则表达式) command_pattern: str = "" # 命令匹配模式(正则表达式)
command_help: str = "" # 命令帮助信息
command_examples: List[str] = field(default_factory=list) # 命令使用示例
def __post_init__(self): def __post_init__(self):
super().__post_init__() super().__post_init__()
if self.command_examples is None:
self.command_examples = []
self.component_type = ComponentType.COMMAND self.component_type = ComponentType.COMMAND

View File

@@ -182,17 +182,17 @@ class EventsManager:
async def cancel_handler_tasks(self, handler_name: str) -> None: async def cancel_handler_tasks(self, handler_name: str) -> None:
tasks_to_be_cancelled = self._handler_tasks.get(handler_name, []) tasks_to_be_cancelled = self._handler_tasks.get(handler_name, [])
remaining_tasks = [task for task in tasks_to_be_cancelled if not task.done()] if remaining_tasks := [task for task in tasks_to_be_cancelled if not task.done()]:
for task in remaining_tasks: for task in remaining_tasks:
task.cancel() task.cancel()
try: try:
await asyncio.wait_for(asyncio.gather(*remaining_tasks, return_exceptions=True), timeout=5) await asyncio.wait_for(asyncio.gather(*remaining_tasks, return_exceptions=True), timeout=5)
logger.info(f"已取消事件处理器 {handler_name} 的所有任务") logger.info(f"已取消事件处理器 {handler_name} 的所有任务")
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning(f"取消事件处理器 {handler_name} 的任务超时,开始强制取消") logger.warning(f"取消事件处理器 {handler_name} 的任务超时,开始强制取消")
except Exception as e: except Exception as e:
logger.error(f"取消事件处理器 {handler_name} 的任务时发生异常: {e}") logger.error(f"取消事件处理器 {handler_name} 的任务时发生异常: {e}")
finally: if handler_name in self._handler_tasks:
del self._handler_tasks[handler_name] del self._handler_tasks[handler_name]
async def unregister_event_subscriber(self, handler_name: str) -> bool: async def unregister_event_subscriber(self, handler_name: str) -> bool: