调整代码

This commit is contained in:
liuwei
2025-04-23 16:11:06 +08:00
parent b919a3834e
commit fc757151e4
11 changed files with 345 additions and 214 deletions

View File

@@ -152,9 +152,32 @@ class Robot(Job):
# 设置ROBOT功能为启用状态
GroupBotManager.set_group_permission(msg.roomid, Feature.ROBOT, PermissionStatus.ENABLED)
# 更新联系人信息
# 群第一次加入机器人管理,自动添加并开启机器人功能,需要进行群成员信息初始化。请完成写入数据库,并更新联系人信息
except Exception as e:
self.LOG.error(f"加入新群,自动添加并开启机器人功能 error: {e}")
# 如果用户信息缓存里面没有这个用户昵称,则添加用户信息,并且维护该用户信息
# 以 wxid 作为唯一标识
try:
if msg.from_group():
wxid = msg.sender
if wxid and wxid not in self.allContacts:
# 添加到数据库
# 这里假设 contacts_db 有 save_contact_info 方法,参数为 dict
resp = self.client.get_chatroom_member_detail(self.app_id, msg.roomid, [wxid])
resp_obj = json_to_object(resp)
infos = resp_obj.data
for info in infos:
self.LOG.info(f"已添加新用户信息到数据库: {wxid}")
# 更新缓存
self.allContacts[wxid] = info.get("nickName", "wxid")
self.contact_manager.set_contacts(self.allContacts)
self.LOG.info(f"已维护新用户信息到缓存: {wxid}")
self.contacts_db.save_chatroom_member_detail(msg.roomid, infos)
except Exception as e:
self.LOG.error(f"添加新用户信息到数据库失败: {e}")
# 发布消息接收事件
self.event_system.publish(EventType.MESSAGE_RECEIVED, {"message": msg})
@@ -206,18 +229,18 @@ class Robot(Job):
elif msg.msg_type == MessageType.TEXT: # 文本消息
# 让配置加载更灵活,自己可以更新配置。也可以利用定时任务更新。
if msg.from_self():
if msg.content == "^更新$":
if msg.content.clean_content == "^更新$":
self.config.reload()
self.gbm.load_local_cache()
self.LOG.info("已更新")
if msg.content == "今日百度新闻":
if msg.content.clean_content == "今日百度新闻":
self.news_baidu_report()
if msg.content == "TO_DB":
if msg.content.clean_content == "TO_DB":
self.message_count_to_db()
if msg.content == "PDF":
if msg.content.clean_content == "PDF":
self.generate_sehuatang_pdf()
if msg.content.raw_content.startswith("清除群-"):
self.gbm.handle_command(msg.roomid, msg.content)
self.gbm.handle_command(msg.roomid, msg.content.clean_content)
else:
self.toChitchat(msg) # 闲聊
@@ -281,7 +304,7 @@ class Robot(Job):
# 转换WxMessage为插件可处理的格式
plugin_msg = {
"type": msg.msg_type,
"content": msg.content,
"content": msg.content.clean_content,
"sender": msg.sender,
"roomid": msg.roomid if msg.from_group() else "",
"xml": msg.content.xml_content,
@@ -402,18 +425,23 @@ class Robot(Job):
except Exception as e:
self.LOG.error(f"xiu_ren_pdf_send error{e}")
# 本逻辑主要解决加载联系人信息的问题,只从数据库里面提取,不完成下载行为。
def get_all_contacts(self) -> dict:
"""获取所有联系人信息并返回字典格式 {wxid: nickname}"""
# 从数据库提取信息,如果数据库没内容,则完成第一次初始化。
try:
# 先尝试从数据库获取联系人信息
contacts_dict = self.contacts_db.get_all_contacts()
# 获取群成员列表
return contacts_dict
# 如果数据库中有联系人信息,直接返回
if contacts_dict:
self.LOG.info(f"从数据库成功获取了 {len(contacts_dict)} 个联系人信息")
return contacts_dict
except Exception as e:
self.LOG.error(f"获取联系人信息失败: {e}")
return {}
def sync_all_contacts(self):
"""同步所有联系人信息"""
try:
# 数据库中没有联系人信息,需要初始化
self.LOG.info("数据库中没有联系人信息,开始初始化...")