zhenxun_bot/zhenxun/builtin_plugins/help/__init__.py

85 lines
2.5 KiB
Python
Raw Normal View History

2024-05-15 23:24:35 +08:00
from nonebot.adapters import Bot
2024-02-25 03:18:34 +08:00
from nonebot.plugin import PluginMetadata
from nonebot.rule import to_me
2024-08-04 19:34:02 +08:00
from nonebot_plugin_alconna import (
Alconna,
AlconnaQuery,
2024-08-04 19:34:02 +08:00
Args,
Match,
2024-08-28 19:08:22 +08:00
Option,
Query,
2024-08-04 19:34:02 +08:00
on_alconna,
store_true,
)
from nonebot_plugin_uninfo import Uninfo
2024-02-25 03:18:34 +08:00
from zhenxun.builtin_plugins.help._config import GROUP_HELP_PATH, SIMPLE_HELP_IMAGE
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
2024-02-25 03:18:34 +08:00
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
2024-08-10 02:25:04 +08:00
from zhenxun.utils.message import MessageUtils
2024-02-25 03:18:34 +08:00
2024-08-28 19:08:22 +08:00
from ._data_source import create_help_img, get_plugin_help
2024-02-25 03:18:34 +08:00
__plugin_meta__ = PluginMetadata(
name="帮助",
description="帮助",
usage="",
extra=PluginExtraData(
author="HibiKier",
version="0.1",
2024-08-18 22:26:53 +08:00
plugin_type=PluginType.DEPENDANT,
is_show=False,
2024-02-25 03:18:34 +08:00
configs=[
RegisterConfig(
key="type",
value="normal",
2024-08-28 19:08:22 +08:00
help="帮助图片样式 ['normal', 'HTML', 'zhenxun']",
default_value="zhenxun",
2024-02-25 03:18:34 +08:00
)
],
).to_dict(),
2024-02-25 03:18:34 +08:00
)
_matcher = on_alconna(
Alconna(
"功能",
Args["name?", str],
2024-08-04 19:34:02 +08:00
Option("-s|--superuser", action=store_true, help_text="超级用户帮助"),
2024-02-25 03:18:34 +08:00
),
aliases={"help", "帮助", "菜单"},
2024-02-25 03:18:34 +08:00
rule=to_me(),
priority=1,
block=True,
)
@_matcher.handle()
async def _(
2024-05-15 23:24:35 +08:00
bot: Bot,
2024-02-25 03:18:34 +08:00
name: Match[str],
2024-10-18 18:57:55 +08:00
session: Uninfo,
2024-08-04 19:34:02 +08:00
is_superuser: Query[bool] = AlconnaQuery("superuser.value", False),
2024-02-25 03:18:34 +08:00
):
2024-08-28 19:08:22 +08:00
_is_superuser = is_superuser.result if is_superuser.available else False
2024-02-25 03:18:34 +08:00
if name.available:
2024-10-18 18:57:55 +08:00
if _is_superuser and session.user.id not in bot.config.superusers:
2024-05-15 23:24:35 +08:00
_is_superuser = False
2024-10-18 18:57:55 +08:00
if result := await get_plugin_help(session.user.id, name.result, _is_superuser):
2024-08-28 19:08:22 +08:00
await MessageUtils.build_message(result).send(reply_to=True)
2024-02-25 03:18:34 +08:00
else:
2024-08-10 02:25:04 +08:00
await MessageUtils.build_message("没有此功能的帮助信息...").send(
reply_to=True
)
2024-08-28 19:08:22 +08:00
logger.info(f"查看帮助详情: {name.result}", "帮助", session=session)
2024-10-18 18:57:55 +08:00
elif session.group and (gid := session.group.id):
2024-08-28 19:08:22 +08:00
_image_path = GROUP_HELP_PATH / f"{gid}.png"
if not _image_path.exists():
2024-10-18 18:57:55 +08:00
await create_help_img(session, gid)
2024-08-28 19:08:22 +08:00
await MessageUtils.build_message(_image_path).finish()
2024-02-25 03:18:34 +08:00
else:
2024-08-28 19:08:22 +08:00
if not SIMPLE_HELP_IMAGE.exists():
2024-10-18 18:57:55 +08:00
await create_help_img(session, None)
2024-08-28 19:08:22 +08:00
await MessageUtils.build_message(SIMPLE_HELP_IMAGE).finish()