This commit is contained in:
HibiKier
2023-03-19 21:05:34 +08:00
parent f8371e7330
commit 1046815d37
36 changed files with 704 additions and 582 deletions
+25 -17
View File
@@ -1,23 +1,26 @@
from nonebot import on_command
from nonebot.adapters.onebot.v11 import (
Bot,
MessageEvent,
GroupMessageEvent,
Message
)
from nonebot.params import CommandArg
from nonebot.rule import to_me
from configs.path_config import IMAGE_PATH, DATA_PATH
from utils.message_builder import image
from ._data_source import create_help_img, get_plugin_help
from ._utils import GROUP_HELP_PATH
import os
from nonebot import on_command
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent
from nonebot.params import CommandArg
from nonebot.rule import to_me
from configs.path_config import DATA_PATH, IMAGE_PATH
from services.log import logger
from utils.message_builder import image
from ._data_source import create_help_img, get_plugin_help
from ._utils import GROUP_HELP_PATH
__zx_plugin_name__ = "帮助"
__plugin_configs__ = {
"TYPE": {"value": "normal", "help": "帮助图片样式 ['normal', 'HTML']", "default_value": "normal"}
"TYPE": {
"value": "normal",
"help": "帮助图片样式 ['normal', 'HTML']",
"default_value": "normal",
"type": str,
}
}
simple_help_image = IMAGE_PATH / "simple_help.png"
@@ -25,7 +28,9 @@ if simple_help_image.exists():
simple_help_image.unlink()
simple_help = on_command("功能", rule=to_me(), aliases={"help", "帮助"}, priority=1, block=True)
simple_help = on_command(
"功能", rule=to_me(), aliases={"help", "帮助"}, priority=1, block=True
)
@simple_help.handle()
@@ -33,15 +38,18 @@ async def _(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
msg = arg.extract_plain_text().strip()
is_super = False
if msg:
if '-super' in msg:
if "-super" in msg:
if str(event.user_id) in bot.config.superusers:
is_super = True
msg = msg.replace('-super', '').strip()
msg = msg.replace("-super", "").strip()
msg = get_plugin_help(msg, is_super)
if msg:
await simple_help.send(image(b64=msg))
else:
await simple_help.send("没有此功能的帮助信息...")
logger.info(
f"查看帮助详情: {msg}", "帮助", event.user_id, getattr(event, "group_id", None)
)
else:
if isinstance(event, GroupMessageEvent):
_image_path = GROUP_HELP_PATH / f"{event.group_id}.png"
+26 -41
View File
@@ -1,14 +1,10 @@
from typing import Optional
from configs.path_config import IMAGE_PATH
from utils.image_utils import BuildImage
from utils.manager import admin_manager, plugin_data_manager, plugins2settings_manager
from ._utils import HelpImageBuild
from utils.image_utils import BuildImage
from configs.path_config import IMAGE_PATH
from utils.manager import (
plugins2settings_manager,
admin_manager,
)
from typing import Optional
import nonebot
random_bk_path = IMAGE_PATH / "background" / "help" / "simple_help"
@@ -36,36 +32,25 @@ def get_plugin_help(msg: str, is_super: bool = False) -> Optional[str]:
module = plugins2settings_manager.get_plugin_module(
msg
) or admin_manager.get_plugin_module(msg)
if module:
try:
plugin = nonebot.plugin.get_plugin(module)
metadata = plugin.metadata
if plugin:
if is_super:
result = plugin.module.__getattribute__(
"__plugin_superuser_usage__"
)
else:
result = (
metadata.usage
if metadata
else plugin.module.__getattribute__("__plugin_usage__")
)
if result:
width = 0
for x in result.split("\n"):
_width = len(x) * 24
width = width if width > _width else _width
height = len(result.split("\n")) * 45
A = BuildImage(width, height, font_size=24)
bk = BuildImage(
width,
height,
background=IMAGE_PATH / "background" / "1.png",
)
A.paste(bk, alpha=True)
A.text((int(width * 0.048), int(height * 0.21)), result)
return A.pic2bs4()
except AttributeError:
pass
if module and (plugin_data := plugin_data_manager.get(module)):
plugin_data.superuser_usage
if is_super:
result = plugin_data.superuser_usage
else:
result = plugin_data.usage
if result:
width = 0
for x in result.split("\n"):
_width = len(x) * 24
width = width if width > _width else _width
height = len(result.split("\n")) * 45
A = BuildImage(width, height, font_size=24)
bk = BuildImage(
width,
height,
background=IMAGE_PATH / "background" / "1.png",
)
A.paste(bk, alpha=True)
A.text((int(width * 0.048), int(height * 0.21)), result)
return A.pic2bs4()
return None
+11 -7
View File
@@ -51,9 +51,9 @@ class HelpImageBuild:
for key in self._data.keys():
plugin_data = self._data[key]
if plugin_data.plugin_type == PluginType.NORMAL:
if not self._sort_data.get(plugin_data.menu_type[0]):
self._sort_data[plugin_data.menu_type[0]] = []
self._sort_data[plugin_data.menu_type[0]].append(self._data[key])
if not self._sort_data.get(plugin_data.menu_type[0]): # type: ignore
self._sort_data[plugin_data.menu_type[0]] = [] # type: ignore
self._sort_data[plugin_data.menu_type[0]].append(self._data[key]) # type: ignore
async def build_image(self, group_id: Optional[int]):
if group_id:
@@ -71,6 +71,7 @@ class HelpImageBuild:
async def build_html_image(self, group_id: Optional[int]) -> bytes:
from nonebot_plugin_htmlrender import template_to_pic
self.sort_type()
classify = {}
for menu in self._sort_data:
@@ -180,9 +181,12 @@ class HelpImageBuild:
if (
not plugin_data.plugin_status.status
and plugin_data.plugin_status.block_type in ["group", "all"]
) or (group_id and not group_manager.get_plugin_super_status(
plugin_data.model, group_id
)):
) or (
group_id
and not group_manager.get_plugin_super_status(
plugin_data.model, group_id
)
):
w = curr_h + int(B.getsize(plugin_data.name)[1] / 2) + 2
pos = (
7,
@@ -191,7 +195,7 @@ class HelpImageBuild:
w,
)
if build_type == "VV":
name_image = await self.build_name_image(
name_image = await self.build_name_image( # type: ignore
max_width,
plugin_data.name,
"black" if not idx % 2 else "white",