mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
✨ 重构帮助,限制普通用户查询管理插件 (#1626)
This commit is contained in:
@@ -1,39 +1,82 @@
|
||||
import nonebot
|
||||
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.models.level_user import LevelUser
|
||||
from zhenxun.models.plugin_info import PluginInfo
|
||||
from zhenxun.configs.path_config import IMAGE_PATH
|
||||
from zhenxun.utils.image_utils import BuildImage, ImageTemplate
|
||||
|
||||
from ._utils import HelpImageBuild
|
||||
from .html_help import build_html_image
|
||||
from .normal_help import build_normal_image
|
||||
from .zhenxun_help import build_zhenxun_image
|
||||
from ._config import GROUP_HELP_PATH, SIMPLE_HELP_IMAGE, base_config
|
||||
|
||||
random_bk_path = IMAGE_PATH / "background" / "help" / "simple_help"
|
||||
|
||||
background = IMAGE_PATH / "background" / "0.png"
|
||||
|
||||
|
||||
async def create_help_img(bot_id: str, group_id: str | None):
|
||||
driver = nonebot.get_driver()
|
||||
|
||||
|
||||
async def create_help_img(bot_id: str, group_id: str | None, platform: str):
|
||||
"""生成帮助图片
|
||||
|
||||
参数:
|
||||
bot_id: bot id
|
||||
group_id: 群号
|
||||
platform: 平台
|
||||
"""
|
||||
await HelpImageBuild().build_image(bot_id, group_id)
|
||||
help_type: str = base_config.get("type")
|
||||
if help_type.lower() == "html":
|
||||
result = BuildImage.open(await build_html_image(group_id))
|
||||
elif help_type.lower() == "zhenxun":
|
||||
result = BuildImage.open(await build_zhenxun_image(bot_id, group_id, platform))
|
||||
else:
|
||||
result = await build_normal_image(group_id)
|
||||
if group_id:
|
||||
await result.save(GROUP_HELP_PATH / f"{group_id}.png")
|
||||
else:
|
||||
await result.save(SIMPLE_HELP_IMAGE)
|
||||
|
||||
|
||||
async def get_plugin_help(name: str, is_superuser: bool) -> str | BuildImage:
|
||||
async def get_user_allow_help(user_id: str) -> list[PluginType]:
|
||||
"""获取用户可访问插件类型列表
|
||||
|
||||
参数:
|
||||
user_id: 用户id
|
||||
|
||||
返回:
|
||||
list[PluginType]: 插件类型列表
|
||||
"""
|
||||
type_list = [PluginType.NORMAL, PluginType.DEPENDANT]
|
||||
for level in await LevelUser.filter(user_id=user_id).values_list(
|
||||
"user_level", flat=True
|
||||
):
|
||||
if level > 0: # type: ignore
|
||||
type_list.extend((PluginType.ADMIN, PluginType.SUPER_AND_ADMIN))
|
||||
break
|
||||
if user_id in driver.config.superusers:
|
||||
type_list.append(PluginType.SUPERUSER)
|
||||
return type_list
|
||||
|
||||
|
||||
async def get_plugin_help(
|
||||
user_id: str, name: str, is_superuser: bool
|
||||
) -> str | BuildImage:
|
||||
"""获取功能的帮助信息
|
||||
|
||||
参数:
|
||||
user_id: 用户id
|
||||
name: 插件名称或id
|
||||
is_superuser: 是否为超级用户
|
||||
"""
|
||||
type_list = await get_user_allow_help(user_id)
|
||||
if name.isdigit():
|
||||
plugin = await PluginInfo.get_or_none(id=int(name))
|
||||
plugin = await PluginInfo.get_or_none(id=int(name), plugin_type__in=type_list)
|
||||
else:
|
||||
plugin = await PluginInfo.get_or_none(
|
||||
name__iexact=name, load_status=True, plugin_type__not=PluginType.PARENT
|
||||
name__iexact=name, load_status=True, plugin_type__in=type_list
|
||||
)
|
||||
if plugin:
|
||||
_plugin = nonebot.get_plugin_by_module_name(plugin.module_path)
|
||||
|
||||
Reference in New Issue
Block a user