✨ 增强缓存功能,优化请求管理逻辑 (#2012)
检查bot是否运行正常 / bot check (push) Has been cancelled
CodeQL Code Security Analysis / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Has been cancelled
CodeQL Code Security Analysis / Analyze (${{ matrix.language }}) (none, python) (push) Has been cancelled
Sequential Lint and Type Check / ruff-call (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Force Sync to Aliyun / sync (push) Has been cancelled
Update Version / update-version (push) Has been cancelled
Sequential Lint and Type Check / pyright-call (push) Has been cancelled

- 在 `record_request.py` 和 `group_handle/__init__.py` 中引入了 `CacheRoot`,实现请求缓存,避免重复处理相同请求。
- 在 `exception.py` 中更新 `ForceAddGroupError` 类,新增 `group_id` 属性以便于错误处理。
- 在 `data_source.py` 中修改 `ForceAddGroupError` 的抛出逻辑,包含 `group_id` 信息。
- 更新 `cache` 类,支持类型化缓存字典和列表,增强缓存的类型安全性。

此更新提升了请求处理的效率和准确性,同时增强了错误信息的可追溯性。
This commit is contained in:
HibiKier
2025-08-06 16:31:09 +08:00
committed by GitHub
parent be86e0bb7f
commit 3deffcb46c
8 changed files with 146 additions and 63 deletions
@@ -3,9 +3,13 @@ class ForceAddGroupError(Exception):
强制拉群
"""
def __init__(self, info: str):
def __init__(self, info: str, group_id: str):
super().__init__(self)
self._info = info
self._group_id = group_id
def get_info(self) -> str:
return self._info
def get_group_id(self) -> str:
return self._group_id
@@ -16,6 +16,7 @@ from zhenxun.configs.config import BotConfig, Config
from zhenxun.configs.utils import PluginExtraData, RegisterConfig, Task
from zhenxun.models.event_log import EventLog
from zhenxun.models.group_console import GroupConsole
from zhenxun.services.cache import CacheRoot
from zhenxun.utils.common_utils import CommonUtils
from zhenxun.utils.enum import EventLogType, PluginType
from zhenxun.utils.platform import PlatformUtils
@@ -92,6 +93,10 @@ group_decrease_handle = on_notice(
)
"""群员减少处理"""
cache = CacheRoot.cache_dict(
"REQUEST_CACHE", (base_config.get("TIP_MESSAGE_LIMIT") or 360) * 60, str
)
@group_increase_handle.handle()
async def _(
@@ -109,7 +114,9 @@ async def _(
bot, str(event.operator_id), str(event.group_id), group
)
except ForceAddGroupError as e:
await PlatformUtils.send_superuser(bot, e.get_info())
if not cache.get(e.get_group_id()):
cache.set(e.get_group_id(), "1")
await PlatformUtils.send_superuser(bot, e.get_info())
else:
await GroupManager.add_user(session, bot)
@@ -147,9 +147,13 @@ class GroupManager:
group_id=group_id,
e=e,
)
raise ForceAddGroupError("强制拉群或未有群信息,退出群聊失败...") from e
raise ForceAddGroupError(
"强制拉群或未有群信息,退出群聊失败...", group_id
) from e
# await GroupConsole.filter(group_id=group_id).delete()
raise ForceAddGroupError(f"触发强制入群保护,已成功退出群聊 {group_id}...")
raise ForceAddGroupError(
f"触发强制入群保护,已成功退出群聊 {group_id}...", group_id
)
else:
await cls.__handle_add_group(bot, group_id, group)
"""刷新群管理员权限"""