重构:移除Robot中的业务任务实现,统一下沉到插件层

- 删除 Robot 内历史业务方法:新闻/Epic/PDF/秀人下载/排行推送等实现\n- 删除仅服务上述业务方法的群发辅助函数,避免平台层承载业务逻辑\n- 清理对应无用 import 依赖(News、Epic、Sehuatang、Xiuren 业务模块等)\n- 保留系统刚需任务方法(登录巡检、消息计数入库)不变,保持系统层职责单一
This commit is contained in:
liuwei
2026-04-16 16:09:36 +08:00
parent 9652c2594e
commit 547c5533d7

139
robot.py
View File

@@ -1,18 +1,14 @@
# -*- coding: utf-8 -*-
import asyncio
import base64
import threading
import time
import tomllib
from collections import deque
import requests
import toml
from loguru import logger
import wechat_ipad
from base.func_epic import is_friday, get_free
from base.func_news import News
from base.plugin_common.event_system import EventType, EventSystem
from base.plugin_common.message_plugin_interface import MessagePluginInterface
from base.plugin_common.plugin_interface import PluginStatus
@@ -23,20 +19,15 @@ from db.connection import DBConnectionManager
from db.contacts_db import ContactsDBOperator
from db.plugin_schedule_db import PluginScheduleDBOperator
from db.system_job_db import SystemJobDBOperator
from plugins.xiuren_image.meitu_dl import meitu_dowload_pub_pic
from plugins.xiuren_image.shenshi_r15 import run_daily_job
from utils.system_jobs import SystemJobLoader
from utils.email_util import EmailSender
from utils.plugin_schedule_manager import PluginScheduleManager
from utils.revoke.message_auto_revoke import MessageAutoRevoke
from utils.robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
from utils.sehuatang.shehuatang import pdf_file_path
from utils.sehuatang.shehuatang_undetected import pdf_file_path_undetected
from utils.wechat.contact_manager import ContactManager
from utils.wechat.member_monitor import ChatroomMemberMonitor
from utils.wechat.message_to_db import MessageStorage
from wechat_ipad import WechatAPIClient
from wechat_ipad.models.appmsg_xml import LINK_XML_NEWS
from wechat_ipad.models.message import WxMessage, MessageType
# 定义全局信号量,限制最大并发 10
@@ -543,61 +534,6 @@ class Robot:
while True:
time.sleep(1)
async def send_group_txt_message(self, msg: str, feature: Feature):
"""向所有启用了特定功能的群发送文本消息"""
try:
receivers = self.gbm.get_group_list()
if not receivers:
return
for r in receivers:
if self.gbm.get_group_permission(r, feature) == PermissionStatus.ENABLED:
await self.ipad_bot.send_text_message(r, msg)
except Exception as e:
self.LOG.error(f"send_group_txt_message:{feature.description} error{e}")
async def send_group_image_message(self, msg: str, feature: Feature):
"""向所有启用了特定功能的群发送文本消息"""
try:
receivers = self.gbm.get_group_list()
if not receivers:
return
resp = requests.get(msg, timeout=10)
resp.raise_for_status()
base64_str = base64.b64encode(resp.content).decode("utf-8")
for r in receivers:
if self.gbm.get_group_permission(r, feature) == PermissionStatus.ENABLED:
await self.ipad_bot.send_image_message(r, base64_str)
except Exception as e:
self.LOG.error(f"send_group_image_message:{feature.description} error{e}")
async def send_group_xml_message(self, msg: str, feature: Feature):
"""向所有启用了特定功能的群发送文本消息"""
try:
receivers = self.gbm.get_group_list()
if not receivers:
return
for r in receivers:
if self.gbm.get_group_permission(r, feature) == PermissionStatus.ENABLED:
await self.ipad_bot.send_link_xml_message(msg, r)
except Exception as e:
self.LOG.error(f"send_group_xml_message:{feature.description} error{e}")
async def send_group_file_message(self, path: str, feature: Feature):
try:
receivers = self.gbm.get_group_list()
if not receivers:
return
for r in receivers:
if self.gbm.get_group_permission(r, feature) == PermissionStatus.ENABLED:
await self.ipad_bot.send_at_message(r, "98堂 PDF已就绪请手动发送", ["Jyunere"])
# await self.ipad_bot.send_image_message(r, path)
except Exception as e:
self.LOG.error(f"send_group_file_message:{feature.description} error{e}")
async def process_plugin_message(self, msg) -> bool:
"""使用插件处理消息"""
# 获取所有消息处理插件
@@ -794,83 +730,10 @@ class Robot:
except Exception as e:
self.LOG.error(f"login_twice_auto_auth error: {e}")
# ============================================== 业务内容==========================================================
async def news_baidu_report_auto(self) -> None:
try:
news = News().get_baidu_news()
await self.send_group_txt_message(news, Feature.DAILY_NEWS)
path = News().get_news_60s()
await self.send_group_image_message(path, Feature.DAILY_NEWS)
await self.send_group_xml_message(LINK_XML_NEWS, Feature.DAILY_NEWS)
except Exception as e:
self.LOG.error(f"newsBaiduReportAuto error{e}")
async def news_en_report(self, website, sender: str = None) -> None:
try:
news = News().get_eng_news(website)
await self.ipad_bot.send_text_message(sender, news)
except Exception as e:
self.LOG.error(f"newsEnReport error{e}")
# 使用装饰器标记定时任务 星期五 10:30 执行
async def send_epic_free_games(self):
try:
if is_friday():
games = get_free()
await self.send_group_txt_message(games, Feature.EPIC)
except Exception as e:
self.LOG.error(f"sendEpicFreeGames error{e}")
# 使用装饰器标记定时任务
# ============================================== 系统级任务(刚需)==========================================================
async def message_count_to_db(self):
try:
self.message_storage.write_to_db()
except Exception as e:
self.LOG.error(f"write_to_db error{e}")
async def generate_sehuatang_pdf(self):
try:
self.LOG.info("开始生成PDF,generate_sehuatang_pdf")
tag, path = pdf_file_path_undetected()
if not tag:
tag, path = pdf_file_path()
# 暂时只发4K群
await self.send_group_file_message(path, Feature.PDF_CAPABILITY)
except Exception as e:
self.LOG.error(f"generateSehuatangPdf error{e}")
async def xiu_ren_download_task(self):
try:
# 每天下载10组图然后发一个帖子PDF
meitu_dowload_pub_pic()
except Exception as e:
self.LOG.error(f"xiu_ren_download_task error{e}")
async def shen_shi_download_task(self):
try:
# 每天下载10组图然后发一个帖子PDF
run_daily_job()
except Exception as e:
self.LOG.error(f"shen_shi_download_task error{e}")
async def generate_and_send_ranking(self):
try:
receivers = self.gbm.get_group_list()
if not receivers:
return
for r in receivers:
if self.gbm.get_group_permission(r, Feature.DAILY_SUMMARY) == PermissionStatus.ENABLED:
tag, output = await self.message_storage.generate_and_send_ranking(r, self.allContacts)
if tag:
await self.ipad_bot.send_text_message(r, output)
except Exception as e:
self.LOG.error(f"SendRanking error{e}")
async def send_ranking_task(self):
result = await self.ipad_bot.get_chatroom_nickname("Jyunere", "43687793133@chatroom")
self.LOG.info(f"send_ranking_task{result}")