2022-10-01 06:51:32 +00:00
|
|
|
from typing import Dict, Optional
|
2022-09-08 01:08:37 +00:00
|
|
|
|
2022-07-26 10:07:31 +00:00
|
|
|
from genshin import Client
|
|
|
|
from telegram import Update
|
|
|
|
from telegram.constants import ChatAction
|
2022-09-08 01:08:37 +00:00
|
|
|
from telegram.ext import CallbackContext, CommandHandler, MessageHandler, filters
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.baseplugin import BasePlugin
|
|
|
|
from core.cookies.error import CookiesNotFoundError
|
2022-08-06 12:37:41 +00:00
|
|
|
from core.cookies.services import CookiesService
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.plugin import Plugin, handler
|
|
|
|
from core.template import TemplateService
|
2022-08-06 12:37:41 +00:00
|
|
|
from core.user import UserService
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.user.error import UserNotFoundError
|
2022-07-31 08:17:27 +00:00
|
|
|
from utils.decorators.error import error_callable
|
|
|
|
from utils.decorators.restricts import restricts
|
2022-10-01 06:51:32 +00:00
|
|
|
from utils.helpers import get_genshin_client, url_to_file, get_public_genshin_client
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
2022-09-03 04:17:31 +00:00
|
|
|
class AbyssUnlocked(Exception):
|
|
|
|
"""根本没动"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NoMostKills(Exception):
|
|
|
|
"""挑战了但是数据没刷新"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
class Abyss(Plugin, BasePlugin):
|
2022-07-31 04:51:22 +00:00
|
|
|
"""深渊数据查询"""
|
|
|
|
|
2022-07-31 08:17:27 +00:00
|
|
|
def __init__(self, user_service: UserService = None, cookies_service: CookiesService = None,
|
|
|
|
template_service: TemplateService = None):
|
2022-07-31 04:51:22 +00:00
|
|
|
self.template_service = template_service
|
|
|
|
self.cookies_service = cookies_service
|
|
|
|
self.user_service = user_service
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_role_star_bg(value: int):
|
|
|
|
if value == 4:
|
|
|
|
return "./background/roleStarBg4.png"
|
|
|
|
elif value == 5:
|
|
|
|
return "./background/roleStarBg5.png"
|
|
|
|
else:
|
|
|
|
raise ValueError("错误的数据")
|
|
|
|
|
2022-10-01 06:51:32 +00:00
|
|
|
async def _get_abyss_data(self, client: Client, uid: Optional[int] = None) -> Dict:
|
|
|
|
if uid is None:
|
|
|
|
_uid = client.uid
|
|
|
|
await client.get_record_cards()
|
|
|
|
else:
|
|
|
|
_uid = client.uid
|
|
|
|
spiral_abyss_info = await client.get_spiral_abyss(_uid)
|
2022-07-26 10:07:31 +00:00
|
|
|
if not spiral_abyss_info.unlocked:
|
2022-09-03 04:17:31 +00:00
|
|
|
raise AbyssUnlocked
|
2022-07-26 10:07:31 +00:00
|
|
|
ranks = spiral_abyss_info.ranks
|
|
|
|
if len(spiral_abyss_info.ranks.most_kills) == 0:
|
2022-09-03 04:17:31 +00:00
|
|
|
raise NoMostKills
|
2022-07-26 10:07:31 +00:00
|
|
|
abyss_data = {
|
2022-10-01 08:43:13 +00:00
|
|
|
"uid": _uid,
|
2022-07-26 10:07:31 +00:00
|
|
|
"max_floor": spiral_abyss_info.max_floor,
|
|
|
|
"total_battles": spiral_abyss_info.total_battles,
|
|
|
|
"total_stars": spiral_abyss_info.total_stars,
|
|
|
|
"most_played_list": [],
|
|
|
|
"most_kills": {
|
2022-10-06 03:46:06 +00:00
|
|
|
"icon": await url_to_file(ranks.most_kills[0].side_icon),
|
2022-07-26 10:07:31 +00:00
|
|
|
"value": ranks.most_kills[0].value,
|
|
|
|
},
|
|
|
|
"strongest_strike": {
|
2022-10-06 03:46:06 +00:00
|
|
|
"icon": await url_to_file(ranks.strongest_strike[0].side_icon),
|
2022-07-26 10:07:31 +00:00
|
|
|
"value": ranks.strongest_strike[0].value
|
|
|
|
},
|
|
|
|
"most_damage_taken": {
|
2022-10-06 03:46:06 +00:00
|
|
|
"icon": await url_to_file(ranks.most_damage_taken[0].side_icon),
|
2022-07-26 10:07:31 +00:00
|
|
|
"value": ranks.most_damage_taken[0].value
|
|
|
|
},
|
|
|
|
"most_bursts_used": {
|
2022-10-06 03:46:06 +00:00
|
|
|
"icon": await url_to_file(ranks.most_bursts_used[0].side_icon),
|
2022-07-26 10:07:31 +00:00
|
|
|
"value": ranks.most_bursts_used[0].value
|
|
|
|
},
|
|
|
|
"most_skills_used": {
|
2022-10-06 03:46:06 +00:00
|
|
|
"icon": await url_to_file(ranks.most_skills_used[0].side_icon),
|
2022-07-26 10:07:31 +00:00
|
|
|
"value": ranks.most_skills_used[0].value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# most_kills
|
|
|
|
most_played_list = ranks.most_played
|
|
|
|
for most_played in most_played_list:
|
|
|
|
temp = {
|
|
|
|
"icon": await url_to_file(most_played.icon),
|
|
|
|
"value": most_played.value,
|
|
|
|
"background": self._get_role_star_bg(most_played.rarity)
|
|
|
|
}
|
|
|
|
abyss_data["most_played_list"].append(temp)
|
|
|
|
return abyss_data
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
@handler(CommandHandler, command="abyss", block=False)
|
|
|
|
@handler(MessageHandler, filters=filters.Regex("^深渊数据查询(.*)"), block=False)
|
2022-08-05 14:45:54 +00:00
|
|
|
@restricts()
|
2022-07-31 08:17:27 +00:00
|
|
|
@error_callable
|
2022-07-31 04:51:22 +00:00
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> None:
|
2022-07-26 10:07:31 +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-07-26 10:07:31 +00:00
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
|
|
|
try:
|
2022-10-01 06:51:32 +00:00
|
|
|
try:
|
|
|
|
client = await get_genshin_client(user.id)
|
|
|
|
abyss_data = await self._get_abyss_data(client)
|
|
|
|
except CookiesNotFoundError:
|
|
|
|
client, uid = await get_public_genshin_client(user.id)
|
|
|
|
abyss_data = await self._get_abyss_data(client, uid)
|
|
|
|
except UserNotFoundError:
|
2022-07-26 10:07:31 +00:00
|
|
|
reply_message = await message.reply_text("未查询到账号信息,请先私聊派蒙绑定账号")
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2022-09-03 04:17:31 +00:00
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 10)
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id, 10)
|
|
|
|
return
|
|
|
|
except (AbyssUnlocked, NoMostKills):
|
2022-10-01 08:43:13 +00:00
|
|
|
await message.reply_text("本次深渊旅行者还没挑战呢,咕咕咕~~~")
|
2022-07-26 10:07:31 +00:00
|
|
|
return
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
2022-07-31 04:51:22 +00:00
|
|
|
png_data = await self.template_service.render('genshin/abyss', "abyss.html", abyss_data,
|
2022-10-01 06:55:53 +00:00
|
|
|
{"width": 865, "height": 504}, full_page=False)
|
2022-07-26 10:07:31 +00:00
|
|
|
await message.reply_photo(png_data, filename=f"abyss_{user.id}.png",
|
|
|
|
allow_sending_without_reply=True)
|