MibooGram/plugins/genshin/daily_note.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

124 lines
6.0 KiB
Python
Raw Normal View History

2022-07-31 05:48:41 +00:00
import datetime
import os
2022-08-05 14:45:54 +00:00
from typing import Optional
2022-07-31 05:48:41 +00:00
from genshin import DataNotPublic
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
2022-07-31 05:48:41 +00:00
from telegram.constants import ChatAction
from telegram.ext import CommandHandler, MessageHandler, ConversationHandler, filters, CallbackContext
from core.baseplugin import BasePlugin
from core.cookies.error import CookiesNotFoundError
from core.cookies.services import CookiesService
from core.plugin import Plugin, handler
from core.template.services import TemplateService
from core.user.error import UserNotFoundError
from core.user.services import UserService
2022-07-31 05:48:41 +00:00
from utils.decorators.error import error_callable
from utils.decorators.restricts import restricts
from utils.helpers import get_genshin_client
from utils.log import logger
2022-07-31 05:48:41 +00:00
class DailyNote(Plugin, BasePlugin):
2022-07-31 05:48:41 +00:00
"""每日便签"""
def __init__(
self,
user_service: UserService = None,
cookies_service: CookiesService = None,
template_service: TemplateService = None,
):
self.template_service = template_service
self.cookies_service = cookies_service
self.user_service = user_service
self.current_dir = os.getcwd()
async def _get_daily_note(self, client) -> bytes:
daily_info = await client.get_genshin_notes(client.uid)
day = datetime.datetime.now().strftime("%m-%d %H:%M") + " 星期" + "一二三四五六日"[datetime.datetime.now().weekday()]
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 = (
(datetime.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-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:
remained_time = (datetime.datetime.now().astimezone() + remained_time).strftime("%m-%d %H:%M")
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")
daily_data = {
"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,
}
png_data = await self.template_service.render(
"genshin/daily_note", "daily_note.html", daily_data, {"width": 600, "height": 548}, full_page=False
)
return png_data
@handler(CommandHandler, command="dailynote", block=False)
@handler(MessageHandler, filters=filters.Regex("^当前状态(.*)"), block=False)
@restricts(return_data=ConversationHandler.END)
2022-07-31 05:48:41 +00:00
@error_callable
2022-08-05 14:45:54 +00:00
async def command_start(self, update: Update, context: CallbackContext) -> Optional[int]:
2022-07-31 05:48:41 +00:00
user = update.effective_user
message = update.effective_message
logger.info(f"用户 {user.full_name}[{user.id}] 查询游戏状态命令请求")
2022-07-31 05:48:41 +00:00
try:
client = await get_genshin_client(user.id)
2022-07-31 05:48:41 +00:00
png_data = await self._get_daily_note(client)
except (UserNotFoundError, CookiesNotFoundError):
2022-07-31 05:48:41 +00:00
if filters.ChatType.GROUPS.filter(message):
buttons = [[InlineKeyboardButton("点我私聊", url=f"https://t.me/{context.bot.username}?start=set_cookie")]]
reply_message = await message.reply_text(
"未查询到您所绑定的账号信息,请先私聊派蒙绑定账号", reply_markup=InlineKeyboardMarkup(buttons)
)
2022-07-31 05:48:41 +00:00
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 30)
2022-07-31 05:48:41 +00:00
self._add_delete_message_job(context, message.chat_id, message.message_id, 30)
else:
await message.reply_text("未查询到您所绑定的账号信息,请先绑定账号")
2022-07-31 05:48:41 +00:00
return
except DataNotPublic:
reply_message = await message.reply_text("查询失败惹,可能是便签功能被禁用了?")
2022-07-31 05:48:41 +00:00
if filters.ChatType.GROUPS.filter(message):
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 300)
self._add_delete_message_job(context, message.chat_id, message.message_id, 300)
return ConversationHandler.END
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
await message.reply_photo(png_data, filename=f"{client.uid}.png", allow_sending_without_reply=True)