Skip to content

Commit

Permalink
添加权限检查依赖注入
Browse files Browse the repository at this point in the history
  • Loading branch information
HibiKier committed Dec 27, 2022
1 parent a0b440e commit 6db9bef
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ PS: **ARM平台** 请使用全量版 同时 **如果你的机器 RAM < 1G 可能

## 更新

### 2022/12/27

* 添加权限检查依赖注入

### 2022/12/26

* 优化`gamedraw`插件
Expand Down
5 changes: 3 additions & 2 deletions plugins/genshin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import nonebot
from pathlib import Path

nonebot.load_plugins("plugins/genshin")
import nonebot

nonebot.load_plugins(str(Path(__file__).parent.resolve()))
4 changes: 3 additions & 1 deletion plugins/genshin/query_user/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

from configs.config import Config
import nonebot

Expand Down Expand Up @@ -26,7 +28,7 @@
"5"
)

nonebot.load_plugins("plugins/genshin/query_user")
nonebot.load_plugins(str(Path(__file__).parent.resolve()))



Expand Down
2 changes: 1 addition & 1 deletion plugins/genshin/query_user/query_role/draw_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
78 changes: 68 additions & 10 deletions utils/depends/__init__.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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,
):
"""
说明:
获取配置项
Expand All @@ -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)
Expand All @@ -46,7 +86,11 @@ async def dependency(matcher: Matcher):
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,
):
"""
说明:
检测配置项在配置文件中是否填写
Expand All @@ -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
Expand All @@ -65,7 +110,13 @@ async def dependency(matcher: Matcher):
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)
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 6db9bef

Please sign in to comment.