mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
🐛 添加新异常判断跳过权限检测
This commit is contained in:
parent
41dd767724
commit
43f1e48c14
@ -2,13 +2,14 @@ from nonebot import on_message
|
|||||||
from nonebot.plugin import PluginMetadata
|
from nonebot.plugin import PluginMetadata
|
||||||
from nonebot_plugin_alconna import UniMsg
|
from nonebot_plugin_alconna import UniMsg
|
||||||
from nonebot_plugin_apscheduler import scheduler
|
from nonebot_plugin_apscheduler import scheduler
|
||||||
from nonebot_plugin_session import EventSession
|
from nonebot_plugin_uninfo import Uninfo
|
||||||
|
|
||||||
from zhenxun.configs.config import Config
|
from zhenxun.configs.config import Config
|
||||||
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
|
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
|
||||||
from zhenxun.models.chat_history import ChatHistory
|
from zhenxun.models.chat_history import ChatHistory
|
||||||
from zhenxun.services.log import logger
|
from zhenxun.services.log import logger
|
||||||
from zhenxun.utils.enum import PluginType
|
from zhenxun.utils.enum import PluginType
|
||||||
|
from zhenxun.utils.utils import get_entity_ids
|
||||||
|
|
||||||
__plugin_meta__ = PluginMetadata(
|
__plugin_meta__ = PluginMetadata(
|
||||||
name="消息存储",
|
name="消息存储",
|
||||||
@ -43,16 +44,15 @@ TEMP_LIST = []
|
|||||||
|
|
||||||
|
|
||||||
@chat_history.handle()
|
@chat_history.handle()
|
||||||
async def _(message: UniMsg, session: EventSession):
|
async def _(message: UniMsg, session: Uninfo):
|
||||||
# group_id = session.id3 or session.id2
|
entity = get_entity_ids(session)
|
||||||
group_id = session.id2
|
|
||||||
TEMP_LIST.append(
|
TEMP_LIST.append(
|
||||||
ChatHistory(
|
ChatHistory(
|
||||||
user_id=session.id1,
|
user_id=entity.user_id,
|
||||||
group_id=group_id,
|
group_id=entity.group_id,
|
||||||
text=str(message),
|
text=str(message),
|
||||||
plain_text=message.extract_plain_text(),
|
plain_text=message.extract_plain_text(),
|
||||||
bot_id=session.bot_id,
|
bot_id=session.self_id,
|
||||||
platform=session.platform,
|
platform=session.platform,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -68,7 +68,7 @@ async def _():
|
|||||||
TEMP_LIST.clear()
|
TEMP_LIST.clear()
|
||||||
if message_list:
|
if message_list:
|
||||||
await ChatHistory.bulk_create(message_list)
|
await ChatHistory.bulk_create(message_list)
|
||||||
logger.debug(f"批量添加聊天记录 {len(message_list)} 条", "定时任务")
|
logger.debug(f"批量添加聊天记录 {len(message_list)} 条", "定时任务")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("定时批量添加聊天记录", "定时任务", e=e)
|
logger.error("定时批量添加聊天记录", "定时任务", e=e)
|
||||||
|
|
||||||
|
|||||||
@ -22,11 +22,15 @@ async def auth_admin(plugin: PluginInfo, session: Uninfo):
|
|||||||
return
|
return
|
||||||
entity = get_entity_ids(session)
|
entity = get_entity_ids(session)
|
||||||
cache = Cache[list[LevelUser]](CacheType.LEVEL)
|
cache = Cache[list[LevelUser]](CacheType.LEVEL)
|
||||||
user_level = await cache.get(session.user.id) or []
|
user_list = await cache.get(session.user.id) or []
|
||||||
if entity.group_id:
|
if entity.group_id:
|
||||||
user_level += await cache.get(session.user.id, entity.group_id) or []
|
user_list += await cache.get(session.user.id, entity.group_id) or []
|
||||||
user = max(user_level, key=lambda x: x.user_level)
|
if user_list:
|
||||||
if user.user_level < plugin.admin_level:
|
user = max(user_list, key=lambda x: x.user_level)
|
||||||
|
user_level = user.user_level
|
||||||
|
else:
|
||||||
|
user_level = 0
|
||||||
|
if user_level < plugin.admin_level:
|
||||||
await send_message(
|
await send_message(
|
||||||
session,
|
session,
|
||||||
[
|
[
|
||||||
@ -38,8 +42,8 @@ async def auth_admin(plugin: PluginInfo, session: Uninfo):
|
|||||||
raise SkipPluginException(
|
raise SkipPluginException(
|
||||||
f"{plugin.name}({plugin.module}) 管理员权限不足..."
|
f"{plugin.name}({plugin.module}) 管理员权限不足..."
|
||||||
)
|
)
|
||||||
elif user_level:
|
elif user_list:
|
||||||
user = max(user_level, key=lambda x: x.user_level)
|
user = max(user_list, key=lambda x: x.user_level)
|
||||||
if user.user_level < plugin.admin_level:
|
if user.user_level < plugin.admin_level:
|
||||||
await send_message(
|
await send_message(
|
||||||
session,
|
session,
|
||||||
|
|||||||
@ -12,3 +12,15 @@ class SkipPluginException(Exception):
|
|||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return self.info
|
return self.info
|
||||||
|
|
||||||
|
|
||||||
|
class PermissionExemption(Exception):
|
||||||
|
def __init__(self, info: str, *args: object) -> None:
|
||||||
|
super().__init__(*args)
|
||||||
|
self.info = info
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return self.info
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return self.info
|
||||||
|
|||||||
@ -28,7 +28,11 @@ from .auth.auth_group import auth_group
|
|||||||
from .auth.auth_limit import LimitManage, auth_limit
|
from .auth.auth_limit import LimitManage, auth_limit
|
||||||
from .auth.auth_plugin import auth_plugin
|
from .auth.auth_plugin import auth_plugin
|
||||||
from .auth.config import LOGGER_COMMAND
|
from .auth.config import LOGGER_COMMAND
|
||||||
from .auth.exception import IsSuperuserException, SkipPluginException
|
from .auth.exception import (
|
||||||
|
IsSuperuserException,
|
||||||
|
PermissionExemption,
|
||||||
|
SkipPluginException,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def get_plugin_and_user(
|
async def get_plugin_and_user(
|
||||||
@ -41,10 +45,10 @@ async def get_plugin_and_user(
|
|||||||
user_id: 用户id
|
user_id: 用户id
|
||||||
|
|
||||||
异常:
|
异常:
|
||||||
SkipPluginException: 插件数据不存在
|
PermissionExemption: 插件数据不存在
|
||||||
SkipPluginException: 插件类型为HIDDEN
|
PermissionExemption: 插件类型为HIDDEN
|
||||||
SkipPluginException: 重复创建用户
|
PermissionExemption: 重复创建用户
|
||||||
SkipPluginException: 用户数据不存在
|
PermissionExemption: 用户数据不存在
|
||||||
|
|
||||||
返回:
|
返回:
|
||||||
tuple[PluginInfo, UserConsole]: 插件信息,用户信息
|
tuple[PluginInfo, UserConsole]: 插件信息,用户信息
|
||||||
@ -52,18 +56,18 @@ async def get_plugin_and_user(
|
|||||||
user_cache = Cache[UserConsole](CacheType.USERS)
|
user_cache = Cache[UserConsole](CacheType.USERS)
|
||||||
plugin = await Cache[PluginInfo](CacheType.PLUGINS).get(module)
|
plugin = await Cache[PluginInfo](CacheType.PLUGINS).get(module)
|
||||||
if not plugin:
|
if not plugin:
|
||||||
raise SkipPluginException(f"插件:{module} 数据不存在,已跳过权限检查...")
|
raise PermissionExemption(f"插件:{module} 数据不存在,已跳过权限检查...")
|
||||||
if plugin.plugin_type == PluginType.HIDDEN:
|
if plugin.plugin_type == PluginType.HIDDEN:
|
||||||
raise SkipPluginException(
|
raise PermissionExemption(
|
||||||
f"插件: {plugin.name}:{plugin.module} 为HIDDEN,已跳过权限检查..."
|
f"插件: {plugin.name}:{plugin.module} 为HIDDEN,已跳过权限检查..."
|
||||||
)
|
)
|
||||||
user = None
|
user = None
|
||||||
try:
|
try:
|
||||||
user = await user_cache.get(user_id)
|
user = await user_cache.get(user_id)
|
||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
raise SkipPluginException("重复创建用户,已跳过该次权限检查...") from e
|
raise PermissionExemption("重复创建用户,已跳过该次权限检查...") from e
|
||||||
if not user:
|
if not user:
|
||||||
raise SkipPluginException("用户数据不存在,已跳过权限检查...")
|
raise PermissionExemption("用户数据不存在,已跳过权限检查...")
|
||||||
return plugin, user
|
return plugin, user
|
||||||
|
|
||||||
|
|
||||||
@ -143,7 +147,7 @@ async def auth(
|
|||||||
module = matcher.plugin_name or ""
|
module = matcher.plugin_name or ""
|
||||||
try:
|
try:
|
||||||
if not module:
|
if not module:
|
||||||
raise SkipPluginException("Matcher插件名称不存在...")
|
raise PermissionExemption("Matcher插件名称不存在...")
|
||||||
plugin, user = await get_plugin_and_user(module, entity.user_id)
|
plugin, user = await get_plugin_and_user(module, entity.user_id)
|
||||||
cost_gold = await get_plugin_cost(bot, user, plugin, session)
|
cost_gold = await get_plugin_cost(bot, user, plugin, session)
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
@ -162,6 +166,8 @@ async def auth(
|
|||||||
ignore_flag = True
|
ignore_flag = True
|
||||||
except IsSuperuserException:
|
except IsSuperuserException:
|
||||||
logger.debug("超级用户跳过权限检测...", LOGGER_COMMAND, session=session)
|
logger.debug("超级用户跳过权限检测...", LOGGER_COMMAND, session=session)
|
||||||
|
except PermissionExemption as e:
|
||||||
|
logger.info(str(e), LOGGER_COMMAND, session=session)
|
||||||
if not ignore_flag and cost_gold > 0:
|
if not ignore_flag and cost_gold > 0:
|
||||||
await reduce_gold(entity.user_id, module, cost_gold, session)
|
await reduce_gold(entity.user_id, module, cost_gold, session)
|
||||||
if ignore_flag:
|
if ignore_flag:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user