2025-01-10 18:39:23 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2024-02-25 03:18:34 +08:00
|
|
|
import nonebot
|
2025-06-23 15:33:46 +08:00
|
|
|
from nonebot.plugin import PluginMetadata
|
|
|
|
|
from nonebot_plugin_htmlrender import template_to_pic
|
2024-10-18 18:57:55 +08:00
|
|
|
from nonebot_plugin_uninfo import Uninfo
|
2024-02-25 03:18:34 +08:00
|
|
|
|
2025-06-23 15:33:46 +08:00
|
|
|
from zhenxun.configs.config import Config
|
|
|
|
|
from zhenxun.configs.path_config import IMAGE_PATH, TEMPLATE_PATH
|
|
|
|
|
from zhenxun.configs.utils import PluginExtraData
|
2024-09-14 05:23:55 +08:00
|
|
|
from zhenxun.models.level_user import LevelUser
|
2024-02-25 03:18:34 +08:00
|
|
|
from zhenxun.models.plugin_info import PluginInfo
|
2025-06-23 15:33:46 +08:00
|
|
|
from zhenxun.models.statistics import Statistics
|
|
|
|
|
from zhenxun.utils._image_template import ImageTemplate
|
2024-12-10 19:49:11 +08:00
|
|
|
from zhenxun.utils.enum import PluginType
|
2025-06-23 15:33:46 +08:00
|
|
|
from zhenxun.utils.image_utils import BuildImage
|
2024-02-25 03:18:34 +08:00
|
|
|
|
2025-01-10 18:39:23 +08:00
|
|
|
from ._config import (
|
|
|
|
|
GROUP_HELP_PATH,
|
|
|
|
|
SIMPLE_DETAIL_HELP_IMAGE,
|
|
|
|
|
SIMPLE_HELP_IMAGE,
|
|
|
|
|
base_config,
|
|
|
|
|
)
|
2024-09-14 05:23:55 +08:00
|
|
|
from .html_help import build_html_image
|
|
|
|
|
from .normal_help import build_normal_image
|
|
|
|
|
from .zhenxun_help import build_zhenxun_image
|
2024-02-25 03:18:34 +08:00
|
|
|
|
|
|
|
|
random_bk_path = IMAGE_PATH / "background" / "help" / "simple_help"
|
|
|
|
|
|
|
|
|
|
background = IMAGE_PATH / "background" / "0.png"
|
|
|
|
|
|
|
|
|
|
|
2024-09-14 05:23:55 +08:00
|
|
|
driver = nonebot.get_driver()
|
|
|
|
|
|
|
|
|
|
|
2025-01-10 18:39:23 +08:00
|
|
|
async def create_help_img(
|
|
|
|
|
session: Uninfo, group_id: str | None, is_detail: bool
|
|
|
|
|
) -> Path:
|
2024-08-28 19:08:22 +08:00
|
|
|
"""生成帮助图片
|
|
|
|
|
|
2024-02-25 03:18:34 +08:00
|
|
|
参数:
|
2024-10-18 18:57:55 +08:00
|
|
|
session: Uninfo
|
2024-08-28 19:08:22 +08:00
|
|
|
group_id: 群号
|
2024-09-14 05:23:55 +08:00
|
|
|
"""
|
2024-12-21 20:46:00 +08:00
|
|
|
help_type = base_config.get("type", "").strip().lower()
|
|
|
|
|
|
|
|
|
|
match help_type:
|
|
|
|
|
case "html":
|
2025-05-15 23:52:20 +08:00
|
|
|
result = BuildImage.open(
|
|
|
|
|
await build_html_image(session, group_id, is_detail)
|
|
|
|
|
)
|
2024-12-21 20:46:00 +08:00
|
|
|
case "zhenxun":
|
2025-01-10 18:39:23 +08:00
|
|
|
result = BuildImage.open(
|
|
|
|
|
await build_zhenxun_image(session, group_id, is_detail)
|
|
|
|
|
)
|
2024-12-21 20:46:00 +08:00
|
|
|
case _:
|
2025-01-10 18:39:23 +08:00
|
|
|
result = await build_normal_image(group_id, is_detail)
|
|
|
|
|
if group_id:
|
|
|
|
|
save_path = GROUP_HELP_PATH / f"{group_id}_{is_detail}.png"
|
|
|
|
|
elif is_detail:
|
|
|
|
|
save_path = SIMPLE_DETAIL_HELP_IMAGE
|
|
|
|
|
else:
|
|
|
|
|
save_path = SIMPLE_HELP_IMAGE
|
2024-12-21 20:46:00 +08:00
|
|
|
await result.save(save_path)
|
2025-01-10 18:39:23 +08:00
|
|
|
return save_path
|
2024-09-14 05:23:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_user_allow_help(user_id: str) -> list[PluginType]:
|
|
|
|
|
"""获取用户可访问插件类型列表
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
user_id: 用户id
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
list[PluginType]: 插件类型列表
|
2024-02-25 03:18:34 +08:00
|
|
|
"""
|
2024-09-14 05:23:55 +08:00
|
|
|
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
|
2024-02-25 03:18:34 +08:00
|
|
|
|
|
|
|
|
|
2025-06-23 15:33:46 +08:00
|
|
|
async def get_normal_help(
|
|
|
|
|
metadata: PluginMetadata, extra: PluginExtraData, is_superuser: bool
|
|
|
|
|
) -> str | bytes:
|
|
|
|
|
"""构建默认帮助详情
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
metadata: PluginMetadata
|
|
|
|
|
extra: PluginExtraData
|
|
|
|
|
is_superuser: 是否超级用户帮助
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
str | bytes: 返回信息
|
|
|
|
|
"""
|
|
|
|
|
items = None
|
|
|
|
|
if is_superuser:
|
|
|
|
|
if usage := extra.superuser_help:
|
|
|
|
|
items = {
|
|
|
|
|
"简介": metadata.description,
|
|
|
|
|
"用法": usage,
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
items = {
|
|
|
|
|
"简介": metadata.description,
|
|
|
|
|
"用法": metadata.usage,
|
|
|
|
|
}
|
|
|
|
|
if items:
|
|
|
|
|
return (await ImageTemplate.hl_page(metadata.name, items)).pic2bytes()
|
|
|
|
|
return "该功能没有帮助信息"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def min_leading_spaces(str_list: list[str]) -> int:
|
|
|
|
|
min_spaces = 9999
|
|
|
|
|
|
|
|
|
|
for s in str_list:
|
|
|
|
|
leading_spaces = len(s) - len(s.lstrip(" "))
|
|
|
|
|
|
|
|
|
|
if leading_spaces < min_spaces:
|
|
|
|
|
min_spaces = leading_spaces
|
|
|
|
|
|
|
|
|
|
return min_spaces if min_spaces != 9999 else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def split_text(text: str):
|
|
|
|
|
split_text = text.split("\n")
|
|
|
|
|
min_spaces = min_leading_spaces(split_text)
|
|
|
|
|
if min_spaces > 0:
|
|
|
|
|
split_text = [s[min_spaces:] for s in split_text]
|
|
|
|
|
return [s.replace(" ", " ") for s in split_text]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_zhenxun_help(
|
|
|
|
|
module: str, metadata: PluginMetadata, extra: PluginExtraData, is_superuser: bool
|
|
|
|
|
) -> str | bytes:
|
|
|
|
|
"""构建ZhenXun帮助详情
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
module: 模块名
|
|
|
|
|
metadata: PluginMetadata
|
|
|
|
|
extra: PluginExtraData
|
|
|
|
|
is_superuser: 是否超级用户帮助
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
str | bytes: 返回信息
|
|
|
|
|
"""
|
|
|
|
|
call_count = await Statistics.filter(plugin_name=module).count()
|
|
|
|
|
usage = metadata.usage
|
|
|
|
|
if is_superuser:
|
|
|
|
|
if not extra.superuser_help:
|
|
|
|
|
return "该功能没有超级用户帮助信息"
|
|
|
|
|
usage = extra.superuser_help
|
|
|
|
|
return await template_to_pic(
|
|
|
|
|
template_path=str((TEMPLATE_PATH / "help_detail").absolute()),
|
|
|
|
|
template_name="main.html",
|
|
|
|
|
templates={
|
|
|
|
|
"title": metadata.name,
|
|
|
|
|
"author": extra.author,
|
|
|
|
|
"version": extra.version,
|
|
|
|
|
"call_count": call_count,
|
|
|
|
|
"descriptions": split_text(metadata.description),
|
|
|
|
|
"usages": split_text(usage),
|
|
|
|
|
},
|
|
|
|
|
pages={
|
|
|
|
|
"viewport": {"width": 824, "height": 590},
|
|
|
|
|
"base_url": f"file://{TEMPLATE_PATH}",
|
|
|
|
|
},
|
|
|
|
|
wait=2,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_plugin_help(user_id: str, name: str, is_superuser: bool) -> str | bytes:
|
2024-02-25 03:18:34 +08:00
|
|
|
"""获取功能的帮助信息
|
|
|
|
|
|
|
|
|
|
参数:
|
2024-09-14 05:23:55 +08:00
|
|
|
user_id: 用户id
|
2024-07-31 18:34:22 +08:00
|
|
|
name: 插件名称或id
|
2024-05-15 23:24:35 +08:00
|
|
|
is_superuser: 是否为超级用户
|
2024-02-25 03:18:34 +08:00
|
|
|
"""
|
2024-09-14 05:23:55 +08:00
|
|
|
type_list = await get_user_allow_help(user_id)
|
2024-07-31 18:34:22 +08:00
|
|
|
if name.isdigit():
|
2024-09-14 05:23:55 +08:00
|
|
|
plugin = await PluginInfo.get_or_none(id=int(name), plugin_type__in=type_list)
|
2024-07-31 18:34:22 +08:00
|
|
|
else:
|
2024-09-07 13:54:25 +08:00
|
|
|
plugin = await PluginInfo.get_or_none(
|
2024-09-14 05:23:55 +08:00
|
|
|
name__iexact=name, load_status=True, plugin_type__in=type_list
|
2024-09-07 13:54:25 +08:00
|
|
|
)
|
2024-07-31 18:34:22 +08:00
|
|
|
if plugin:
|
2024-02-25 03:18:34 +08:00
|
|
|
_plugin = nonebot.get_plugin_by_module_name(plugin.module_path)
|
|
|
|
|
if _plugin and _plugin.metadata:
|
2025-06-23 15:33:46 +08:00
|
|
|
extra_data = PluginExtraData(**_plugin.metadata.extra)
|
|
|
|
|
if Config.get_config("help", "detail_type") == "zhenxun":
|
|
|
|
|
return await get_zhenxun_help(
|
|
|
|
|
plugin.module, _plugin.metadata, extra_data, is_superuser
|
|
|
|
|
)
|
2024-05-15 23:24:35 +08:00
|
|
|
else:
|
2025-06-23 15:33:46 +08:00
|
|
|
return await get_normal_help(_plugin.metadata, extra_data, is_superuser)
|
2024-02-25 03:18:34 +08:00
|
|
|
return "糟糕! 该功能没有帮助喔..."
|
|
|
|
|
return "没有查找到这个功能噢..."
|