🎨 优化检测代码结构

This commit is contained in:
HibiKier 2025-04-05 22:39:42 +08:00
parent 6eb9bb510a
commit 03f0185e46

View File

@ -1,3 +1,5 @@
import contextlib
from nonebot.adapters import Bot, Event from nonebot.adapters import Bot, Event
from nonebot.exception import IgnoredException from nonebot.exception import IgnoredException
from nonebot.matcher import Matcher from nonebot.matcher import Matcher
@ -7,8 +9,8 @@ from tortoise.exceptions import IntegrityError
from zhenxun.models.plugin_info import PluginInfo from zhenxun.models.plugin_info import PluginInfo
from zhenxun.models.user_console import UserConsole from zhenxun.models.user_console import UserConsole
from zhenxun.services.log import logger
from zhenxun.services.cache import Cache from zhenxun.services.cache import Cache
from zhenxun.services.log import logger
from zhenxun.utils.enum import ( from zhenxun.utils.enum import (
CacheType, CacheType,
GoldHandle, GoldHandle,
@ -53,16 +55,22 @@ async def auth(
group_id = session.group.id group_id = session.group.id
is_ignore = False is_ignore = False
cost_gold = 0 cost_gold = 0
try: with contextlib.suppress(ImportError):
from nonebot.adapters.onebot.v11 import PokeNotifyEvent from nonebot.adapters.onebot.v11 import PokeNotifyEvent
if matcher.type == "notice" and not isinstance(event, PokeNotifyEvent): if matcher.type == "notice" and not isinstance(event, PokeNotifyEvent):
"""过滤除poke外的notice""" """过滤除poke外的notice"""
return return
except ImportError:
pass
user_cache = Cache[UserConsole](CacheType.USERS) user_cache = Cache[UserConsole](CacheType.USERS)
if matcher.plugin and (module := matcher.plugin.name): if matcher.plugin and (module := matcher.plugin.name):
plugin = await Cache[PluginInfo](CacheType.PLUGINS).get(module)
if not plugin:
return logger.debug(f"插件:{module} 数据不存在,已跳过权限检查...")
if plugin.plugin_type == PluginType.HIDDEN:
return logger.debug(
f"插件: {plugin.name}:{plugin.module} 为HIDDEN已跳过权限检查..."
)
user = None
try: try:
user = await user_cache.get(session.user.id) user = await user_cache.get(session.user.id)
except IntegrityError as e: except IntegrityError as e:
@ -72,15 +80,10 @@ async def auth(
session=session, session=session,
e=e, e=e,
) )
return if not user:
plugin = await Cache[PluginInfo](CacheType.PLUGINS).get(module) return logger.debug(
if user and plugin: "用户数据不存在,已跳过权限检查...", "AuthChecker", session=session
if plugin.plugin_type == PluginType.HIDDEN:
logger.debug(
f"插件: {plugin.name}:{plugin.module} "
"为HIDDEN已跳过权限检查..."
) )
return
try: try:
cost_gold = await auth_cost(user, plugin, session) cost_gold = await auth_cost(user, plugin, session)
if session.user.id in bot.config.superusers: if session.user.id in bot.config.superusers: