调整群检查逻辑
This commit is contained in:
@@ -9,6 +9,7 @@ from wcferry import Wcf
|
||||
from plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
from plugin_common.plugin_interface import PluginStatus
|
||||
from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
|
||||
from message_util import MessageUtil # 导入消息工具类
|
||||
|
||||
|
||||
class GroupMemberChangePlugin(MessagePluginInterface):
|
||||
@@ -52,7 +53,8 @@ class GroupMemberChangePlugin(MessagePluginInterface):
|
||||
|
||||
# 保存上下文对象
|
||||
self.wcf: Wcf = context.get("wcf")
|
||||
# 获取群管理器
|
||||
# 创建消息工具实例message_util
|
||||
self.message_util: MessageUtil = context.get("message_util")
|
||||
|
||||
# 从配置中获取启用状态和检查间隔
|
||||
plugin_config = self._config.get("GroupMemberChange", {})
|
||||
@@ -130,6 +132,11 @@ class GroupMemberChangePlugin(MessagePluginInterface):
|
||||
# 获取当前群成员
|
||||
current_members = self.wcf.get_chatroom_members(group_id)
|
||||
|
||||
# 添加安全检查:如果获取到的成员列表为空,可能是接口异常
|
||||
if not current_members:
|
||||
self.LOG.warning(f"获取群 {group_id} 成员列表为空,可能是接口异常,跳过本次检查")
|
||||
return
|
||||
|
||||
# 如果是首次检查该群
|
||||
if group_id not in self.local_members:
|
||||
self.LOG.info(f"首次检查群 {group_id},记录当前成员")
|
||||
@@ -139,6 +146,12 @@ class GroupMemberChangePlugin(MessagePluginInterface):
|
||||
# 获取上次记录的成员
|
||||
previous_members = self.local_members[group_id]
|
||||
|
||||
# 添加安全检查:如果上次记录的成员为空,重新初始化
|
||||
if not previous_members:
|
||||
self.LOG.warning(f"群 {group_id} 上次记录的成员列表为空,重新初始化")
|
||||
self.local_members[group_id] = current_members
|
||||
return
|
||||
|
||||
# 比较成员变化
|
||||
current_member_ids = set(current_members.keys())
|
||||
previous_member_ids = set(previous_members.keys())
|
||||
@@ -146,6 +159,14 @@ class GroupMemberChangePlugin(MessagePluginInterface):
|
||||
# 找出退群的成员
|
||||
left_members = previous_member_ids - current_member_ids
|
||||
|
||||
# 添加安全检查:如果退群人数超过阈值,可能是异常情况
|
||||
if len(left_members) > len(previous_member_ids) * 0.5: # 如果超过50%的成员"退群"
|
||||
self.LOG.warning(
|
||||
f"群 {group_id} 检测到超过50%的成员退群 ({len(left_members)}/{len(previous_member_ids)}),判定为异常情况,跳过通知")
|
||||
# 更新本地缓存但不发送通知
|
||||
self.local_members[group_id] = current_members
|
||||
return
|
||||
|
||||
# 找出新加入的成员
|
||||
joined_members = current_member_ids - previous_member_ids
|
||||
|
||||
@@ -161,13 +182,14 @@ class GroupMemberChangePlugin(MessagePluginInterface):
|
||||
# 处理新加入成员
|
||||
for wxid in joined_members:
|
||||
nickname = current_members[wxid]
|
||||
# 可以在这里添加欢迎新成员的代码
|
||||
self._send_join_notification(group_id, wxid, nickname) # 添加欢迎新成员的功能
|
||||
|
||||
# 更新本地缓存
|
||||
self.local_members[group_id] = current_members
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"检查群 {group_id} 成员变化时发生错误: {e}", exc_info=True)
|
||||
# 异常情况下不更新缓存,避免错误数据导致误判
|
||||
|
||||
def _send_leave_notification(self, group_id: str, wxid: str, nickname: str):
|
||||
"""发送成员退群通知"""
|
||||
@@ -178,9 +200,22 @@ class GroupMemberChangePlugin(MessagePluginInterface):
|
||||
微信号: {wxid}
|
||||
退群时间: {now_time}
|
||||
"""
|
||||
self.wcf.send_text(message, group_id)
|
||||
# 使用message_util发送消息
|
||||
self.message_util.send_text_msg(message, group_id)
|
||||
self.LOG.info(f"已发送退群通知: {nickname} 退出群 {group_id}")
|
||||
|
||||
def _send_join_notification(self, group_id: str, wxid: str, nickname: str):
|
||||
"""发送成员入群通知"""
|
||||
now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
message = f"""【入群欢迎】
|
||||
欢迎新成员: {nickname}
|
||||
入群时间: {now_time}
|
||||
"""
|
||||
# 使用message_util发送消息
|
||||
self.message_util.send_text_msg(message, group_id)
|
||||
self.LOG.info(f"已发送入群通知: {nickname} 加入群 {group_id}")
|
||||
|
||||
@property
|
||||
def commands(self) -> List[str]:
|
||||
"""插件支持的命令列表"""
|
||||
|
||||
Reference in New Issue
Block a user