加入指令数据统计,指令看板内容
This commit is contained in:
1
event_system/__init__.py
Normal file
1
event_system/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# 事件系统包初始化文件
|
||||
59
event_system/event_manager.py
Normal file
59
event_system/event_manager.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import logging
|
||||
from typing import Dict, List, Type, Callable, Any
|
||||
from threading import Lock
|
||||
|
||||
class Event:
|
||||
"""事件基类"""
|
||||
def __init__(self, **kwargs):
|
||||
for key, value in kwargs.items():
|
||||
setattr(self, key, value)
|
||||
|
||||
class EventManager:
|
||||
"""事件管理器,单例模式"""
|
||||
_instance = None
|
||||
_lock = Lock()
|
||||
|
||||
@classmethod
|
||||
def get_instance(cls):
|
||||
"""获取事件管理器实例"""
|
||||
if cls._instance is None:
|
||||
with cls._lock:
|
||||
if cls._instance is None:
|
||||
cls._instance = cls()
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
if EventManager._instance is not None:
|
||||
raise RuntimeError("EventManager 是单例类,请使用 get_instance() 方法获取实例")
|
||||
|
||||
self.handlers: Dict[Type[Event], List[Callable]] = {}
|
||||
self.logger = logging.getLogger("EventManager")
|
||||
|
||||
def register(self, event_type: Type[Event], handler: Callable) -> None:
|
||||
"""注册事件处理器"""
|
||||
if event_type not in self.handlers:
|
||||
self.handlers[event_type] = []
|
||||
|
||||
if handler not in self.handlers[event_type]:
|
||||
self.handlers[event_type].append(handler)
|
||||
self.logger.debug(f"注册事件处理器: {event_type.__name__} -> {handler.__name__}")
|
||||
|
||||
def unregister(self, event_type: Type[Event], handler: Callable) -> None:
|
||||
"""取消注册事件处理器"""
|
||||
if event_type in self.handlers and handler in self.handlers[event_type]:
|
||||
self.handlers[event_type].remove(handler)
|
||||
self.logger.debug(f"取消注册事件处理器: {event_type.__name__} -> {handler.__name__}")
|
||||
|
||||
def publish(self, event_type: Type[Event], event_data: Dict[str, Any] = None) -> None:
|
||||
"""发布事件"""
|
||||
if event_data is None:
|
||||
event_data = {}
|
||||
|
||||
event = event_type(**event_data)
|
||||
|
||||
if event_type in self.handlers:
|
||||
for handler in self.handlers[event_type]:
|
||||
try:
|
||||
handler(event)
|
||||
except Exception as e:
|
||||
self.logger.error(f"事件处理器 {handler.__name__} 处理 {event_type.__name__} 事件出错: {e}")
|
||||
74
event_system/events/plugin_events.py
Normal file
74
event_system/events/plugin_events.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from event_system.event_manager import Event
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
|
||||
class PluginCallStartEvent(Event):
|
||||
"""插件调用开始事件"""
|
||||
def __init__(self, plugin_name: str, command: str, user_id: str,
|
||||
group_id: Optional[str] = None, **kwargs):
|
||||
"""
|
||||
Args:
|
||||
plugin_name: 插件名称
|
||||
command: 触发的命令
|
||||
user_id: 用户ID
|
||||
group_id: 群组ID,私聊为None
|
||||
"""
|
||||
super().__init__(
|
||||
plugin_name=plugin_name,
|
||||
command=command,
|
||||
user_id=user_id,
|
||||
group_id=group_id,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
|
||||
class PluginCallEndEvent(Event):
|
||||
"""插件调用结束事件"""
|
||||
def __init__(self, plugin_name: str, command: str, user_id: str,
|
||||
group_id: Optional[str], success: bool,
|
||||
process_time_ms: float, result: Any = None, **kwargs):
|
||||
"""
|
||||
Args:
|
||||
plugin_name: 插件名称
|
||||
command: 触发的命令
|
||||
user_id: 用户ID
|
||||
group_id: 群组ID,私聊为None
|
||||
success: 是否调用成功
|
||||
process_time_ms: 处理时间(毫秒)
|
||||
result: 处理结果
|
||||
"""
|
||||
super().__init__(
|
||||
plugin_name=plugin_name,
|
||||
command=command,
|
||||
user_id=user_id,
|
||||
group_id=group_id,
|
||||
success=success,
|
||||
process_time_ms=process_time_ms,
|
||||
result=result,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
|
||||
class PluginErrorEvent(Event):
|
||||
"""插件错误事件"""
|
||||
def __init__(self, plugin_name: str, command: str, user_id: str,
|
||||
group_id: Optional[str], error_message: str,
|
||||
stack_trace: Optional[str] = None, **kwargs):
|
||||
"""
|
||||
Args:
|
||||
plugin_name: 插件名称
|
||||
command: 触发的命令
|
||||
user_id: 用户ID
|
||||
group_id: 群组ID,私聊为None
|
||||
error_message: 错误信息
|
||||
stack_trace: 堆栈跟踪
|
||||
"""
|
||||
super().__init__(
|
||||
plugin_name=plugin_name,
|
||||
command=command,
|
||||
user_id=user_id,
|
||||
group_id=group_id,
|
||||
error_message=error_message,
|
||||
stack_trace=stack_trace,
|
||||
**kwargs
|
||||
)
|
||||
40
event_system/events/stats_events.py
Normal file
40
event_system/events/stats_events.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
|
||||
from event_system.event import Event
|
||||
|
||||
|
||||
@dataclass
|
||||
class PluginCallStartEvent(Event):
|
||||
"""插件调用开始事件"""
|
||||
plugin_name: str
|
||||
command: str
|
||||
user_id: str
|
||||
group_id: Optional[str]
|
||||
start_time: datetime
|
||||
|
||||
|
||||
@dataclass
|
||||
class PluginCallEndEvent(Event):
|
||||
"""插件调用结束事件"""
|
||||
plugin_name: str
|
||||
command: str
|
||||
user_id: str
|
||||
group_id: Optional[str]
|
||||
start_time: datetime
|
||||
end_time: datetime
|
||||
success: bool
|
||||
response: Optional[str]
|
||||
|
||||
|
||||
@dataclass
|
||||
class PluginCallErrorEvent(Event):
|
||||
"""插件调用错误事件"""
|
||||
plugin_name: str
|
||||
command: str
|
||||
user_id: str
|
||||
group_id: Optional[str]
|
||||
start_time: datetime
|
||||
error_message: str
|
||||
stack_trace: Optional[str]
|
||||
Reference in New Issue
Block a user