2022-07-31 05:48:41 +00:00
|
|
|
import datetime
|
2023-03-14 01:27:22 +00:00
|
|
|
from datetime import datetime
|
2022-08-05 14:45:54 +00:00
|
|
|
from typing import Optional
|
2022-07-31 05:48:41 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
import genshin
|
2022-07-31 05:48:41 +00:00
|
|
|
from genshin import DataNotPublic
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
2022-07-31 05:48:41 +00:00
|
|
|
from telegram.constants import ChatAction
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram.ext import ConversationHandler, filters, CallbackContext
|
2022-11-28 08:38:49 +00:00
|
|
|
from telegram.helpers import create_deep_linked_url
|
2022-07-31 05:48:41 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.plugin import Plugin, handler
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.services.template.models import RenderResult
|
|
|
|
from core.services.template.services import TemplateService
|
|
|
|
from plugins.tools.genshin import GenshinHelper, CookiesNotFoundError, PlayerNotFoundError
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2022-07-31 05:48:41 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
__all__ = ("DailyNotePlugin",)
|
2022-07-31 05:48:41 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
class DailyNotePlugin(Plugin):
|
2022-07-31 05:48:41 +00:00
|
|
|
"""每日便签"""
|
|
|
|
|
|
|
|
def __init__(
|
2022-10-22 13:54:04 +00:00
|
|
|
self,
|
2023-03-14 01:27:22 +00:00
|
|
|
template: TemplateService,
|
|
|
|
helper: GenshinHelper,
|
2022-07-31 05:48:41 +00:00
|
|
|
):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.template_service = template
|
|
|
|
self.helper = helper
|
2022-07-31 05:48:41 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
async def _get_daily_note(self, client: genshin.Client) -> RenderResult:
|
2022-07-31 05:48:41 +00:00
|
|
|
daily_info = await client.get_genshin_notes(client.uid)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
day = datetime.now().strftime("%m-%d %H:%M") + " 星期" + "一二三四五六日"[datetime.now().weekday()]
|
2022-07-31 05:48:41 +00:00
|
|
|
resin_recovery_time = (
|
|
|
|
daily_info.resin_recovery_time.strftime("%m-%d %H:%M")
|
|
|
|
if daily_info.max_resin - daily_info.current_resin
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
realm_recovery_time = (
|
2023-03-14 01:27:22 +00:00
|
|
|
(datetime.now().astimezone() + daily_info.remaining_realm_currency_recovery_time).strftime("%m-%d %H:%M")
|
2022-07-31 05:48:41 +00:00
|
|
|
if daily_info.max_realm_currency - daily_info.current_realm_currency
|
|
|
|
else None
|
2022-10-10 11:07:28 +00:00
|
|
|
)
|
2022-07-31 05:48:41 +00:00
|
|
|
remained_time = None
|
|
|
|
for i in daily_info.expeditions:
|
|
|
|
if remained_time:
|
|
|
|
if remained_time < i.remaining_time:
|
|
|
|
remained_time = i.remaining_time
|
|
|
|
else:
|
|
|
|
remained_time = i.remaining_time
|
|
|
|
if remained_time:
|
2023-03-14 01:27:22 +00:00
|
|
|
remained_time = (datetime.now().astimezone() + remained_time).strftime("%m-%d %H:%M")
|
|
|
|
|
2022-07-31 05:48:41 +00:00
|
|
|
transformer, transformer_ready, transformer_recovery_time = False, None, None
|
|
|
|
if daily_info.remaining_transformer_recovery_time is not None:
|
|
|
|
transformer = True
|
|
|
|
transformer_ready = daily_info.remaining_transformer_recovery_time.total_seconds() == 0
|
|
|
|
transformer_recovery_time = daily_info.transformer_recovery_time.strftime("%m-%d %H:%M")
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
render_data = {
|
2022-07-31 05:48:41 +00:00
|
|
|
"uid": client.uid,
|
|
|
|
"day": day,
|
|
|
|
"resin_recovery_time": resin_recovery_time,
|
|
|
|
"current_resin": daily_info.current_resin,
|
|
|
|
"max_resin": daily_info.max_resin,
|
|
|
|
"realm_recovery_time": realm_recovery_time,
|
|
|
|
"current_realm_currency": daily_info.current_realm_currency,
|
|
|
|
"max_realm_currency": daily_info.max_realm_currency,
|
|
|
|
"claimed_commission_reward": daily_info.claimed_commission_reward,
|
|
|
|
"completed_commissions": daily_info.completed_commissions,
|
|
|
|
"max_commissions": daily_info.max_commissions,
|
|
|
|
"expeditions": bool(daily_info.expeditions),
|
|
|
|
"remained_time": remained_time,
|
|
|
|
"current_expeditions": len(daily_info.expeditions),
|
|
|
|
"max_expeditions": daily_info.max_expeditions,
|
|
|
|
"remaining_resin_discounts": daily_info.remaining_resin_discounts,
|
|
|
|
"max_resin_discounts": daily_info.max_resin_discounts,
|
|
|
|
"transformer": transformer,
|
|
|
|
"transformer_ready": transformer_ready,
|
|
|
|
"transformer_recovery_time": transformer_recovery_time,
|
|
|
|
}
|
2022-10-22 07:03:59 +00:00
|
|
|
render_result = await self.template_service.render(
|
2023-05-09 11:01:45 +00:00
|
|
|
"genshin/daily_note/daily_note.jinja2",
|
2023-03-14 01:27:22 +00:00
|
|
|
render_data,
|
|
|
|
{"width": 600, "height": 548},
|
|
|
|
full_page=False,
|
|
|
|
ttl=8 * 60,
|
2022-07-31 05:48:41 +00:00
|
|
|
)
|
2022-10-22 07:03:59 +00:00
|
|
|
return render_result
|
2022-07-31 05:48:41 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
@handler.command("dailynote", block=False)
|
|
|
|
@handler.message(filters.Regex("^当前状态(.*)"), block=False)
|
|
|
|
async def command_start(self, update: Update, _: CallbackContext) -> Optional[int]:
|
2022-09-18 08:35:37 +00:00
|
|
|
message = update.effective_message
|
2023-03-14 01:27:22 +00:00
|
|
|
user = update.effective_user
|
|
|
|
logger.info("用户 %s[%s] 每日便签命令请求", user.full_name, user.id)
|
|
|
|
|
2022-07-31 05:48:41 +00:00
|
|
|
try:
|
2023-03-14 01:27:22 +00:00
|
|
|
# 获取当前用户的 genshin.Client
|
|
|
|
client = await self.helper.get_genshin_client(user.id)
|
|
|
|
# 渲染
|
2022-10-22 07:03:59 +00:00
|
|
|
render_result = await self._get_daily_note(client)
|
2023-03-14 01:27:22 +00:00
|
|
|
except (CookiesNotFoundError, PlayerNotFoundError):
|
|
|
|
buttons = [
|
|
|
|
[
|
|
|
|
InlineKeyboardButton(
|
|
|
|
"点我绑定账号", url=create_deep_linked_url(self.application.bot.username, "set_cookie")
|
|
|
|
)
|
|
|
|
]
|
|
|
|
]
|
2022-07-31 05:48:41 +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)
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(reply_message, delay=30)
|
|
|
|
self.add_delete_message_job(message, delay=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:48:41 +00:00
|
|
|
return
|
|
|
|
except DataNotPublic:
|
2022-10-22 13:54:04 +00:00
|
|
|
reply_message = await message.reply_text("查询失败惹,可能是便签功能被禁用了?请尝试通过米游社或者 hoyolab 获取一次便签信息后重试。")
|
2022-07-31 05:48:41 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(reply_message, delay=30)
|
|
|
|
self.add_delete_message_job(message, delay=30)
|
2022-07-31 05:48:41 +00:00
|
|
|
return ConversationHandler.END
|
2023-03-14 01:27:22 +00:00
|
|
|
|
2022-10-01 08:29:22 +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)
|