mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +08:00
🚑 移除认证队列机制并放宽消息记录规则 (#2091)
* 🚑 fix(auth_hook):直接调用 auth 并移除认证队列 * 🚑 fix(chat_message): 简化规则函数,移除不必要的命令检查和时间间隔逻辑 --------- Co-authored-by: ATTomatoo <1126160939@qq.com>
This commit is contained in:
co-authored by
ATTomatoo
parent
8378921c71
commit
662d61a672
@@ -1,8 +1,7 @@
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
from nonebot import get_driver, on_message
|
||||
from nonebot.adapters import Event
|
||||
from nonebot import on_message
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot_plugin_alconna import UniMsg
|
||||
from nonebot_plugin_apscheduler import scheduler
|
||||
@@ -38,43 +37,8 @@ __plugin_meta__ = PluginMetadata(
|
||||
)
|
||||
|
||||
|
||||
_COMMAND_STARTS = {str(item) for item in (get_driver().config.command_start or [])}
|
||||
_LAST_GROUP_SAVE: dict[str, float] = {}
|
||||
_LAST_USER_SAVE: dict[str, float] = {}
|
||||
_GROUP_MIN_INTERVAL = 0.5
|
||||
_USER_MIN_INTERVAL = 0.2
|
||||
|
||||
|
||||
def _is_command_like(text: str) -> bool:
|
||||
if not text:
|
||||
return False
|
||||
for start in _COMMAND_STARTS:
|
||||
if text.startswith(start):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
async def rule(event: Event, message: UniMsg, session: Uninfo) -> bool:
|
||||
if is_overloaded():
|
||||
return False
|
||||
if not Config.get_config("chat_history", "FLAG"):
|
||||
return False
|
||||
if not message:
|
||||
return False
|
||||
text = message.extract_plain_text().strip()
|
||||
if _is_command_like(text):
|
||||
return False
|
||||
entity = get_entity_ids(session)
|
||||
now = time.time()
|
||||
if entity.group_id:
|
||||
last_group = _LAST_GROUP_SAVE.get(entity.group_id, 0)
|
||||
if now - last_group < _GROUP_MIN_INTERVAL:
|
||||
return False
|
||||
if entity.user_id:
|
||||
last_user = _LAST_USER_SAVE.get(entity.user_id, 0)
|
||||
if now - last_user < _USER_MIN_INTERVAL:
|
||||
return False
|
||||
return True
|
||||
def rule(message: UniMsg) -> bool:
|
||||
return bool(Config.get_config("chat_history", "FLAG") and message)
|
||||
|
||||
|
||||
chat_history = on_message(rule=rule, priority=1, block=False)
|
||||
@@ -89,10 +53,6 @@ _DROP_LOG_INTERVAL = 10.0
|
||||
async def _(message: UniMsg, session: Uninfo):
|
||||
entity = get_entity_ids(session)
|
||||
now = time.time()
|
||||
if entity.group_id:
|
||||
_LAST_GROUP_SAVE[entity.group_id] = now
|
||||
if entity.user_id:
|
||||
_LAST_USER_SAVE[entity.user_id] = now
|
||||
if is_overloaded():
|
||||
return
|
||||
try:
|
||||
|
||||
@@ -11,25 +11,20 @@ from nonebot_plugin_uninfo import Uninfo
|
||||
|
||||
from zhenxun.services.cache.runtime_cache import is_cache_ready
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.services.message_load import is_overloaded, signal_overload
|
||||
from zhenxun.services.message_load import is_overloaded
|
||||
from zhenxun.utils.utils import get_entity_ids
|
||||
|
||||
from .auth.config import LOGGER_COMMAND
|
||||
from .auth.exception import SkipPluginException
|
||||
from .auth_checker import (
|
||||
LimitManager,
|
||||
_get_event_cache,
|
||||
auth,
|
||||
auth_ban_fast,
|
||||
auth_precheck,
|
||||
route_precheck,
|
||||
)
|
||||
|
||||
_SKIP_AUTH_PLUGINS = {"chat_history", "chat_message"}
|
||||
_BOT_CONNECT_TS: float | None = None
|
||||
_AUTH_QUEUE_MAXSIZE = 200
|
||||
_AUTH_QUEUE_HIGH_WATER = 160
|
||||
_AUTH_OVERLOAD_WINDOW = 5.0
|
||||
_AUTH_QUEUE: asyncio.Queue[tuple[Matcher, Event, Bot, Uninfo, UniMsg]] = asyncio.Queue(
|
||||
maxsize=_AUTH_QUEUE_MAXSIZE
|
||||
)
|
||||
@@ -107,41 +102,30 @@ async def _auth_preprocessor(
|
||||
):
|
||||
if event.get_type() == "message" and not is_cache_ready():
|
||||
raise IgnoredException("cache not ready ignore")
|
||||
if _skip_auth_for_plugin(matcher):
|
||||
return
|
||||
start_time = time.time()
|
||||
entity = get_entity_ids(session)
|
||||
event_cache = _get_event_cache(event, session, entity)
|
||||
_get_event_cache(event, session, entity)
|
||||
|
||||
if await route_precheck(matcher, event, session, message):
|
||||
return
|
||||
try:
|
||||
await auth_ban_fast(matcher, event, bot, session)
|
||||
except SkipPluginException as exc:
|
||||
logger.info(str(exc), LOGGER_COMMAND, session=session)
|
||||
raise IgnoredException("ban fast ignore") from exc
|
||||
try:
|
||||
await auth_precheck(matcher, event, bot, session, message)
|
||||
except SkipPluginException as exc:
|
||||
logger.info(str(exc), LOGGER_COMMAND, session=session)
|
||||
raise IgnoredException("precheck ignore") from exc
|
||||
|
||||
if event_cache is not None and event_cache.get("route_skip") is True:
|
||||
if not is_overloaded():
|
||||
logger.debug("route miss skip auth task", LOGGER_COMMAND)
|
||||
if _skip_auth_for_plugin(matcher):
|
||||
return
|
||||
|
||||
try:
|
||||
_AUTH_QUEUE.put_nowait((matcher, event, bot, session, message))
|
||||
except asyncio.QueueFull:
|
||||
signal_overload(_AUTH_OVERLOAD_WINDOW)
|
||||
now = time.monotonic()
|
||||
global _LAST_DROP_LOG
|
||||
if now - _LAST_DROP_LOG > 1.0:
|
||||
_LAST_DROP_LOG = now
|
||||
logger.warning("auth queue full, skip auth task", LOGGER_COMMAND)
|
||||
return
|
||||
if _AUTH_QUEUE.qsize() >= _AUTH_QUEUE_HIGH_WATER:
|
||||
signal_overload(_AUTH_OVERLOAD_WINDOW)
|
||||
await auth(
|
||||
matcher,
|
||||
event,
|
||||
bot,
|
||||
session,
|
||||
message,
|
||||
skip_ban=False,
|
||||
)
|
||||
except IgnoredException:
|
||||
raise
|
||||
except Exception as exc:
|
||||
logger.error("auth check failed", LOGGER_COMMAND, e=exc)
|
||||
raise IgnoredException("auth failed") from exc
|
||||
|
||||
now = time.monotonic()
|
||||
last_log = getattr(_auth_preprocessor, "_last_log", 0.0)
|
||||
if now - last_log > 1.0 and not is_overloaded():
|
||||
|
||||
Reference in New Issue
Block a user