mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
帮助新增HTML生成(新布局),添加配置TYPE切换
This commit is contained in:
parent
5810c28294
commit
380588c11d
@ -299,6 +299,7 @@ PS: **ARM平台** 请使用全量版 同时 **如果你的机器 RAM < 1G 可能
|
|||||||
### 2022/12/11
|
### 2022/12/11
|
||||||
|
|
||||||
* 词条问题支持真寻的昵称开头与at真寻开头并优化回复
|
* 词条问题支持真寻的昵称开头与at真寻开头并优化回复
|
||||||
|
* 帮助新增HTML生成(新布局),添加配置`TYPE`切换
|
||||||
* 更正私聊时功能管理回复错误
|
* 更正私聊时功能管理回复错误
|
||||||
* 修复加入新群聊时初始化功能开关错误
|
* 修复加入新群聊时初始化功能开关错误
|
||||||
* 添加单例注解
|
* 添加单例注解
|
||||||
|
|||||||
@ -9,15 +9,15 @@ from nonebot.params import CommandArg
|
|||||||
from nonebot.rule import to_me
|
from nonebot.rule import to_me
|
||||||
from configs.path_config import IMAGE_PATH, DATA_PATH
|
from configs.path_config import IMAGE_PATH, DATA_PATH
|
||||||
from utils.message_builder import image
|
from utils.message_builder import image
|
||||||
from .data_source import create_help_img, get_plugin_help
|
from ._data_source import create_help_img, get_plugin_help
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
__zx_plugin_name__ = "帮助"
|
__zx_plugin_name__ = "帮助"
|
||||||
|
|
||||||
# __plugin_configs__ = {
|
__plugin_configs__ = {
|
||||||
# "TYPE": {"value": "normal", "help": "帮助图片样式 ['normal', 'VV']", "default_value": "normal"}
|
"TYPE": {"value": "normal", "help": "帮助图片样式 ['normal', 'HTML']", "default_value": "normal"}
|
||||||
# }
|
}
|
||||||
|
|
||||||
group_help_path = DATA_PATH / "group_help"
|
group_help_path = DATA_PATH / "group_help"
|
||||||
simple_help_image = IMAGE_PATH / "simple_help.png"
|
simple_help_image = IMAGE_PATH / "simple_help.png"
|
||||||
|
|||||||
14
basic_plugins/help/_config.py
Normal file
14
basic_plugins/help/_config.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from typing import Optional, List, Any, Union, Dict
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class Item(BaseModel):
|
||||||
|
plugin_name: str
|
||||||
|
sta: int
|
||||||
|
|
||||||
|
|
||||||
|
class PluginList(BaseModel):
|
||||||
|
plugin_type: str
|
||||||
|
icon: str
|
||||||
|
logo: str
|
||||||
|
items: List[Item]
|
||||||
4
basic_plugins/help/data_source.py → basic_plugins/help/_data_source.py
Executable file → Normal file
4
basic_plugins/help/data_source.py → basic_plugins/help/_data_source.py
Executable file → Normal file
@ -1,4 +1,4 @@
|
|||||||
from .utils import HelpImageBuild
|
from ._utils import HelpImageBuild
|
||||||
from utils.image_utils import BuildImage
|
from utils.image_utils import BuildImage
|
||||||
from configs.path_config import IMAGE_PATH
|
from configs.path_config import IMAGE_PATH
|
||||||
from utils.manager import (
|
from utils.manager import (
|
||||||
@ -30,7 +30,7 @@ async def _create_help_img(group_id: Optional[int], help_image: Path):
|
|||||||
:param group_id: 群号
|
:param group_id: 群号
|
||||||
:param help_image: 图片路径
|
:param help_image: 图片路径
|
||||||
"""
|
"""
|
||||||
(await HelpImageBuild().build_image(group_id)).save(help_image)
|
await HelpImageBuild().build_image(group_id, help_image)
|
||||||
|
|
||||||
|
|
||||||
def get_plugin_help(msg: str, is_super: bool = False) -> Optional[str]:
|
def get_plugin_help(msg: str, is_super: bool = False) -> Optional[str]:
|
||||||
@ -1,5 +1,9 @@
|
|||||||
|
from pathlib import Path
|
||||||
from typing import List, Tuple, Dict, Optional
|
from typing import List, Tuple, Dict, Optional
|
||||||
from configs.path_config import IMAGE_PATH
|
from nonebot_plugin_htmlrender import template_to_pic
|
||||||
|
|
||||||
|
from ._config import Item
|
||||||
|
from configs.path_config import IMAGE_PATH, TEMPLATE_PATH
|
||||||
from utils.decorator import Singleton
|
from utils.decorator import Singleton
|
||||||
from utils.image_utils import BuildImage
|
from utils.image_utils import BuildImage
|
||||||
from configs.config import Config
|
from configs.config import Config
|
||||||
@ -9,8 +13,11 @@ import random
|
|||||||
from utils.manager import plugin_data_manager, group_manager
|
from utils.manager import plugin_data_manager, group_manager
|
||||||
from utils.manager.models import PluginData, PluginType
|
from utils.manager.models import PluginData, PluginType
|
||||||
|
|
||||||
|
|
||||||
background_path = IMAGE_PATH / "background" / "help" / "simple_help"
|
background_path = IMAGE_PATH / "background" / "help" / "simple_help"
|
||||||
|
|
||||||
|
logo_path = TEMPLATE_PATH / 'menu' / 'res' / 'logo'
|
||||||
|
|
||||||
|
|
||||||
async def build_help_image(image_group: List[List[BuildImage]], h: int):
|
async def build_help_image(image_group: List[List[BuildImage]], h: int):
|
||||||
bk = None
|
bk = None
|
||||||
@ -114,16 +121,29 @@ def group_image(image_list: List[BuildImage]) -> Tuple[List[List[BuildImage]], i
|
|||||||
class HelpImageBuild:
|
class HelpImageBuild:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
print('初始化咯')
|
|
||||||
self._data: Dict[str, PluginData] = plugin_data_manager.get_data()
|
self._data: Dict[str, PluginData] = plugin_data_manager.get_data()
|
||||||
self._sort_data: Dict[str, List[PluginData]] = {}
|
self._sort_data: Dict[str, List[PluginData]] = {}
|
||||||
self._image_list = []
|
self._image_list = []
|
||||||
|
self.icon2str = {
|
||||||
|
'normal': 'fa fa-cog',
|
||||||
|
'原神相关': 'fa fa-circle-o',
|
||||||
|
'常规插件': 'fa fa-cubes',
|
||||||
|
'联系管理员': 'fa fa-envelope-o',
|
||||||
|
'抽卡相关': 'fa fa-credit-card-alt',
|
||||||
|
'来点好康的': 'fa fa-picture-o',
|
||||||
|
'数据统计': 'fa fa-bar-chart',
|
||||||
|
'一些工具': 'fa fa-shopping-cart',
|
||||||
|
'商店': 'fa fa-shopping-cart',
|
||||||
|
'其它': 'fa fa-tags',
|
||||||
|
'群内小游戏': 'fa fa-gamepad',
|
||||||
|
}
|
||||||
|
|
||||||
def sort_type(self):
|
def sort_type(self):
|
||||||
"""
|
"""
|
||||||
说明:
|
说明:
|
||||||
对插件按照菜单类型分类
|
对插件按照菜单类型分类
|
||||||
"""
|
"""
|
||||||
|
if not self._sort_data.keys():
|
||||||
for key in self._data.keys():
|
for key in self._data.keys():
|
||||||
plugin_data = self._data[key]
|
plugin_data = self._data[key]
|
||||||
if plugin_data.plugin_type == PluginType.NORMAL:
|
if plugin_data.plugin_type == PluginType.NORMAL:
|
||||||
@ -144,7 +164,66 @@ class HelpImageBuild:
|
|||||||
await image.atext((0, 0), name, text_color, center_type="center")
|
await image.atext((0, 0), name, text_color, center_type="center")
|
||||||
return image
|
return image
|
||||||
|
|
||||||
async def build_image(self, group_id: Optional[int]) -> BuildImage:
|
async def build_image(self, group_id: Optional[int], help_image: Path):
|
||||||
|
build_type = Config.get_config("help", "TYPE")
|
||||||
|
if build_type == 'HTML':
|
||||||
|
byt = await self.build_html_image(group_id)
|
||||||
|
with open(help_image, 'wb') as f:
|
||||||
|
f.write(byt)
|
||||||
|
else:
|
||||||
|
img = await self.build_pil_image(group_id)
|
||||||
|
img.save(help_image)
|
||||||
|
|
||||||
|
async def build_html_image(self, group_id: Optional[int]) -> bytes:
|
||||||
|
self.sort_type()
|
||||||
|
classify = {}
|
||||||
|
for menu in self._sort_data:
|
||||||
|
for plugin in self._sort_data[menu]:
|
||||||
|
sta = 0
|
||||||
|
if not plugin.plugin_status.status and plugin.plugin_status.block_type:
|
||||||
|
if plugin.plugin_status.block_type in ['all', 'group']:
|
||||||
|
sta = 2
|
||||||
|
if not group_manager.get_plugin_super_status(plugin.model, group_id):
|
||||||
|
sta = 2
|
||||||
|
if not group_manager.get_plugin_status(plugin.model, group_id):
|
||||||
|
sta = 1
|
||||||
|
if classify.get(menu):
|
||||||
|
classify[menu].append(Item(plugin_name=plugin.name, sta=sta))
|
||||||
|
else:
|
||||||
|
classify[menu] = [Item(plugin_name=plugin.name, sta=sta)]
|
||||||
|
max_len = 0
|
||||||
|
flag_index = -1
|
||||||
|
max_data = None
|
||||||
|
plugin_list = []
|
||||||
|
for index, plu in enumerate(classify.keys()):
|
||||||
|
if plu in self.icon2str.keys():
|
||||||
|
icon = self.icon2str[plu]
|
||||||
|
else:
|
||||||
|
icon = 'fa fa-pencil-square-o'
|
||||||
|
logo = logo_path / random.choice(os.listdir(logo_path))
|
||||||
|
# print(str(logo.absolute()))
|
||||||
|
data = {'name': plu if plu != 'normal' else '功能', 'items': classify[plu], 'icon': icon,
|
||||||
|
'logo': str(logo.absolute())}
|
||||||
|
if len(classify[plu]) > max_len:
|
||||||
|
max_len = len(classify[plu])
|
||||||
|
flag_index = index
|
||||||
|
max_data = data
|
||||||
|
plugin_list.append(data)
|
||||||
|
del plugin_list[flag_index]
|
||||||
|
plugin_list.insert(0, max_data)
|
||||||
|
pic = await template_to_pic(
|
||||||
|
template_path=str((TEMPLATE_PATH / 'menu').absolute()),
|
||||||
|
template_name='zhenxun_menu.html',
|
||||||
|
templates={"plugin_list": plugin_list},
|
||||||
|
pages={
|
||||||
|
"viewport": {"width": 1903, "height": 975},
|
||||||
|
"base_url": f"file://{TEMPLATE_PATH}",
|
||||||
|
},
|
||||||
|
wait=2,
|
||||||
|
)
|
||||||
|
return pic
|
||||||
|
|
||||||
|
async def build_pil_image(self, group_id: Optional[int]) -> BuildImage:
|
||||||
"""
|
"""
|
||||||
说明:
|
说明:
|
||||||
构造帮助图片
|
构造帮助图片
|
||||||
@ -152,7 +231,6 @@ class HelpImageBuild:
|
|||||||
:param group_id: 群号
|
:param group_id: 群号
|
||||||
"""
|
"""
|
||||||
self._image_list = []
|
self._image_list = []
|
||||||
if not self._sort_data.keys():
|
|
||||||
self.sort_type()
|
self.sort_type()
|
||||||
font_size = 24
|
font_size = 24
|
||||||
build_type = Config.get_config("help", "TYPE")
|
build_type = Config.get_config("help", "TYPE")
|
||||||
Loading…
Reference in New Issue
Block a user