Skip to content

Commit

Permalink
✨ 新增好感度/金币设置和详细帮助 (#1831)
Browse files Browse the repository at this point in the history
* ✨ 新增好感度/金币设置和详细帮助

* 🔧 修复html帮助
  • Loading branch information
HibiKier authored Jan 10, 2025
1 parent dc143c0 commit 264929e
Show file tree
Hide file tree
Showing 22 changed files with 281 additions and 44 deletions.
9 changes: 8 additions & 1 deletion zhenxun/builtin_plugins/chat_history/chat_message_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from nonebot_plugin_session import EventSession
import pytz

from zhenxun.configs.utils import PluginExtraData
from zhenxun.configs.utils import Command, PluginExtraData
from zhenxun.models.chat_history import ChatHistory
from zhenxun.models.group_member_info import GroupInfoUser
from zhenxun.services.log import logger
Expand Down Expand Up @@ -45,6 +45,13 @@
version="0.1",
plugin_type=PluginType.NORMAL,
menu_type="数据统计",
commands=[
Command(command="消息统计"),
Command(command="日消息统计"),
Command(command="周消息排行"),
Command(command="月消息排行"),
Command(command="年消息排行"),
],
).to_dict(),
)

Expand Down
30 changes: 24 additions & 6 deletions zhenxun/builtin_plugins/help/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
)
from nonebot_plugin_uninfo import Uninfo

from zhenxun.builtin_plugins.help._config import GROUP_HELP_PATH, SIMPLE_HELP_IMAGE
from zhenxun.builtin_plugins.help._config import (
GROUP_HELP_PATH,
SIMPLE_DETAIL_HELP_IMAGE,
SIMPLE_HELP_IMAGE,
)
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
Expand Down Expand Up @@ -47,6 +51,7 @@
"功能",
Args["name?", str],
Option("-s|--superuser", action=store_true, help_text="超级用户帮助"),
Option("-d|--detail", action=store_true, help_text="详细帮助"),
),
aliases={"help", "帮助", "菜单"},
rule=to_me(),
Expand All @@ -55,12 +60,21 @@
)


_matcher.shortcut(
r"详细帮助",
command="功能",
arguments=["--detail"],
prefix=True,
)


@_matcher.handle()
async def _(
bot: Bot,
name: Match[str],
session: Uninfo,
is_superuser: Query[bool] = AlconnaQuery("superuser.value", False),
is_detail: Query[bool] = AlconnaQuery("detail.value", False),
):
_is_superuser = is_superuser.result if is_superuser.available else False
if name.available:
Expand All @@ -74,11 +88,15 @@ async def _(
)
logger.info(f"查看帮助详情: {name.result}", "帮助", session=session)
elif session.group and (gid := session.group.id):
_image_path = GROUP_HELP_PATH / f"{gid}.png"
_image_path = GROUP_HELP_PATH / f"{gid}_{is_detail.result}.png"
if not _image_path.exists():
await create_help_img(session, gid)
result = await create_help_img(session, gid, is_detail.result)
await MessageUtils.build_message(_image_path).finish()
else:
if not SIMPLE_HELP_IMAGE.exists():
await create_help_img(session, None)
await MessageUtils.build_message(SIMPLE_HELP_IMAGE).finish()
if is_detail.result:
_image_path = SIMPLE_DETAIL_HELP_IMAGE
else:
_image_path = SIMPLE_HELP_IMAGE
if not _image_path.exists():
result = await create_help_img(session, None, is_detail.result)
await MessageUtils.build_message(_image_path).finish()
4 changes: 4 additions & 0 deletions zhenxun/builtin_plugins/help/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
if SIMPLE_HELP_IMAGE.exists():
SIMPLE_HELP_IMAGE.unlink()

SIMPLE_DETAIL_HELP_IMAGE = IMAGE_PATH / "SIMPLE_DETAIL_HELP.png"
if SIMPLE_DETAIL_HELP_IMAGE.exists():
SIMPLE_DETAIL_HELP_IMAGE.unlink()

base_config = Config.get("help")
30 changes: 23 additions & 7 deletions zhenxun/builtin_plugins/help/_data_source.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import nonebot
from nonebot_plugin_uninfo import Uninfo

Expand All @@ -7,7 +9,12 @@
from zhenxun.utils.enum import PluginType
from zhenxun.utils.image_utils import BuildImage, ImageTemplate

from ._config import GROUP_HELP_PATH, SIMPLE_HELP_IMAGE, base_config
from ._config import (
GROUP_HELP_PATH,
SIMPLE_DETAIL_HELP_IMAGE,
SIMPLE_HELP_IMAGE,
base_config,
)
from .html_help import build_html_image
from .normal_help import build_normal_image
from .zhenxun_help import build_zhenxun_image
Expand All @@ -20,7 +27,9 @@
driver = nonebot.get_driver()


async def create_help_img(session: Uninfo, group_id: str | None):
async def create_help_img(
session: Uninfo, group_id: str | None, is_detail: bool
) -> Path:
"""生成帮助图片
参数:
Expand All @@ -31,14 +40,21 @@ async def create_help_img(session: Uninfo, group_id: str | None):

match help_type:
case "html":
result = BuildImage.open(await build_html_image(group_id))
result = BuildImage.open(await build_html_image(group_id, is_detail))
case "zhenxun":
result = BuildImage.open(await build_zhenxun_image(session, group_id))
result = BuildImage.open(
await build_zhenxun_image(session, group_id, is_detail)
)
case _:
result = await build_normal_image(group_id)

save_path = GROUP_HELP_PATH / f"{group_id}.png" if group_id else SIMPLE_HELP_IMAGE
result = await build_normal_image(group_id, is_detail)
if group_id:
save_path = GROUP_HELP_PATH / f"{group_id}_{is_detail}.png"
elif is_detail:
save_path = SIMPLE_DETAIL_HELP_IMAGE
else:
save_path = SIMPLE_HELP_IMAGE
await result.save(save_path)
return save_path


async def get_user_allow_help(user_id: str) -> list[PluginType]:
Expand Down
7 changes: 5 additions & 2 deletions zhenxun/builtin_plugins/help/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ async def sort_type() -> dict[str, list[PluginInfo]]:
return sort_data


async def classify_plugin(group_id: str | None, handle: Callable) -> dict[str, list]:
async def classify_plugin(
group_id: str | None, is_detail: bool, handle: Callable
) -> dict[str, list]:
"""对插件进行分类并判断状态
参数:
group_id: 群组id
is_detail: 是否详细帮助
返回:
dict[str, list[Item]]: 分类插件数据
Expand All @@ -42,5 +45,5 @@ async def classify_plugin(group_id: str | None, handle: Callable) -> dict[str, l
for plugin in value:
if not classify.get(menu):
classify[menu] = []
classify[menu].append(handle(plugin, group))
classify[menu].append(handle(plugin, group, is_detail))
return classify
10 changes: 7 additions & 3 deletions zhenxun/builtin_plugins/help/html_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ class PluginList(BaseModel):
}


def __handle_item(plugin: PluginInfo, group: GroupConsole | None) -> Item:
def __handle_item(
plugin: PluginInfo, group: GroupConsole | None, is_detail: bool
) -> Item:
"""构造Item
参数:
plugin: PluginInfo
group: 群组
is_detail: 是否详细
返回:
Item: Item
Expand Down Expand Up @@ -116,13 +119,14 @@ def build_plugin_data(classify: dict[str, list[Item]]) -> list[dict[str, str]]:
return plugin_list


async def build_html_image(group_id: str | None) -> bytes:
async def build_html_image(group_id: str | None, is_detail: bool) -> bytes:
"""构造HTML帮助图片
参数:
group_id: 群号
is_detail: 是否详细帮助
"""
classify = await classify_plugin(group_id, __handle_item)
classify = await classify_plugin(group_id, is_detail, __handle_item)
plugin_list = build_plugin_data(classify)
return await template_to_pic(
template_path=str((TEMPLATE_PATH / "menu").absolute()),
Expand Down
3 changes: 2 additions & 1 deletion zhenxun/builtin_plugins/help/normal_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
BACKGROUND_PATH = IMAGE_PATH / "background" / "help" / "simple_help"


async def build_normal_image(group_id: str | None) -> BuildImage:
async def build_normal_image(group_id: str | None, is_detail: bool) -> BuildImage:
"""构造PIL帮助图片
参数:
group_id: 群号
is_detail: 详细帮助
"""
image_list = []
font_size = 24
Expand Down
28 changes: 23 additions & 5 deletions zhenxun/builtin_plugins/help/zhenxun_help.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import nonebot
from nonebot_plugin_htmlrender import template_to_pic
from nonebot_plugin_uninfo import Uninfo
from pydantic import BaseModel

from zhenxun.configs.config import BotConfig
from zhenxun.configs.path_config import TEMPLATE_PATH
from zhenxun.configs.utils import PluginExtraData
from zhenxun.models.group_console import GroupConsole
from zhenxun.models.plugin_info import PluginInfo
from zhenxun.utils.enum import BlockType
Expand All @@ -15,9 +17,11 @@
class Item(BaseModel):
plugin_name: str
"""插件名称"""
commands: list[str]
"""插件命令"""


def __handle_item(plugin: PluginInfo, group: GroupConsole | None):
def __handle_item(plugin: PluginInfo, group: GroupConsole | None, is_detail: bool):
"""构造Item
参数:
Expand All @@ -36,7 +40,12 @@ def __handle_item(plugin: PluginInfo, group: GroupConsole | None):
plugin.name = f"{plugin.name}(不可用)"
elif group and f"{plugin.module}," in group.block_plugin:
plugin.name = f"{plugin.name}(不可用)"
return Item(plugin_name=f"{plugin.id}-{plugin.name}")
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]
return Item(plugin_name=f"{plugin.id}-{plugin.name}", commands=commands)


def build_plugin_data(classify: dict[str, list[Item]]) -> list[dict[str, str]]:
Expand Down Expand Up @@ -123,29 +132,38 @@ def build_line_data(plugin_list: list[dict]) -> list[dict]:
return data


async def build_zhenxun_image(session: Uninfo, group_id: str | None) -> bytes:
async def build_zhenxun_image(
session: Uninfo, group_id: str | None, is_detail: bool
) -> bytes:
"""构造真寻帮助图片
参数:
bot_id: bot_id
group_id: 群号
is_detail: 是否详细帮助
"""
classify = await classify_plugin(group_id, __handle_item)
classify = await classify_plugin(group_id, is_detail, __handle_item)
plugin_list = build_plugin_data(classify)
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)
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
return await template_to_pic(
template_path=str((TEMPLATE_PATH / "ss_menu").absolute()),
template_name="main.html",
templates={
"data": {
"plugin_list": plugin_list,
"ava": bot_ava,
"width": width,
"font_size": (title_font, tip_font),
"is_detail": is_detail,
}
},
pages={
"viewport": {"width": 637, "height": 453},
"viewport": {"width": width, "height": 453},
"base_url": f"file://{TEMPLATE_PATH}",
},
wait=2,
Expand Down
6 changes: 4 additions & 2 deletions zhenxun/builtin_plugins/info/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from nonebot_plugin_uninfo import Uninfo
from playwright.async_api import TimeoutError

from zhenxun.configs.utils import PluginExtraData
from zhenxun.configs.utils import Command, PluginExtraData
from zhenxun.models.group_member_info import GroupInfoUser
from zhenxun.services.log import logger
from zhenxun.utils.depends import UserName
Expand All @@ -20,7 +20,9 @@
指令:
我的信息 ?[at]
""".strip(),
extra=PluginExtraData(author="HibiKier", version="0.1").to_dict(),
extra=PluginExtraData(
author="HibiKier", version="0.1", commands=[Command(command="我的信息")]
).to_dict(),
)


Expand Down
2 changes: 1 addition & 1 deletion zhenxun/builtin_plugins/info/my_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async def get_user_info(
bytes: 图片数据
"""
platform = PlatformUtils.get_platform(session) or "qq"
ava_url = PlatformUtils.get_user_avatar_url(user_id, platform)
ava_url = PlatformUtils.get_user_avatar_url(user_id, platform, session.self_id)
user = await UserConsole.get_user(user_id, platform)
level = await LevelUser.get_user_level(user_id, group_id)
sign_level = 0
Expand Down
3 changes: 3 additions & 0 deletions zhenxun/builtin_plugins/init/init_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ async def _handle_setting(
cost_gold=setting.cost_gold,
plugin_type=extra_data.plugin_type,
admin_level=extra_data.admin_level,
is_show=extra_data.is_show,
ignore_prompt=extra_data.ignore_prompt,
parent=(plugin.parent_plugin.module_name if plugin.parent_plugin else None),
)
)
Expand Down Expand Up @@ -121,6 +123,7 @@ async def _():
"admin_level",
"plugin_type",
"is_show",
"ignore_prompt",
]
)
update_list.append(plugin)
Expand Down
7 changes: 6 additions & 1 deletion zhenxun/builtin_plugins/nickname.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from nonebot_plugin_uninfo import Uninfo

from zhenxun.configs.config import BotConfig, Config
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
from zhenxun.configs.utils import Command, PluginExtraData, RegisterConfig
from zhenxun.models.ban_console import BanConsole
from zhenxun.models.friend_user import FriendUser
from zhenxun.models.group_member_info import GroupInfoUser
Expand All @@ -36,6 +36,11 @@
version="0.1",
plugin_type=PluginType.NORMAL,
menu_type="其他",
commands=[
Command(command="以后叫我 [昵称]"),
Command(command="全局昵称设置 [昵称]"),
Command(command=f"{BotConfig.self_nickname}我是谁"),
],
configs=[
RegisterConfig(
key="BLACK_WORD",
Expand Down
10 changes: 9 additions & 1 deletion zhenxun/builtin_plugins/shop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from nonebot_plugin_uninfo import Uninfo

from zhenxun.configs.utils import BaseBlock, PluginExtraData, RegisterConfig
from zhenxun.configs.utils import BaseBlock, Command, PluginExtraData, RegisterConfig
from zhenxun.services.log import logger
from zhenxun.utils.depends import UserName
from zhenxun.utils.enum import BlockType, PluginType
Expand Down Expand Up @@ -44,6 +44,14 @@
version="0.1",
plugin_type=PluginType.NORMAL,
menu_type="商店",
commands=[
Command(command="我的金币"),
Command(command="我的道具"),
Command(command="购买道具"),
Command(command="使用道具"),
Command(command="金币排行"),
Command(command="金币总排行"),
],
limits=[BaseBlock(check_type=BlockType.GROUP)],
configs=[
RegisterConfig(
Expand Down
Loading

0 comments on commit 264929e

Please sign in to comment.