🎨 优化检测代码结构

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,38 +80,33 @@ 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( try:
f"插件: {plugin.name}:{plugin.module} " cost_gold = await auth_cost(user, plugin, session)
"为HIDDEN已跳过权限检查..." if session.user.id in bot.config.superusers:
) if plugin.plugin_type == PluginType.SUPERUSER:
return raise IsSuperuserException()
try: if not plugin.limit_superuser:
cost_gold = await auth_cost(user, plugin, session) cost_gold = 0
if session.user.id in bot.config.superusers: raise IsSuperuserException()
if plugin.plugin_type == PluginType.SUPERUSER: await auth_bot(plugin, bot.self_id)
raise IsSuperuserException() await auth_group(plugin, session, message)
if not plugin.limit_superuser: await auth_admin(plugin, session)
cost_gold = 0 await auth_plugin(plugin, session, event)
raise IsSuperuserException() await auth_limit(plugin, session)
await auth_bot(plugin, bot.self_id) except IsSuperuserException:
await auth_group(plugin, session, message) logger.debug(
await auth_admin(plugin, session) "超级用户或被ban跳过权限检测...", "AuthChecker", session=session
await auth_plugin(plugin, session, event) )
await auth_limit(plugin, session) except IgnoredException:
except IsSuperuserException: is_ignore = True
logger.debug( LimitManage.unblock(matcher.plugin.name, user_id, group_id, channel_id)
"超级用户或被ban跳过权限检测...", "AuthChecker", session=session except AssertionError as e:
) is_ignore = True
except IgnoredException: logger.debug("消息无法发送", session=session, e=e)
is_ignore = True
LimitManage.unblock(matcher.plugin.name, user_id, group_id, channel_id)
except AssertionError as e:
is_ignore = True
logger.debug("消息无法发送", session=session, e=e)
if cost_gold and user_id: if cost_gold and user_id:
"""花费金币""" """花费金币"""
try: try: