avatar list send photos instead of document

This commit is contained in:
omg-xtao 2024-05-21 21:56:35 +08:00 committed by GitHub
parent 767076b132
commit 3237adfb97
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 23 deletions

View File

@ -1,6 +1,7 @@
import asyncio 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 import GenshinClient
from simnet.errors import BadRequest as SimnetBadRequest from simnet.errors import BadRequest as SimnetBadRequest
from simnet.models.genshin.calculator import CalculatorTalent, CalculatorCharacterDetails 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.players.services import PlayerInfoService
from core.services.template.models import FileType from core.services.template.models import FileType
from core.services.template.services import TemplateService from core.services.template.services import TemplateService
from gram_core.services.template.models import RenderGroupResult
from modules.wiki.base import Model from modules.wiki.base import Model
from plugins.tools.genshin import CharacterDetails, GenshinHelper from plugins.tools.genshin import CharacterDetails, GenshinHelper
from plugins.tools.player_info import PlayerInfoSystem from plugins.tools.player_info import PlayerInfoSystem
@ -24,6 +26,7 @@ from utils.uid import mask_number
if TYPE_CHECKING: if TYPE_CHECKING:
from telegram import Update from telegram import Update
from telegram.ext import ContextTypes from telegram.ext import ContextTypes
from gram_core.services.template.models import RenderResult
MAX_AVATAR_COUNT = 40 MAX_AVATAR_COUNT = 40
@ -133,6 +136,36 @@ class AvatarListPlugin(Plugin):
reverse=True, reverse=True,
)[:max_length] )[: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.command("avatars", cookie=True, block=False)
@handler.message(filters.Regex(r"^(全部)?练度统计$"), cookie=True, block=False) @handler.message(filters.Regex(r"^(全部)?练度统计$"), cookie=True, block=False)
async def avatar_list(self, update: "Update", _: "ContextTypes.DEFAULT_TYPE"): async def avatar_list(self, update: "Update", _: "ContextTypes.DEFAULT_TYPE"):
@ -164,39 +197,27 @@ class AvatarListPlugin(Plugin):
client.player_id, user_id, user_name 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 "uid": mask_number(client.player_id), # 玩家uid
"nickname": nickname, # 玩家昵称 "nickname": nickname, # 玩家昵称
"avatar": avatar, # 玩家头像 "avatar": avatar, # 玩家头像
"rarity": rarity, # 玩家头像对应的角色星级 "rarity": rarity, # 玩家头像对应的角色星级
"namecard": name_card, # 玩家名片 "namecard": name_card, # 玩家名片
"avatar_datas": avatar_datas, # 角色数据
"has_more": len(characters) != len(avatar_datas), # 是否显示了全部角色 "has_more": len(characters) != len(avatar_datas), # 是否显示了全部角色
} }
as_document = all_avatars and len(characters) > MAX_AVATAR_COUNT images = await self.avatar_list_render(base_render_data, avatar_datas, not all_avatars)
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,
)
self.add_delete_message_job(notice, delay=5) self.add_delete_message_job(notice, delay=5)
if as_document:
await image.reply_document(message, filename="练度统计.png") for group in ArkoWrapper(images).group(10): # 每 10 张图片分一个组
else: await RenderGroupResult(results=group).reply_media_group(
await image.reply_photo(message) message, allow_sending_without_reply=True, write_timeout=60
)
self.log_user( self.log_user(
update, update,
logger.info, logger.info,
"[bold]练度统计[/bold]发送%s成功", "[bold]练度统计[/bold]发送图片成功",
"文件" if all_avatars else "图片",
extra={"markup": True}, extra={"markup": True},
) )

View File

@ -56,7 +56,7 @@
class="row" class="row"
{% endif %} {% endif %}
> >
<div style="background-color: {{ row_bg }}">{{ loop.index }}</div> <div style="background-color: {{ row_bg }}">{{ start_id + loop.index }}</div>
<div class="role" style="flex: 4;background-color: {{ row_bg }}"> <div class="role" style="flex: 4;background-color: {{ row_bg }}">
<div class="role-icon" style="flex: 1.5;"> <div class="role-icon" style="flex: 1.5;">