尝试加入权限内容。

This commit is contained in:
liuwei
2025-03-18 14:51:37 +08:00
parent bcca2dab28
commit adfa3505a5
3 changed files with 33 additions and 112 deletions

View File

@@ -24,7 +24,6 @@ from wcferry import Wcf, WxMsg
from base.func_bard import BardAssistant
from base.func_chatglm import ChatGLM
from base.func_chatgpt import ChatGPT
from base.func_chengyu import cy
from base.func_news import News
from base.func_tigerbot import TigerBot
from base.func_xinghuo_web import XinghuoWeb
@@ -95,6 +94,8 @@ class Robot(Job):
self.groups = {} # 存储按group_id分组的消息列表每个group_id最多保留10条消息
GroupBotManager.load_local_cache()
# 权限模块加载
self.gbm = GroupBotManager()
# 初始化插件系统
self.LOG.info("开始初始化插件系统...")
self.plugin_registry = PluginRegistry()
@@ -123,8 +124,6 @@ class Robot(Job):
# 消息存档模块初始化,自动完成入库动作
self.message_storage = MessageStorage()
# 权限模块加载
self.gbm = GroupBotManager()
# 群成员变更模块加载
self.gmc = GroupMemberChange(wcf, self.redis_pool)
# 点歌模块加载
@@ -203,33 +202,6 @@ class Robot(Job):
"""
return self.toChitchat(msg)
def toChengyu(self, msg: WxMsg) -> bool:
"""
处理成语查询/接龙消息
:param msg: 微信消息结构
:return: 处理状态,`True` 成功,`False` 失败
"""
status = False
texts = re.findall(r"^([#|?|])(.*)$", msg.content)
# [('#', '天天向上')]
if texts:
flag = texts[0][0]
text = texts[0][1]
if flag == "#": # 接龙
if cy.isChengyu(text):
rsp = cy.getNext(text)
if rsp:
self.send_text_msg(rsp, msg.roomid)
status = True
elif flag in ["?", ""]: # 查词
if cy.isChengyu(text):
rsp = cy.getMeaning(text)
if rsp:
self.send_text_msg(rsp, msg.roomid)
status = True
return status
def toChitchat(self, msg: WxMsg) -> bool:
"""闲聊,接入 ChatGPT
"""
@@ -344,18 +316,6 @@ class Robot(Job):
except Exception as e:
self.LOG.error(f"archive_message error: {e}")
# 记录在群里发的最新消息,可以通过撤回指令撤回
try:
if msg.from_self():
self.revoke_receive_message(msg)
rsp = self.gbm.handle_command(msg.roomid, msg.content)
# 不在群里发送,防止被骚扰
if rsp is not None:
self.send_text_msg(rsp, msg.sender)
return
except Exception as e:
self.LOG.error(f"revoke_receive_message error: {e}")
# 兼容不@ 直接/触发指令,回答问题。
try:
if msg.content.startswith("/"):
@@ -589,40 +549,6 @@ class Robot(Job):
except Exception as e:
self.LOG.error(f"send_group_file_message:{feature.description} error{e}")
# 自动撤回功能块
def revoke_receive_message(self, msg: WxMsg):
try:
group_id = msg.roomid
# 如果该group_id没有记录过初始化一个列表
if group_id not in self.groups:
self.groups[group_id] = []
# 将消息ID添加到对应group_id的消息列表中最多保留10条消息
if len(self.groups[group_id]) >= 10:
self.groups[group_id].pop(0) # 超过10条时移除最早的消息
self.groups[group_id].append(msg.id)
self.LOG.info(f"Message revoke received for group {group_id}: {msg.id}")
except Exception as e:
self.LOG.error(f"Revoke_receive_message error{e}")
def revoke_messages(self, group_id):
try:
# 如果没有该group_id直接返回
if group_id not in self.groups:
self.LOG.debug(f"No messages found for group {group_id}.")
return
# 按照逆序撤回该组的消息
for msg_id in reversed(self.groups[group_id]):
self.wcf.revoke_msg(msg_id) # 假设调用撤回方法
self.LOG.info(f"Message {msg_id} recalled from group {group_id}.")
self.groups[group_id].remove(msg_id) # 撤回后移除该消息ID
self.LOG.debug(f"Message {msg_id} removed from group {group_id}.")
except Exception as e:
self.LOG.error(f"revoke_messages error{e}")
def process_plugin_message(self, msg: WxMsg) -> bool:
"""使用插件处理消息"""
# 获取所有消息处理插件
@@ -644,7 +570,8 @@ class Robot(Job):
"is_at": msg.is_at(self.wxid),
"timestamp": time.time(),
"wcf": self.wcf, # 提供wcf对象让插件可以直接发送消息
"message_util": self.message_util # 提供消息工具类
"message_util": self.message_util, # 提供消息工具类
"gbm": self.gbm # 每次从程序变量中取,保证最新
}
# 检查插件是否可以处理该消息
@@ -752,8 +679,6 @@ class Robot(Job):
try:
# 每天下载10组图然后发一个帖子PDF
meitu_dowload_pub_pic()
# meitu_dowload_heisi_pic()
except Exception as e:
self.LOG.error(f"xiu_ren_download_task error{e}")
@@ -762,8 +687,5 @@ class Robot(Job):
pub_path = generate_pdf_from_images("xiuren")
self.wcf.send_file(pub_path, "45317011307@chatroom")
# heisi_path = generate_pdf_from_images("xiuren/heisi")
# self.wcf.send_file(heisi_path, "45317011307@chatroom")
except Exception as e:
self.LOG.error(f"xiu_ren_pdf_send error{e}")