From 3237adfb97329df3ffd19916905476ec39cc33f9 Mon Sep 17 00:00:00 2001 From: omg-xtao <100690902+omg-xtao@users.noreply.github.com> Date: Tue, 21 May 2024 21:56:35 +0800 Subject: [PATCH] :sparkles: avatar list send photos instead of document --- plugins/genshin/avatar_list.py | 65 +++++++++++++++-------- resources/genshin/avatar_list/main.jinja2 | 2 +- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/plugins/genshin/avatar_list.py b/plugins/genshin/avatar_list.py index cfbb6bca..51130e11 100644 --- a/plugins/genshin/avatar_list.py +++ b/plugins/genshin/avatar_list.py @@ -1,6 +1,7 @@ import asyncio -from typing import List, Optional, Sequence, TYPE_CHECKING +from typing import List, Optional, Sequence, TYPE_CHECKING, Union, Tuple, Any, Dict +from arkowrapper import ArkoWrapper from simnet import GenshinClient from simnet.errors import BadRequest as SimnetBadRequest from simnet.models.genshin.calculator import CalculatorTalent, CalculatorCharacterDetails @@ -15,6 +16,7 @@ from core.services.players import PlayersService from core.services.players.services import PlayerInfoService from core.services.template.models import FileType from core.services.template.services import TemplateService +from gram_core.services.template.models import RenderGroupResult from modules.wiki.base import Model from plugins.tools.genshin import CharacterDetails, GenshinHelper from plugins.tools.player_info import PlayerInfoSystem @@ -24,6 +26,7 @@ from utils.uid import mask_number if TYPE_CHECKING: from telegram import Update from telegram.ext import ContextTypes + from gram_core.services.template.models import RenderResult MAX_AVATAR_COUNT = 40 @@ -133,6 +136,36 @@ class AvatarListPlugin(Plugin): reverse=True, )[:max_length] + async def avatar_list_render( + self, + base_render_data: Dict, + avatar_datas: List[AvatarData], + only_one_page: bool, + ) -> Union[Tuple[Any], List["RenderResult"], None]: + def render_task(start_id: int, c: List[AvatarData]): + _render_data = { + "avatar_datas": c, # 角色数据 + "start_id": start_id, # 开始序号 + } + _render_data.update(base_render_data) + return self.template_service.render( + "genshin/avatar_list/main.jinja2", + _render_data, + viewport={"width": 1040, "height": 500}, + full_page=True, + query_selector=".container", + file_type=FileType.PHOTO, + ttl=30 * 24 * 60 * 60, + ) + + if only_one_page: + return [await render_task(0, avatar_datas)] + avatar_datas_group = [ + avatar_datas[i : i + MAX_AVATAR_COUNT] for i in range(0, len(avatar_datas), MAX_AVATAR_COUNT) + ] + tasks = [render_task(i * MAX_AVATAR_COUNT, c) for i, c in enumerate(avatar_datas_group)] + return await asyncio.gather(*tasks) + @handler.command("avatars", cookie=True, block=False) @handler.message(filters.Regex(r"^(全部)?练度统计$"), cookie=True, block=False) async def avatar_list(self, update: "Update", _: "ContextTypes.DEFAULT_TYPE"): @@ -164,39 +197,27 @@ class AvatarListPlugin(Plugin): client.player_id, user_id, user_name ) - render_data = { + await message.reply_chat_action(ChatAction.UPLOAD_PHOTO) + base_render_data = { "uid": mask_number(client.player_id), # 玩家uid "nickname": nickname, # 玩家昵称 "avatar": avatar, # 玩家头像 "rarity": rarity, # 玩家头像对应的角色星级 "namecard": name_card, # 玩家名片 - "avatar_datas": avatar_datas, # 角色数据 "has_more": len(characters) != len(avatar_datas), # 是否显示了全部角色 } - as_document = all_avatars and len(characters) > MAX_AVATAR_COUNT - - await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT if as_document else ChatAction.UPLOAD_PHOTO) - - image = await self.template_service.render( - "genshin/avatar_list/main.jinja2", - render_data, - viewport={"width": 1040, "height": 500}, - full_page=True, - query_selector=".container", - file_type=FileType.DOCUMENT if as_document else FileType.PHOTO, - ttl=30 * 24 * 60 * 60, - ) + images = await self.avatar_list_render(base_render_data, avatar_datas, not all_avatars) self.add_delete_message_job(notice, delay=5) - if as_document: - await image.reply_document(message, filename="练度统计.png") - else: - await image.reply_photo(message) + + for group in ArkoWrapper(images).group(10): # 每 10 张图片分一个组 + await RenderGroupResult(results=group).reply_media_group( + message, allow_sending_without_reply=True, write_timeout=60 + ) self.log_user( update, logger.info, - "[bold]练度统计[/bold]发送%s成功", - "文件" if all_avatars else "图片", + "[bold]练度统计[/bold]发送图片成功", extra={"markup": True}, ) diff --git a/resources/genshin/avatar_list/main.jinja2 b/resources/genshin/avatar_list/main.jinja2 index 72f0cb38..9a31eb49 100644 --- a/resources/genshin/avatar_list/main.jinja2 +++ b/resources/genshin/avatar_list/main.jinja2 @@ -56,7 +56,7 @@ class="row" {% endif %} > -