加入bot,自动注入内容,在项目启动完成之后,给每个插件注入bot
This commit is contained in:
2
main.py
2
main.py
@@ -110,7 +110,7 @@ def jobs(robot: Robot):
|
||||
# ✅ 每 3 小时登录验证
|
||||
@async_job.at_times(["14:43"])
|
||||
async def login_check_job():
|
||||
robot.login_twice_auto_auth()
|
||||
await robot.login_twice_auto_auth()
|
||||
|
||||
@async_job.at_times(["11:15"])
|
||||
async def update_image_cache_job():
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from typing import Dict, Any, Tuple, Optional, List
|
||||
from plugin_common.plugin_interface import PluginInterface
|
||||
from wechat_ipad import WechatAPIClient
|
||||
|
||||
|
||||
class MessagePluginInterface(PluginInterface):
|
||||
@@ -15,6 +16,10 @@ class MessagePluginInterface(PluginInterface):
|
||||
"""支持的命令列表"""
|
||||
return []
|
||||
|
||||
# 需要完成jobs 的业务,所以完成bot注入
|
||||
def set_bot(self, bot: WechatAPIClient) -> None:
|
||||
self.bot: WechatAPIClient = bot
|
||||
|
||||
def can_process(self, message: Dict[str, Any]) -> bool:
|
||||
"""
|
||||
检查插件是否可以处理该消息
|
||||
|
||||
@@ -64,6 +64,7 @@ class PluginInterface(ABC):
|
||||
self._plugin_path = ""
|
||||
# 初始化日志记录器
|
||||
self.LOG = logger
|
||||
self.bot = None
|
||||
|
||||
def load_config(self) -> bool:
|
||||
"""
|
||||
|
||||
@@ -11,6 +11,7 @@ from plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
from plugin_common.scheduled_plugin_interface import ScheduledPluginInterface
|
||||
from plugin_common.plugin_registry import PluginRegistry
|
||||
from plugin_common.event_system import EventSystem, EventType
|
||||
from wechat_ipad import WechatAPIClient
|
||||
|
||||
|
||||
class PluginManager:
|
||||
@@ -40,7 +41,6 @@ class PluginManager:
|
||||
cls._instance._initialized = False
|
||||
return cls._instance
|
||||
|
||||
|
||||
def __init__(self, plugin_dir: str = "plugins"):
|
||||
"""
|
||||
初始化插件管理器
|
||||
@@ -572,3 +572,13 @@ class PluginManager:
|
||||
self.LOG.warning(f"模块映射: {self.module_to_display}")
|
||||
|
||||
return None, None
|
||||
|
||||
def inject_bot(self, bot: WechatAPIClient):
|
||||
self.system_context["bot"] = bot
|
||||
for name, plugin in self.plugins.items():
|
||||
if hasattr(plugin, "set_bot"):
|
||||
try:
|
||||
plugin.set_bot(bot)
|
||||
self.LOG.info(f"已成功注入 bot 到插件 {name}")
|
||||
except Exception as e:
|
||||
self.LOG.error(f"注入 bot 到插件 {name} 失败: {e}")
|
||||
|
||||
@@ -46,7 +46,6 @@ class GameTaskPlugin(MessagePluginInterface):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.LOG = logger
|
||||
self.bot: WechatAPIClient = None
|
||||
async_job.at_times(["14:43"])(self.run_random_task_assignment)
|
||||
|
||||
def initialize(self, context: Dict[str, Any]) -> bool:
|
||||
@@ -117,7 +116,6 @@ class GameTaskPlugin(MessagePluginInterface):
|
||||
gbm: GroupBotManager = message.get("gbm")
|
||||
all_contacts = message.get("all_contacts", {})
|
||||
|
||||
self.bot: WechatAPIClient = message.get("bot")
|
||||
self.LOG.debug(f"插件执行: {self.name}:{content}")
|
||||
|
||||
# 检查权限
|
||||
|
||||
20
robot.py
20
robot.py
@@ -214,7 +214,7 @@ class Robot:
|
||||
self.LOG.error(f"获取新消息失败 {e}")
|
||||
if "用户可能退出" in str(e):
|
||||
self.LOG.error(f"用户可能退出: {e}")
|
||||
self.login_twice_auto_auth()
|
||||
await self.login_twice_auto_auth()
|
||||
await asyncio.sleep(5)
|
||||
continue
|
||||
|
||||
@@ -296,7 +296,8 @@ class Robot:
|
||||
self.nickname = self.ipad_bot.nickname
|
||||
self.alias = self.ipad_bot.alias
|
||||
self.phone = self.ipad_bot.phone
|
||||
|
||||
# 注入加载完成的bot
|
||||
self.plugin_manager.inject_bot(self.ipad_bot)
|
||||
self.LOG.info(
|
||||
f"wechat_ipad登录账号信息: wxid: {self.wxid} 昵称: {self.nickname} 微信号: {self.alias} 手机号: {self.phone}")
|
||||
break
|
||||
@@ -315,7 +316,7 @@ class Robot:
|
||||
self.LOG.error(f"wechat_ipad heartbeat: {e}")
|
||||
if "用户可能退出" in str(e):
|
||||
self.LOG.error(f"用户可能退出: {e}")
|
||||
self.login_twice_auto_auth()
|
||||
await self.login_twice_auto_auth()
|
||||
await asyncio.sleep(60)
|
||||
|
||||
async def _heartbeat_task_long(self):
|
||||
@@ -332,7 +333,7 @@ class Robot:
|
||||
self.LOG.error(f"wechat_ipad heartbeat long: {e}")
|
||||
if "用户可能退出" in str(e):
|
||||
self.LOG.error(f"用户可能退出: {e}")
|
||||
self.login_twice_auto_auth()
|
||||
await self.login_twice_auto_auth()
|
||||
await asyncio.sleep(120)
|
||||
|
||||
async def _process_ipad_message(self, message: WxMessage):
|
||||
@@ -412,9 +413,9 @@ class Robot:
|
||||
self.gbm.load_local_cache()
|
||||
await self.ipad_bot.send_text_message("filehelper", "已更新")
|
||||
elif content.clean_content == "PDF":
|
||||
self.generate_sehuatang_pdf()
|
||||
await self.generate_sehuatang_pdf()
|
||||
elif content.clean_content == "排行榜":
|
||||
self.generate_and_send_ranking()
|
||||
await self.generate_and_send_ranking()
|
||||
|
||||
if is_group:
|
||||
self.LOG.debug(f"入库和记录群消息: {message}")
|
||||
@@ -614,10 +615,10 @@ class Robot:
|
||||
self.contacts_db.delete_chatroom_all_info(group_id)
|
||||
self.LOG.info("联系人信息刷新完成")
|
||||
|
||||
def login_twice_auto_auth(self) -> None:
|
||||
async def login_twice_auto_auth(self) -> None:
|
||||
try:
|
||||
self.LOG.info(f"定时进行二次登录动作")
|
||||
resp = self.ipad_bot.twice_auto_auth()
|
||||
resp = await self.ipad_bot.twice_auto_auth()
|
||||
if resp:
|
||||
self.LOG.info(f"定时二次登录成功!")
|
||||
if self.ipad_running:
|
||||
@@ -632,7 +633,6 @@ class Robot:
|
||||
|
||||
# ============================================== 业务内容==========================================================
|
||||
|
||||
|
||||
async def news_baidu_report_auto(self) -> None:
|
||||
try:
|
||||
news = News().get_baidu_news()
|
||||
@@ -648,6 +648,7 @@ class Robot:
|
||||
self.LOG.error(f"newsEnReport error:{e}")
|
||||
|
||||
# 使用装饰器标记定时任务 星期五 10:30 执行
|
||||
|
||||
async def send_epic_free_games(self):
|
||||
try:
|
||||
if is_friday():
|
||||
@@ -657,6 +658,7 @@ class Robot:
|
||||
self.LOG.error(f"sendEpicFreeGames error:{e}")
|
||||
|
||||
# 使用装饰器标记定时任务
|
||||
|
||||
async def message_count_to_db(self):
|
||||
try:
|
||||
self.message_storage.write_to_db()
|
||||
|
||||
Reference in New Issue
Block a user