2022-10-29 04:11:26 +00:00
|
|
|
import random
|
2022-08-06 06:22:37 +00:00
|
|
|
from typing import Optional
|
2022-07-31 05:16:38 +00:00
|
|
|
|
2022-10-08 13:35:46 +00:00
|
|
|
from genshin import Client
|
2022-10-08 12:47:55 +00:00
|
|
|
from genshin.models import GenshinUserStats
|
2022-10-12 09:35:59 +00:00
|
|
|
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
2022-07-31 05:16:38 +00:00
|
|
|
from telegram.constants import ChatAction
|
2022-10-08 12:47:55 +00:00
|
|
|
from telegram.ext import (
|
|
|
|
CallbackContext,
|
|
|
|
CommandHandler,
|
|
|
|
MessageHandler,
|
|
|
|
filters,
|
|
|
|
)
|
2022-11-28 08:38:49 +00:00
|
|
|
from telegram.helpers import create_deep_linked_url
|
2022-07-31 05:16:38 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.baseplugin import BasePlugin
|
2022-10-09 05:45:50 +00:00
|
|
|
from core.cookies.error import CookiesNotFoundError, TooManyRequestPublicCookies
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.plugin import Plugin, handler
|
2022-10-22 07:03:59 +00:00
|
|
|
from core.template.models import RenderResult
|
2022-08-06 12:37:41 +00:00
|
|
|
from core.template.services import TemplateService
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.user.error import UserNotFoundError
|
2022-07-31 05:16:38 +00:00
|
|
|
from utils.decorators.error import error_callable
|
|
|
|
from utils.decorators.restricts import restricts
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.helpers import url_to_file, get_genshin_client, get_public_genshin_client
|
|
|
|
from utils.log import logger
|
2022-07-31 05:16:38 +00:00
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
class UserStatsPlugins(Plugin, BasePlugin):
|
|
|
|
"""玩家统计查询"""
|
2022-07-31 05:16:38 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
def __init__(self, template_service: TemplateService = None):
|
2022-07-31 05:16:38 +00:00
|
|
|
self.template_service = template_service
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
@handler(CommandHandler, command="stats", block=False)
|
|
|
|
@handler(MessageHandler, filters=filters.Regex("^玩家统计查询(.*)"), block=False)
|
2022-10-09 05:45:50 +00:00
|
|
|
@restricts()
|
2022-09-08 01:08:37 +00:00
|
|
|
@error_callable
|
2022-08-06 06:22:37 +00:00
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> Optional[int]:
|
2022-07-31 05:16:38 +00:00
|
|
|
user = update.effective_user
|
2022-09-08 01:08:37 +00:00
|
|
|
message = update.effective_message
|
|
|
|
logger.info(f"用户 {user.full_name}[{user.id}] 查询游戏用户命令请求")
|
2022-10-08 13:35:46 +00:00
|
|
|
uid: Optional[int] = None
|
2022-07-31 05:16:38 +00:00
|
|
|
try:
|
|
|
|
args = context.args
|
2022-08-06 06:22:37 +00:00
|
|
|
if args is not None and len(args) >= 1:
|
|
|
|
uid = int(args[0])
|
2022-09-08 01:08:37 +00:00
|
|
|
except ValueError as exc:
|
2022-10-08 13:35:46 +00:00
|
|
|
logger.warning(f"获取 uid 发生错误! 错误信息为 {repr(exc)}")
|
2022-07-31 05:16:38 +00:00
|
|
|
await message.reply_text("输入错误")
|
2022-10-09 05:45:50 +00:00
|
|
|
return
|
2022-07-31 05:16:38 +00:00
|
|
|
try:
|
2022-09-08 01:08:37 +00:00
|
|
|
try:
|
|
|
|
client = await get_genshin_client(user.id)
|
|
|
|
except CookiesNotFoundError:
|
2022-10-08 13:35:46 +00:00
|
|
|
client, uid = await get_public_genshin_client(user.id)
|
2022-10-22 07:03:59 +00:00
|
|
|
render_result = await self.render(client, uid)
|
2022-07-31 05:16:38 +00:00
|
|
|
except UserNotFoundError:
|
2022-11-28 08:38:49 +00:00
|
|
|
buttons = [[InlineKeyboardButton("点我绑定账号", url=create_deep_linked_url(context.bot.username, "set_uid"))]]
|
2022-07-31 05:16:38 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2022-10-12 09:35:59 +00:00
|
|
|
reply_message = await message.reply_text(
|
|
|
|
"未查询到您所绑定的账号信息,请先私聊派蒙绑定账号", reply_markup=InlineKeyboardMarkup(buttons)
|
|
|
|
)
|
2022-10-10 11:07:28 +00:00
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 30)
|
2022-08-06 06:22:37 +00:00
|
|
|
|
2022-10-10 11:07:28 +00:00
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id, 30)
|
2022-10-12 09:35:59 +00:00
|
|
|
else:
|
2022-10-22 13:54:04 +00:00
|
|
|
await message.reply_text("未查询到您所绑定的账号信息,请先绑定账号", reply_markup=InlineKeyboardMarkup(buttons))
|
2022-07-31 05:16:38 +00:00
|
|
|
return
|
2022-10-09 05:45:50 +00:00
|
|
|
except TooManyRequestPublicCookies:
|
|
|
|
await message.reply_text("用户查询次数过多 请稍后重试")
|
|
|
|
return
|
2022-07-31 05:16:38 +00:00
|
|
|
except AttributeError as exc:
|
2022-10-05 16:08:13 +00:00
|
|
|
logger.error("角色数据有误")
|
|
|
|
logger.exception(exc)
|
2022-07-31 05:16:38 +00:00
|
|
|
await message.reply_text("角色数据有误 估计是派蒙晕了")
|
2022-10-09 05:45:50 +00:00
|
|
|
return
|
2022-07-31 05:16:38 +00:00
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
2022-10-22 07:03:59 +00:00
|
|
|
await render_result.reply_photo(message, filename=f"{client.uid}.png", allow_sending_without_reply=True)
|
2022-10-08 12:47:55 +00:00
|
|
|
|
2022-10-22 07:03:59 +00:00
|
|
|
async def render(self, client: Client, uid: Optional[int] = None) -> RenderResult:
|
2022-10-08 13:35:46 +00:00
|
|
|
if uid is None:
|
2022-10-08 12:47:55 +00:00
|
|
|
uid = client.uid
|
|
|
|
|
2022-10-08 13:35:46 +00:00
|
|
|
user_info = await client.get_genshin_user(uid)
|
|
|
|
logger.debug(user_info)
|
2022-10-08 12:47:55 +00:00
|
|
|
|
|
|
|
# 因为需要替换线上图片地址为本地地址,先克隆数据,避免修改原数据
|
|
|
|
user_info = user_info.copy(deep=True)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"uid": uid,
|
|
|
|
"info": user_info.info,
|
|
|
|
"stats": user_info.stats,
|
|
|
|
"explorations": user_info.explorations,
|
|
|
|
"teapot": user_info.teapot,
|
|
|
|
"stats_labels": [
|
|
|
|
("活跃天数", "days_active"),
|
|
|
|
("成就达成数", "achievements"),
|
|
|
|
("获取角色数", "characters"),
|
|
|
|
("深境螺旋", "spiral_abyss"),
|
|
|
|
("解锁传送点", "unlocked_waypoints"),
|
|
|
|
("解锁秘境", "unlocked_domains"),
|
|
|
|
("奇馈宝箱数", "remarkable_chests"),
|
|
|
|
("华丽宝箱数", "luxurious_chests"),
|
|
|
|
("珍贵宝箱数", "precious_chests"),
|
|
|
|
("精致宝箱数", "exquisite_chests"),
|
|
|
|
("普通宝箱数", "common_chests"),
|
|
|
|
("风神瞳", "anemoculi"),
|
|
|
|
("岩神瞳", "geoculi"),
|
|
|
|
("雷神瞳", "electroculi"),
|
|
|
|
("草神瞳", "dendroculi"),
|
|
|
|
],
|
2022-10-29 04:11:26 +00:00
|
|
|
"style": random.choice(["mondstadt", "liyue"]), # nosec
|
2022-10-08 12:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# html = await self.template_service.render_async(
|
2022-10-12 13:39:47 +00:00
|
|
|
# "genshin/stats/stats.html", data
|
2022-10-08 12:47:55 +00:00
|
|
|
# )
|
|
|
|
# logger.debug(html)
|
|
|
|
|
|
|
|
await self.cache_images(user_info)
|
|
|
|
|
|
|
|
return await self.template_service.render(
|
2022-10-12 13:39:47 +00:00
|
|
|
"genshin/stats/stats.html",
|
2022-10-08 12:47:55 +00:00
|
|
|
data,
|
|
|
|
{"width": 650, "height": 800},
|
|
|
|
full_page=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def cache_images(data: GenshinUserStats) -> None:
|
|
|
|
"""缓存所有图片到本地"""
|
|
|
|
# TODO: 并发下载所有资源
|
|
|
|
|
|
|
|
# 探索地区
|
|
|
|
for item in data.explorations:
|
|
|
|
item.__config__.allow_mutation = True
|
|
|
|
item.icon = await url_to_file(item.icon)
|
|
|
|
item.cover = await url_to_file(item.cover)
|