2025-01-10 18:39:23 +08:00
|
|
|
import nonebot
|
2024-09-14 05:23:55 +08:00
|
|
|
from nonebot_plugin_htmlrender import template_to_pic
|
2024-12-10 19:49:11 +08:00
|
|
|
from nonebot_plugin_uninfo import Uninfo
|
|
|
|
|
from pydantic import BaseModel
|
2024-09-14 05:23:55 +08:00
|
|
|
|
2024-10-18 18:57:55 +08:00
|
|
|
from zhenxun.configs.config import BotConfig
|
2024-09-14 05:23:55 +08:00
|
|
|
from zhenxun.configs.path_config import TEMPLATE_PATH
|
2025-01-10 18:39:23 +08:00
|
|
|
from zhenxun.configs.utils import PluginExtraData
|
2025-05-15 23:52:20 +08:00
|
|
|
from zhenxun.models.bot_console import BotConsole
|
2024-09-14 05:23:55 +08:00
|
|
|
from zhenxun.models.group_console import GroupConsole
|
2024-12-10 19:49:11 +08:00
|
|
|
from zhenxun.models.plugin_info import PluginInfo
|
|
|
|
|
from zhenxun.utils.enum import BlockType
|
|
|
|
|
from zhenxun.utils.platform import PlatformUtils
|
2024-09-14 05:23:55 +08:00
|
|
|
|
|
|
|
|
from ._utils import classify_plugin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Item(BaseModel):
|
|
|
|
|
plugin_name: str
|
|
|
|
|
"""插件名称"""
|
2025-01-10 18:39:23 +08:00
|
|
|
commands: list[str]
|
|
|
|
|
"""插件命令"""
|
2025-07-14 22:35:29 +08:00
|
|
|
id: str
|
|
|
|
|
"""插件id"""
|
|
|
|
|
status: bool
|
|
|
|
|
"""插件状态"""
|
|
|
|
|
has_superuser_help: bool
|
|
|
|
|
"""插件是否拥有超级用户帮助"""
|
2024-09-14 05:23:55 +08:00
|
|
|
|
|
|
|
|
|
2025-05-15 23:52:20 +08:00
|
|
|
def __handle_item(
|
|
|
|
|
bot: BotConsole | None,
|
|
|
|
|
plugin: PluginInfo,
|
|
|
|
|
group: GroupConsole | None,
|
|
|
|
|
is_detail: bool,
|
|
|
|
|
):
|
2024-09-14 05:23:55 +08:00
|
|
|
"""构造Item
|
|
|
|
|
|
|
|
|
|
参数:
|
2025-05-15 23:52:20 +08:00
|
|
|
bot: BotConsole
|
2024-09-14 05:23:55 +08:00
|
|
|
plugin: PluginInfo
|
|
|
|
|
group: 群组
|
2025-05-15 23:52:20 +08:00
|
|
|
is_detail: 是否为详细
|
2024-09-14 05:23:55 +08:00
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
Item: Item
|
|
|
|
|
"""
|
2025-07-14 22:35:29 +08:00
|
|
|
status = True
|
|
|
|
|
has_superuser_help = False
|
|
|
|
|
nb_plugin = nonebot.get_plugin_by_module_name(plugin.module_path)
|
|
|
|
|
if nb_plugin and nb_plugin.metadata and nb_plugin.metadata.extra:
|
|
|
|
|
extra_data = PluginExtraData(**nb_plugin.metadata.extra)
|
|
|
|
|
if extra_data.superuser_help:
|
|
|
|
|
has_superuser_help = True
|
2024-09-14 05:23:55 +08:00
|
|
|
if not plugin.status:
|
|
|
|
|
if plugin.block_type == BlockType.ALL:
|
2025-07-14 22:35:29 +08:00
|
|
|
status = False
|
2024-09-25 18:29:01 +08:00
|
|
|
elif group and plugin.block_type == BlockType.GROUP:
|
2025-07-14 22:35:29 +08:00
|
|
|
status = False
|
2024-09-25 18:29:01 +08:00
|
|
|
elif not group and plugin.block_type == BlockType.PRIVATE:
|
2025-07-14 22:35:29 +08:00
|
|
|
status = False
|
2024-09-25 18:29:01 +08:00
|
|
|
elif group and f"{plugin.module}," in group.block_plugin:
|
2025-07-14 22:35:29 +08:00
|
|
|
status = False
|
2025-05-15 23:52:20 +08:00
|
|
|
elif bot and f"{plugin.module}," in bot.block_plugins:
|
2025-07-14 22:35:29 +08:00
|
|
|
status = False
|
2025-01-10 18:39:23 +08:00
|
|
|
commands = []
|
|
|
|
|
nb_plugin = nonebot.get_plugin_by_module_name(plugin.module_path)
|
|
|
|
|
if is_detail and nb_plugin and nb_plugin.metadata and nb_plugin.metadata.extra:
|
|
|
|
|
extra_data = PluginExtraData(**nb_plugin.metadata.extra)
|
|
|
|
|
commands = [cmd.command for cmd in extra_data.commands]
|
2025-07-14 22:35:29 +08:00
|
|
|
return Item(
|
|
|
|
|
plugin_name=plugin.name,
|
|
|
|
|
commands=commands,
|
|
|
|
|
id=str(plugin.id),
|
|
|
|
|
status=status,
|
|
|
|
|
has_superuser_help=has_superuser_help,
|
|
|
|
|
)
|
2024-09-14 05:23:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_plugin_data(classify: dict[str, list[Item]]) -> list[dict[str, str]]:
|
|
|
|
|
"""构建前端插件数据
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
classify: 插件数据
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
list[dict[str, str]]: 前端插件数据
|
|
|
|
|
"""
|
2024-11-28 23:02:12 +08:00
|
|
|
classify = dict(sorted(classify.items(), key=lambda x: len(x[1]), reverse=True))
|
|
|
|
|
menu_key = next(iter(classify.keys()))
|
2024-09-14 05:23:55 +08:00
|
|
|
max_data = classify[menu_key]
|
|
|
|
|
del classify[menu_key]
|
|
|
|
|
plugin_list = [
|
|
|
|
|
{
|
|
|
|
|
"name": "主要功能" if menu in ["normal", "功能"] else menu,
|
|
|
|
|
"items": value,
|
|
|
|
|
}
|
|
|
|
|
for menu, value in classify.items()
|
|
|
|
|
]
|
2025-07-14 22:35:29 +08:00
|
|
|
plugin_list.insert(0, {"name": menu_key, "items": max_data})
|
2024-09-14 05:23:55 +08:00
|
|
|
for plugin in plugin_list:
|
2025-07-14 22:35:29 +08:00
|
|
|
plugin["items"].sort(key=lambda x: x.id)
|
|
|
|
|
return plugin_list
|
2024-09-14 05:23:55 +08:00
|
|
|
|
|
|
|
|
|
2025-01-10 18:39:23 +08:00
|
|
|
async def build_zhenxun_image(
|
|
|
|
|
session: Uninfo, group_id: str | None, is_detail: bool
|
|
|
|
|
) -> bytes:
|
2024-09-14 05:23:55 +08:00
|
|
|
"""构造真寻帮助图片
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
bot_id: bot_id
|
|
|
|
|
group_id: 群号
|
2025-01-10 18:39:23 +08:00
|
|
|
is_detail: 是否详细帮助
|
2024-09-14 05:23:55 +08:00
|
|
|
"""
|
2025-05-15 23:52:20 +08:00
|
|
|
classify = await classify_plugin(session, group_id, is_detail, __handle_item)
|
2024-09-14 05:23:55 +08:00
|
|
|
plugin_list = build_plugin_data(classify)
|
2024-10-18 18:57:55 +08:00
|
|
|
platform = PlatformUtils.get_platform(session)
|
|
|
|
|
bot_id = BotConfig.get_qbot_uid(session.self_id) or session.self_id
|
|
|
|
|
bot_ava = PlatformUtils.get_user_avatar_url(bot_id, platform)
|
2025-01-10 18:39:23 +08:00
|
|
|
width = int(637 * 1.5) if is_detail else 637
|
|
|
|
|
title_font = int(53 * 1.5) if is_detail else 53
|
|
|
|
|
tip_font = int(19 * 1.5) if is_detail else 19
|
2025-07-14 22:35:29 +08:00
|
|
|
plugin_count = sum(len(plugin["items"]) for plugin in plugin_list)
|
2024-09-14 05:23:55 +08:00
|
|
|
return await template_to_pic(
|
|
|
|
|
template_path=str((TEMPLATE_PATH / "ss_menu").absolute()),
|
|
|
|
|
template_name="main.html",
|
|
|
|
|
templates={
|
|
|
|
|
"data": {
|
|
|
|
|
"plugin_list": plugin_list,
|
2024-10-18 18:57:55 +08:00
|
|
|
"ava": bot_ava,
|
2025-01-10 18:39:23 +08:00
|
|
|
"width": width,
|
|
|
|
|
"font_size": (title_font, tip_font),
|
|
|
|
|
"is_detail": is_detail,
|
2025-07-14 22:35:29 +08:00
|
|
|
"plugin_count": plugin_count,
|
2024-09-14 05:23:55 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
pages={
|
2025-07-14 22:35:29 +08:00
|
|
|
"viewport": {"width": width, "height": 10},
|
2024-09-14 05:23:55 +08:00
|
|
|
"base_url": f"file://{TEMPLATE_PATH}",
|
|
|
|
|
},
|
|
|
|
|
wait=2,
|
|
|
|
|
)
|