zhenxun_bot/utils/depends/__init__.py

189 lines
5.7 KiB
Python
Raw Normal View History

2022-12-27 11:31:34 +08:00
from typing import Callable, List, Optional, Union
2022-10-15 22:13:41 +08:00
2022-12-27 11:31:34 +08:00
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
2022-10-15 22:13:41 +08:00
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent
from nonebot.internal.matcher import Matcher
from nonebot.internal.params import Depends
2022-12-27 11:31:34 +08:00
from utils.manager import admin_manager
from utils.message_builder import at
2022-12-27 11:31:34 +08:00
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)
2022-10-22 18:12:55 +08:00
def CostGold(gold: int):
"""
说明:
插件方法调用使用金币
参数:
:param gold: 金币数量
"""
2022-12-27 11:31:34 +08:00
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)
2022-12-27 11:31:34 +08:00
await UserShopGoldLog.add_shop_log(
event.user_id, event.group_id, 2, matcher.plugin_name, gold, 1
)
return Depends(dependency)
2022-12-27 11:31:34 +08:00
def GetConfig(
module: Optional[str] = None,
config: str = "",
default_value: Optional[str] = None,
prompt: Optional[str] = None,
):
2022-10-22 18:12:55 +08:00
"""
说明:
获取配置项
参数:
:param module: 模块名为空时默认使用当前插件模块名
:param config: 配置项名称
:param default_value: 默认值
:param prompt: 为空时提示
"""
2022-12-27 11:31:34 +08:00
2022-10-22 18:12:55 +08:00
async def dependency(matcher: Matcher):
module_ = module or matcher.plugin_name
value = Config.get_config(module_, config, default_value)
if value is None:
await matcher.finish(prompt or f"配置项 {config} 未填写!")
return value
return Depends(dependency)
2022-12-27 11:31:34 +08:00
def CheckConfig(
module: Optional[str] = None,
config: Union[str, List[str]] = "",
prompt: Optional[str] = None,
):
2022-10-22 18:12:55 +08:00
"""
说明:
检测配置项在配置文件中是否填写
参数:
:param module: 模块名为空时默认使用当前插件模块名
:param config: 需要检查的配置项名称
:param prompt: 为空时提示
"""
2022-12-27 11:31:34 +08:00
2022-10-22 18:12:55 +08:00
async def dependency(matcher: Matcher):
module_ = module or matcher.plugin_name
config_list = [config] if isinstance(config, str) else config
for c in config_list:
if Config.get_config(module_, c) is None:
await matcher.finish(prompt or f"配置项 {c} 未填写!")
return Depends(dependency)
2022-12-27 11:31:34 +08:00
async def _match(
matcher: Matcher,
event: MessageEvent,
msg: Optional[str],
func: Callable,
contain_reply: bool,
):
2022-10-15 22:13:41 +08:00
_list = func(event.message)
2022-10-22 18:12:55 +08:00
if event.reply and contain_reply:
_list = func(event.reply.message)
2022-10-15 22:13:41 +08:00
if not _list and msg:
await matcher.finish(msg)
return _list
2022-10-22 18:12:55 +08:00
def ImageList(msg: Optional[str] = None, contain_reply: bool = True) -> List[str]:
2022-10-15 22:13:41 +08:00
"""
说明:
2022-10-22 18:12:55 +08:00
获取图片列表包括回复时含有msg时不能为空为空时提示并结束事件
2022-10-15 22:13:41 +08:00
参数:
:param msg: 提示文本
2022-10-22 18:12:55 +08:00
:param contain_reply: 包含回复内容
2022-10-15 22:13:41 +08:00
"""
2022-12-27 11:31:34 +08:00
2022-10-15 22:13:41 +08:00
async def dependency(matcher: Matcher, event: MessageEvent):
2022-10-22 18:12:55 +08:00
return await _match(matcher, event, msg, get_message_img, contain_reply)
2022-10-15 22:13:41 +08:00
return Depends(dependency)
2022-12-13 23:01:12 +08:00
def AtList(msg: Optional[str] = None, contain_reply: bool = True) -> List[int]:
2022-10-15 22:13:41 +08:00
"""
说明:
2022-10-22 18:12:55 +08:00
获取at列表包括回复时含有msg时不能为空为空时提示并结束事件
2022-10-15 22:13:41 +08:00
参数:
:param msg: 提示文本
2022-10-22 18:12:55 +08:00
:param contain_reply: 包含回复内容
2022-10-15 22:13:41 +08:00
"""
2022-12-27 11:31:34 +08:00
2022-10-15 22:13:41 +08:00
async def dependency(matcher: Matcher, event: MessageEvent):
2022-12-27 11:31:34 +08:00
return [
int(x)
for x in await _match(matcher, event, msg, get_message_at, contain_reply)
]
2022-10-15 22:13:41 +08:00
return Depends(dependency)
2022-10-22 18:12:55 +08:00
def FaceList(msg: Optional[str] = None, contain_reply: bool = True) -> List[str]:
2022-10-15 22:13:41 +08:00
"""
说明:
2022-10-22 18:12:55 +08:00
获取face列表包括回复时含有msg时不能为空为空时提示并结束事件
2022-10-15 22:13:41 +08:00
参数:
:param msg: 提示文本
2022-10-22 18:12:55 +08:00
:param contain_reply: 包含回复内容
2022-10-15 22:13:41 +08:00
"""
2022-12-27 11:31:34 +08:00
2022-10-15 22:13:41 +08:00
async def dependency(matcher: Matcher, event: MessageEvent):
2022-10-22 18:12:55 +08:00
return await _match(matcher, event, msg, get_message_face, contain_reply)
2022-10-15 22:13:41 +08:00
return Depends(dependency)
2022-10-22 18:12:55 +08:00
def PlaintText(msg: Optional[str] = None, contain_reply: bool = True) -> str:
2022-10-15 22:13:41 +08:00
"""
说明:
2022-10-22 18:12:55 +08:00
获取纯文本且包括回复时含有msg时不能为空为空时提示并结束事件
2022-10-15 22:13:41 +08:00
参数:
:param msg: 提示文本
2022-10-22 18:12:55 +08:00
:param contain_reply: 包含回复内容
2022-10-15 22:13:41 +08:00
"""
2022-12-27 11:31:34 +08:00
2022-10-15 22:13:41 +08:00
async def dependency(matcher: Matcher, event: MessageEvent):
2022-10-22 18:12:55 +08:00
return await _match(matcher, event, msg, get_message_text, contain_reply)
2022-10-15 22:13:41 +08:00
return Depends(dependency)