插件定时能力扩展:接入天气/群总结/百科问答/成员画像并补齐周月触发器编辑

- 将 weather、message_summary、game_task、member_context 从硬编码 async_job 注册迁移为插件调度能力(get_schedule_actions/run_scheduled_action)\n- 保持原有默认时间与默认启用行为,新增执行统计结果用于后台日志展示\n- 为群总结与天气推送增加目标群范围适配,支持按后台配置选择 all/白名单/单群执行\n- 成员交互摘要支持日/周/月三类动作接入调度中心,兼容指定群与全量群刷新\n- 后台插件调度页面新增 every_week_time 与 every_month_last_day_time 的编辑支持
This commit is contained in:
liuwei
2026-04-16 15:49:02 +08:00
parent 184999b175
commit 1166323ab5
5 changed files with 333 additions and 66 deletions

View File

@@ -4,7 +4,6 @@ from typing import Dict, Any, List, Optional, Tuple
from loguru import logger
from utils.decorator.async_job import async_job
from base.plugin_common.message_plugin_interface import MessagePluginInterface
from base.plugin_common.plugin_interface import PluginStatus
from utils.decorator.plugin_decorators import plugin_stats_decorator
@@ -60,7 +59,6 @@ class GameTaskPlugin(MessagePluginInterface):
self.LOG = logger
# 注册功能权限
self.feature = self.register_feature()
async_job.at_times(["17:58"])(self.run_random_task_assignment)
def initialize(self, context: Dict[str, Any]) -> bool:
"""初始化插件"""
@@ -185,6 +183,43 @@ class GameTaskPlugin(MessagePluginInterface):
self.LOG.error(f"处理消息出错: {e}")
return False, f"处理出错: {e}"
def get_schedule_actions(self) -> List[Dict[str, Any]]:
"""声明百科问答插件支持的可调度动作。"""
return [
{
"action_key": "random_task_assignment",
"name": "群随机发题",
"description": "在配置时间给目标群随机发放一条百科问答任务",
"trigger_type": "at_times",
"trigger_config": {"time_list": ["17:58"]},
"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 != "random_task_assignment":
return {
"success": False,
"summary": f"不支持的动作: {action_key}",
"detail": {"action_key": action_key},
}
target_groups = [str(g).strip() for g in (context.get("target_groups") or []) if str(g).strip()]
result = await self.run_random_task_assignment(target_groups=target_groups)
return {
"success": bool(result.get("failed_groups", 0) == 0),
"summary": (
f"发题完成: 候选{result.get('candidate_groups', 0)}群,"
f"成功{result.get('success_groups', 0)}群,失败{result.get('failed_groups', 0)}"
),
"detail": result,
}
async def _handle_join_game(self, sender: str, roomid: str, wx_nick_name: str) -> None:
"""处理加入游戏请求"""
try:
@@ -535,20 +570,34 @@ class GameTaskPlugin(MessagePluginInterface):
sender
)
async def run_random_task_assignment(self) -> None:
"""定时任务:整点触发排除23:00-08:00"""
async def run_random_task_assignment(self, target_groups: Optional[List[str]] = None) -> Dict[str, int]:
"""定时任务:随机发题,排除 23:00-08:00
Args:
target_groups: 指定目标群列表;为空时按原逻辑扫描全部游戏群。
Returns:
dict: 执行统计信息。
"""
current_hour = datetime.now().hour
if current_hour >= 23 or current_hour < 9:
self.LOG.info(f"当前时间 {current_hour}:00 在23:00-08:00区间跳过任务发放")
return
return {"candidate_groups": 0, "success_groups": 0, "failed_groups": 0}
try:
# 获取所有群聊
groups = self.encyclopedia_db.get_all_groups()
target_group_set = {g for g in (target_groups or []) if g}
candidate_groups = 0
success_groups = 0
failed_groups = 0
for group in groups:
if target_group_set and group not in target_group_set:
continue
# 检查权限
if GroupBotManager.get_group_permission(group,self.feature) == PermissionStatus.DISABLED:
continue
candidate_groups += 1
# 获取群内所有玩家
players = self.encyclopedia_db.get_all_players_in_group(group)
@@ -583,8 +632,17 @@ class GameTaskPlugin(MessagePluginInterface):
f"🌼 积分:{score}\n"
f"🌈 抢答格式:/a {active_task_id} 答案",
[holder_id])
success_groups += 1
else:
failed_groups += 1
return {
"candidate_groups": candidate_groups,
"success_groups": success_groups,
"failed_groups": failed_groups,
}
except Exception as e:
self.LOG.error(f"定时任务出错: {e}")
return {"candidate_groups": 0, "success_groups": 0, "failed_groups": 1}
# 解析JSON
def extract_content(self, data_string):