Update utils.py

pull/65/head
foxplaying 2025-10-28 13:53:02 +08:00 committed by GitHub
parent 35469f7ce6
commit ccd3a65221
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 34 deletions

View File

@ -1,5 +1,6 @@
import websockets as Server import websockets as Server
import json import json
import asyncio
import base64 import base64
import uuid import uuid
import urllib3 import urllib3
@ -294,43 +295,41 @@ async def read_ban_list(
一个已经自然解除全体禁言的群的BanUser列表, 一个已经自然解除全体禁言的群的BanUser列表,
] ]
""" """
try: try:
ban_list = db_manager.get_ban_records() ban_list = db_manager.get_ban_records()
lifted_list: List[BanUser] = [] lifted_list: List[BanUser] = []
logger.info("已经读取禁言列表") logger.info("已经读取禁言列表")
for ban_record in list(ban_list): tasks = []
if ban_record.user_id == 0: for record in ban_list:
try: if record.user_id == 0: # 群全体禁言
fetched_group_info = await get_group_info(websocket, ban_record.group_id) async def handle_group(r=record):
except Exception as e: try:
logger.warning(f"获取群信息失败(群号: {ban_record.group_id}{e},保留全体禁言状态") group_info = await get_group_info(websocket, r.group_id)
continue except Exception as e:
if not fetched_group_info: logger.warning(f"获取群信息失败(群号: {r.group_id}{e},保留禁言状态")
logger.warning(f"{ban_record.group_id} 暂未返回群信息,跳过全体禁言检查") return None
continue if not group_info or group_info.get("group_all_shut") == 0:
group_all_shut = fetched_group_info.get("group_all_shut") return r
if group_all_shut == 0: return None
lifted_list.append(ban_record) tasks.append(handle_group())
ban_list.remove(ban_record) else: # 普通用户
continue async def handle_user(r=record):
try: try:
fetched_member_info = await get_member_info(websocket, ban_record.group_id, ban_record.user_id) member_info = await get_member_info(websocket, r.group_id, r.user_id)
except Exception as e: except Exception as e:
logger.warning( logger.warning(f"获取成员信息失败(群号: {r.group_id} 用户ID: {r.user_id}{e}")
f"获取群成员信息失败(群号: {ban_record.group_id} 用户ID: {ban_record.user_id}{e},保留禁言状态" return None
) if not member_info:
continue return None
if fetched_member_info is None: lift_time = member_info.get("shut_up_timestamp", 0)
logger.warning( if lift_time == 0:
f"{ban_record.group_id} 用户 {ban_record.user_id} 暂无成员信息Napcat缓存未加载暂不视为解除" return r
) r.lift_time = lift_time
continue return None
lift_ban_time: int = fetched_member_info.get("shut_up_timestamp", 0) tasks.append(handle_user())
if lift_ban_time == 0: results = await asyncio.gather(*tasks)
lifted_list.append(ban_record) lifted_list = [r for r in results if r is not None]
ban_list.remove(ban_record) ban_list = [r for r in ban_list if r not in lifted_list]
else:
ban_record.lift_time = lift_ban_time
db_manager.update_ban_record(ban_list) db_manager.update_ban_record(ban_list)
return ban_list, lifted_list return ban_list, lifted_list
except Exception as e: except Exception as e: