From 6db9bef115cdd982d60da54be5c5db68577b4cf5 Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Tue, 27 Dec 2022 11:31:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9D=83=E9=99=90=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E4=BE=9D=E8=B5=96=E6=B3=A8=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 + plugins/genshin/__init__.py | 5 +- plugins/genshin/query_user/__init__.py | 4 +- .../query_user/query_role/draw_image.py | 2 +- utils/depends/__init__.py | 78 ++++++++++++++++--- 5 files changed, 79 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f7da400a..11db27a6 100644 --- a/README.md +++ b/README.md @@ -299,6 +299,10 @@ PS: **ARM平台** 请使用全量版 同时 **如果你的机器 RAM < 1G 可能 ## 更新 +### 2022/12/27 + +* 添加权限检查依赖注入 + ### 2022/12/26 * 优化`gamedraw`插件 diff --git a/plugins/genshin/__init__.py b/plugins/genshin/__init__.py index 2d21e611..eb35e275 100755 --- a/plugins/genshin/__init__.py +++ b/plugins/genshin/__init__.py @@ -1,4 +1,5 @@ +from pathlib import Path + import nonebot -nonebot.load_plugins("plugins/genshin") - +nonebot.load_plugins(str(Path(__file__).parent.resolve())) diff --git a/plugins/genshin/query_user/__init__.py b/plugins/genshin/query_user/__init__.py index 8751dd7f..49b8a858 100644 --- a/plugins/genshin/query_user/__init__.py +++ b/plugins/genshin/query_user/__init__.py @@ -1,3 +1,5 @@ +from pathlib import Path + from configs.config import Config import nonebot @@ -26,7 +28,7 @@ Config.add_plugin_config( "5" ) -nonebot.load_plugins("plugins/genshin/query_user") +nonebot.load_plugins(str(Path(__file__).parent.resolve())) diff --git a/plugins/genshin/query_user/query_role/draw_image.py b/plugins/genshin/query_user/query_role/draw_image.py index 14650401..f0f0c00b 100644 --- a/plugins/genshin/query_user/query_role/draw_image.py +++ b/plugins/genshin/query_user/query_role/draw_image.py @@ -234,7 +234,7 @@ def get_home_data_image(home_data_list: List[Dict]) -> BuildImage: 画出家园数据 :param home_data_list: 家园列表 """ - h = 130 + 340 * 4 + h = 130 + 340 * len(home_data_list) region = BuildImage( 550, h, color="#E3DBD1", font="HYWenHei-85W.ttf", font_size=40 ) diff --git a/utils/depends/__init__.py b/utils/depends/__init__.py index be037513..a1927a9b 100644 --- a/utils/depends/__init__.py +++ b/utils/depends/__init__.py @@ -1,13 +1,44 @@ -from typing import List, Callable, Optional, Union +from typing import Callable, List, Optional, Union +from configs.config import Config +from models.bag_user import BagUser +from models.level_user import LevelUser +from models.user_shop_gold_log import UserShopGoldLog from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent from nonebot.internal.matcher import Matcher from nonebot.internal.params import Depends -from models.user_shop_gold_log import UserShopGoldLog -from models.bag_user import BagUser + +from utils.manager import admin_manager from utils.message_builder import at -from utils.utils import get_message_at, get_message_face, get_message_img, get_message_text -from configs.config import Config +from utils.utils import ( + get_message_at, + get_message_face, + get_message_img, + get_message_text, +) + + +def AdminCheck(level: Optional[int] = None): + """ + 说明: + 权限检查 + 参数: + :param level: 等级 + """ + + async def dependency(matcher: Matcher, event: GroupMessageEvent): + plugin_level = admin_manager.get_plugin_module(matcher.plugin_name) + user_level = await LevelUser.get_user_level(event.user_id, event.group_id) + if level is None: + if user_level < plugin_level: + await matcher.finish( + at(event.user_id) + f"你的权限不足喔,该功能需要的权限等级:{plugin_level}" + ) + else: + if user_level < level: + await matcher.finish(at(event.user_id) + f"你的权限不足喔,该功能需要的权限等级:{level}") + + return Depends(dependency) def CostGold(gold: int): @@ -17,16 +48,24 @@ def CostGold(gold: int): 参数: :param gold: 金币数量 """ + async def dependency(matcher: Matcher, event: GroupMessageEvent): if (await BagUser.get_gold(event.user_id, event.group_id)) < gold: await matcher.finish(at(event.user_id) + f"金币不足..该功能需要{gold}金币..") await BagUser.spend_gold(event.user_id, event.group_id, gold) - await UserShopGoldLog.add_shop_log(event.user_id, event.group_id, 2, matcher.plugin_name, gold, 1) + await UserShopGoldLog.add_shop_log( + event.user_id, event.group_id, 2, matcher.plugin_name, gold, 1 + ) return Depends(dependency) -def GetConfig(module: Optional[str] = None, config: str = "", default_value: Optional[str] = None, prompt: Optional[str] = None): +def GetConfig( + module: Optional[str] = None, + config: str = "", + default_value: Optional[str] = None, + prompt: Optional[str] = None, +): """ 说明: 获取配置项 @@ -36,6 +75,7 @@ def GetConfig(module: Optional[str] = None, config: str = "", default_value: Opt :param default_value: 默认值 :param prompt: 为空时提示 """ + async def dependency(matcher: Matcher): module_ = module or matcher.plugin_name value = Config.get_config(module_, config, default_value) @@ -46,7 +86,11 @@ def GetConfig(module: Optional[str] = None, config: str = "", default_value: Opt return Depends(dependency) -def CheckConfig(module: Optional[str] = None, config: Union[str, List[str]] = "", prompt: Optional[str] = None): +def CheckConfig( + module: Optional[str] = None, + config: Union[str, List[str]] = "", + prompt: Optional[str] = None, +): """ 说明: 检测配置项在配置文件中是否填写 @@ -55,6 +99,7 @@ def CheckConfig(module: Optional[str] = None, config: Union[str, List[str]] = "" :param config: 需要检查的配置项名称 :param prompt: 为空时提示 """ + async def dependency(matcher: Matcher): module_ = module or matcher.plugin_name config_list = [config] if isinstance(config, str) else config @@ -65,7 +110,13 @@ def CheckConfig(module: Optional[str] = None, config: Union[str, List[str]] = "" return Depends(dependency) -async def _match(matcher: Matcher, event: MessageEvent, msg: Optional[str], func: Callable, contain_reply: bool): +async def _match( + matcher: Matcher, + event: MessageEvent, + msg: Optional[str], + func: Callable, + contain_reply: bool, +): _list = func(event.message) if event.reply and contain_reply: _list = func(event.reply.message) @@ -82,6 +133,7 @@ def ImageList(msg: Optional[str] = None, contain_reply: bool = True) -> List[str :param msg: 提示文本 :param contain_reply: 包含回复内容 """ + async def dependency(matcher: Matcher, event: MessageEvent): return await _match(matcher, event, msg, get_message_img, contain_reply) @@ -96,8 +148,12 @@ def AtList(msg: Optional[str] = None, contain_reply: bool = True) -> List[int]: :param msg: 提示文本 :param contain_reply: 包含回复内容 """ + async def dependency(matcher: Matcher, event: MessageEvent): - return [int(x) for x in await _match(matcher, event, msg, get_message_at, contain_reply)] + return [ + int(x) + for x in await _match(matcher, event, msg, get_message_at, contain_reply) + ] return Depends(dependency) @@ -110,6 +166,7 @@ def FaceList(msg: Optional[str] = None, contain_reply: bool = True) -> List[str] :param msg: 提示文本 :param contain_reply: 包含回复内容 """ + async def dependency(matcher: Matcher, event: MessageEvent): return await _match(matcher, event, msg, get_message_face, contain_reply) @@ -124,6 +181,7 @@ def PlaintText(msg: Optional[str] = None, contain_reply: bool = True) -> str: :param msg: 提示文本 :param contain_reply: 包含回复内容 """ + async def dependency(matcher: Matcher, event: MessageEvent): return await _match(matcher, event, msg, get_message_text, contain_reply)