插件化项目优化,支持将代码改造为插件,支持自动加载
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import importlib
|
||||
import inspect
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import xml.etree.ElementTree as ET
|
||||
from queue import Empty
|
||||
from threading import Thread
|
||||
from datetime import datetime, timedelta
|
||||
import random
|
||||
from typing import Optional
|
||||
|
||||
import redis
|
||||
|
||||
@@ -40,18 +45,19 @@ from message_sign.main import SignInSystem
|
||||
from message_storage.message_to_db import MessageStorage
|
||||
from message_summary.message_summary_dify import message_summary_dify
|
||||
from music.bot_music import BotMusic
|
||||
from plugin_common.event_system import EventType, EventSystem
|
||||
from plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
from plugin_common.plugin_interface import PluginInterface, PluginStatus
|
||||
from plugin_common.plugin_manager import PluginManager
|
||||
from plugin_common.plugin_registry import PluginRegistry
|
||||
from point_trade.main import PointTrade
|
||||
from robot_cmd.robot_command import GroupBotManager
|
||||
from job_mgmt import Job
|
||||
from robot_cmd.robot_command import Feature
|
||||
from robot_cmd.robot_command import PermissionStatus
|
||||
import mysql.connector.pooling
|
||||
|
||||
__version__ = "39.2.4.0"
|
||||
|
||||
from message_report.process_message import process_message
|
||||
from message_report.write_db import write_to_db, generate_and_send_ranking
|
||||
from message_summary.message_summary_4o import message_summary
|
||||
from sehuatang.shehuatang import pdf_file_path
|
||||
from xiuren.main import Xiuren
|
||||
from xiuren.meitu_dl import meitu_dowload_pic, meitu_dowload_pub_pic, meitu_dowload_heisi_pic
|
||||
@@ -88,6 +94,33 @@ class Robot(Job):
|
||||
self.message_util = MessageUtil(wcf, self.allContacts)
|
||||
self.groups = {} # 存储按group_id分组的消息列表,每个group_id最多保留10条消息
|
||||
GroupBotManager.load_local_cache()
|
||||
|
||||
# 初始化插件系统
|
||||
self.LOG.info("开始初始化插件系统...")
|
||||
self.plugin_registry = PluginRegistry()
|
||||
self.event_system = EventSystem()
|
||||
self.plugin_modules = {} # 存储已加载的插件模块
|
||||
self.plugins = {} # 存储已加载的插件实例
|
||||
|
||||
# 设置插件系统上下文
|
||||
self.system_context = {
|
||||
"config": config,
|
||||
"wcf": wcf,
|
||||
"event_system": self.event_system,
|
||||
"plugin_registry": self.plugin_registry,
|
||||
"db_pool": self.db_pool,
|
||||
"redis_pool": self.redis_pool,
|
||||
"all_contacts": self.allContacts,
|
||||
"message_util": self.message_util
|
||||
}
|
||||
|
||||
self.plugin_manager = PluginManager(plugin_dir=getattr(self.config, "plugin_dir", "plugins"))
|
||||
self.plugin_manager.set_system_context(self.system_context)
|
||||
self.plugins = self.plugin_manager.load_all_plugins()
|
||||
|
||||
# 加载插件
|
||||
self.LOG.info("插件系统初始化完成")
|
||||
|
||||
# 消息存档模块初始化,自动完成入库动作
|
||||
self.message_storage = MessageStorage()
|
||||
# 权限模块加载
|
||||
@@ -288,7 +321,14 @@ class Robot(Job):
|
||||
receivers = msg.roomid
|
||||
self.sendTextMsg(content, receivers, msg.sender)
|
||||
"""
|
||||
# 发布消息接收事件
|
||||
self.event_system.publish(EventType.MESSAGE_RECEIVED, {"message": msg})
|
||||
|
||||
# 尝试使用插件处理消息
|
||||
if self.process_plugin_message(msg):
|
||||
return
|
||||
|
||||
# 如果没有插件处理,使用原有逻辑处理消息
|
||||
# 群聊消息
|
||||
if msg.from_group():
|
||||
# 调用统计逻辑进行聊天数据统计:
|
||||
@@ -583,6 +623,46 @@ class Robot(Job):
|
||||
except Exception as e:
|
||||
self.LOG.error(f"revoke_messages error:{e}")
|
||||
|
||||
def process_plugin_message(self, msg: WxMsg) -> bool:
|
||||
"""使用插件处理消息"""
|
||||
# 获取所有消息处理插件
|
||||
message_plugins = self.plugin_registry.get_plugins_by_type(MessagePluginInterface)
|
||||
|
||||
# 依次尝试处理消息
|
||||
for plugin in message_plugins:
|
||||
if plugin.status != PluginStatus.RUNNING:
|
||||
continue
|
||||
|
||||
try:
|
||||
# 转换WxMsg为插件可处理的格式
|
||||
plugin_msg = {
|
||||
"type": msg.type,
|
||||
"content": msg.content,
|
||||
"sender": msg.sender,
|
||||
"roomid": msg.roomid if msg.from_group() else "",
|
||||
"xml": msg.xml,
|
||||
"is_at": msg.is_at(self.wxid),
|
||||
"timestamp": time.time(),
|
||||
"wcf": self.wcf, # 提供wcf对象,让插件可以直接发送消息
|
||||
"message_util": self.message_util # 提供消息工具类
|
||||
}
|
||||
|
||||
# 检查插件是否可以处理该消息
|
||||
if plugin.can_process(plugin_msg):
|
||||
processed, _ = plugin.process_message(plugin_msg)
|
||||
if processed:
|
||||
# 发布消息处理事件
|
||||
self.event_system.publish(EventType.MESSAGE_PROCESSED, {
|
||||
"message": msg,
|
||||
"plugin": plugin.name
|
||||
})
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
self.LOG.error(f"插件 {plugin.name} 处理消息失败: {e}")
|
||||
|
||||
return False
|
||||
|
||||
# ============================================== 业务内容==========================================================
|
||||
|
||||
def news_baidu_report_auto(self) -> None:
|
||||
|
||||
Reference in New Issue
Block a user