zhenxun_bot/basic_plugins/super_help/data_source.py

80 lines
3.0 KiB
Python
Raw Normal View History

2021-12-16 11:16:28 +08:00
from utils.image_utils import BuildImage
2021-10-03 14:24:07 +08:00
from configs.path_config import IMAGE_PATH
from services.log import logger
from utils.utils import get_matchers
2022-02-19 18:20:19 +08:00
from nonebot.adapters.onebot.v11 import Bot
2021-10-03 14:24:07 +08:00
from nonebot import Driver
import asyncio
import nonebot
driver: Driver = nonebot.get_driver()
2022-02-19 18:20:19 +08:00
background = IMAGE_PATH / "background" / "0.png"
2021-10-03 14:24:07 +08:00
2022-02-19 18:20:19 +08:00
superuser_help_image = IMAGE_PATH / "superuser_help.png"
2021-10-03 14:24:07 +08:00
@driver.on_bot_connect
async def create_help_image(bot: Bot = None):
"""
创建超级用户帮助图片
"""
await asyncio.get_event_loop().run_in_executor(None, _create_help_image)
def _create_help_image():
"""
创建管理员帮助图片
"""
_matchers = get_matchers()
_plugin_name_list = []
width = 0
help_str = "超级用户帮助\n\n* 注: * 代表可有多个相同参数 ? 代表可省略该参数 *\n\n"
2021-12-16 11:16:28 +08:00
tmp_img = BuildImage(0, 0, plain_text='1', font_size=24)
2021-10-03 14:24:07 +08:00
for matcher in _matchers:
2021-11-04 16:11:50 +08:00
plugin_name = ""
2021-10-03 14:24:07 +08:00
try:
2022-02-19 18:20:19 +08:00
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
2021-10-03 14:24:07 +08:00
_module = _plugin.module
try:
plugin_name = _module.__getattribute__("__zx_plugin_name__")
except AttributeError:
continue
is_superuser_usage = False
try:
_ = _module.__getattribute__("__plugin_superuser_usage__")
is_superuser_usage = True
except AttributeError:
pass
if (
("[superuser]" in plugin_name.lower() or is_superuser_usage)
and plugin_name != "超级用户帮助 [Superuser]"
and plugin_name not in _plugin_name_list
and "[hidden]" not in plugin_name.lower()
):
_plugin_name_list.append(plugin_name)
try:
plugin_des = _module.__getattribute__("__plugin_des__")
except AttributeError:
plugin_des = '_'
plugin_cmd = _module.__getattribute__("__plugin_cmd__")
if is_superuser_usage:
plugin_cmd = [x for x in plugin_cmd if "[_superuser]" in x]
plugin_cmd = " / ".join(plugin_cmd).replace('[_superuser]', '').strip()
help_str += f"{plugin_des} -> {plugin_cmd}\n\n"
x = tmp_img.getsize(f"{plugin_des} -> {plugin_cmd}")[0]
width = width if width > x else x
except Exception as e:
logger.warning(
2022-02-19 18:20:19 +08:00
f"获取超级用户插件 {matcher.plugin_name}: {plugin_name} 设置失败... {type(e)}{e}"
2021-10-03 14:24:07 +08:00
)
height = len(help_str.split("\n")) * 33
width += 500
2021-12-16 11:16:28 +08:00
A = BuildImage(width, height, font_size=24)
_background = BuildImage(width, height, background=background)
2021-10-03 14:24:07 +08:00
A.text((300, 140), help_str)
A.paste(_background, alpha=True)
A.save(superuser_help_image)
logger.info(f"已成功加载 {len(_plugin_name_list)} 条超级用户命令")