Files
abot/plugins/daily_ranking/main.py
liuwei 9652c2594e 系统业务任务插件化迁移:下沉7项非刚需任务并接入平滑迁移
- 系统任务保留刚需三项:登录巡检、消息计数入库、媒体补偿处理;移除新闻/Epic/排行/PDF/秀人维护等业务型系统任务定义\n- 新增 daily_news、epic_free、daily_ranking、sehuatang_push 四个插件,将原系统业务任务改为插件可调度动作\n- 扩展 xiuren_image 插件调度动作,新增秀人下载、绅士R15下载、图片缓存更新三项维护任务\n- 新增系统任务到插件任务的幂等迁移逻辑:按旧 job_key 映射到插件 action,同步 trigger_type/trigger_config/enabled,并通过 payload 标记防止反复覆盖\n- 在 Robot 启动流程中接入迁移执行与重载,并清理已迁移的历史系统任务记录,避免后台双份维护\n- 扩展插件调度数据库操作:支持按 plugin_name + action_key 精确查询,便于迁移与对账
2026-04-16 16:05:59 +08:00

124 lines
4.2 KiB
Python

# -*- coding: utf-8 -*-
from typing import Any, Dict, List, Optional, Tuple
from base.plugin_common.message_plugin_interface import MessagePluginInterface
from base.plugin_common.plugin_interface import PluginStatus
from utils.robot_cmd.robot_command import GroupBotManager
from utils.wechat.message_to_db import MessageStorage
class DailyRankingPlugin(MessagePluginInterface):
"""每日群消息排行推送插件。"""
FEATURE_KEY = "DAILY_SUMMARY"
FEATURE_DESCRIPTION = "🕤 每日群发言总结 [每日9:30定时发送]"
@property
def name(self) -> str:
return "每日排行"
@property
def version(self) -> str:
return "1.0.0"
@property
def description(self) -> str:
return "将群消息排行推送从系统任务迁移到插件任务。"
@property
def author(self) -> str:
return "ABOT Team"
@property
def commands(self) -> List[str]:
return []
@property
def feature_key(self) -> Optional[str]:
return self.FEATURE_KEY
@property
def feature_description(self) -> Optional[str]:
return self.FEATURE_DESCRIPTION
def __init__(self):
super().__init__()
self.feature = self.register_feature()
self.message_storage: Optional[MessageStorage] = None
def initialize(self, context: Dict[str, Any]) -> bool:
# 与历史系统逻辑保持一致,直接复用 MessageStorage 的排行生成能力。
self.message_storage = MessageStorage()
return True
def start(self) -> bool:
self.status = PluginStatus.RUNNING
return True
def stop(self) -> bool:
self.status = PluginStatus.STOPPED
return True
def can_process(self, message: Dict[str, Any]) -> bool:
return False
async def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
return False, None
def get_schedule_actions(self) -> List[Dict[str, Any]]:
return [
{
"action_key": "daily_message_ranking_push",
"name": "群消息排行推送",
"description": "每天生成并发送群消息发言排行",
"trigger_type": "at_times",
"trigger_config": {"time_list": ["09:30"]},
"target_scope": "all_enabled_groups",
"target_config": {},
"payload": {},
"default_enabled": True,
}
]
async def run_scheduled_action(self, action_key: str, context: Dict[str, Any]) -> Dict[str, Any]:
if action_key != "daily_message_ranking_push":
return {
"success": False,
"summary": f"不支持的动作: {action_key}",
"detail": {"action_key": action_key},
}
if not self.bot:
return {"success": False, "summary": "bot 未注入", "detail": {}}
if not self.message_storage:
return {"success": False, "summary": "message_storage 未初始化", "detail": {}}
target_groups = [str(g).strip() for g in (context.get("target_groups") or []) if str(g).strip()]
if not target_groups:
target_groups = [
gid for gid in GroupBotManager.get_group_list()
if GroupBotManager.get_group_permission(gid, self.feature).value == "enabled"
]
if not target_groups:
return {"success": False, "summary": "没有可推送目标群", "detail": {"target_count": 0}}
success_groups = []
failed_groups = {}
for gid in target_groups:
try:
ok, text = await self.message_storage.generate_and_send_ranking(gid, {})
if ok and text:
await self.bot.send_text_message(gid, text)
success_groups.append(gid)
except Exception as e:
failed_groups[gid] = str(e)
return {
"success": len(failed_groups) == 0,
"summary": f"每日排行推送完成: 成功{len(success_groups)}群, 失败{len(failed_groups)}",
"detail": {
"target_count": len(target_groups),
"success_groups": success_groups,
"failed_groups": failed_groups,
},
}