2023-04-26 12:02:57 +00:00
|
|
|
import datetime
|
|
|
|
from datetime import datetime
|
2023-07-19 05:52:30 +00:00
|
|
|
from typing import Optional, TYPE_CHECKING
|
2023-04-26 12:02:57 +00:00
|
|
|
|
2023-07-19 05:52:30 +00:00
|
|
|
from simnet.errors import DataNotPublic
|
2024-02-19 12:55:13 +00:00
|
|
|
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton
|
2023-04-26 12:02:57 +00:00
|
|
|
from telegram.constants import ChatAction
|
|
|
|
from telegram.ext import ConversationHandler, filters, CallbackContext
|
2024-02-19 12:55:13 +00:00
|
|
|
from telegram.helpers import create_deep_linked_url
|
2023-04-26 12:02:57 +00:00
|
|
|
|
|
|
|
from core.plugin import Plugin, handler
|
|
|
|
from core.services.template.models import RenderResult
|
|
|
|
from core.services.template.services import TemplateService
|
2023-08-21 14:43:01 +00:00
|
|
|
from plugins.tools.genshin import GenshinHelper
|
2023-04-26 12:02:57 +00:00
|
|
|
from utils.log import logger
|
2023-08-26 13:21:26 +00:00
|
|
|
from utils.uid import mask_number
|
2023-04-26 12:02:57 +00:00
|
|
|
|
2023-07-19 05:52:30 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from simnet import StarRailClient
|
|
|
|
|
|
|
|
|
2023-04-26 12:02:57 +00:00
|
|
|
__all__ = ("DailyNotePlugin",)
|
|
|
|
|
|
|
|
|
|
|
|
class DailyNotePlugin(Plugin):
|
|
|
|
"""每日便签"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
template: TemplateService,
|
|
|
|
helper: GenshinHelper,
|
|
|
|
):
|
|
|
|
self.template_service = template
|
|
|
|
self.helper = helper
|
|
|
|
|
2023-07-19 05:52:30 +00:00
|
|
|
async def _get_daily_note(self, client: "StarRailClient") -> RenderResult:
|
|
|
|
daily_info = await client.get_starrail_notes(client.player_id)
|
2023-04-26 12:02:57 +00:00
|
|
|
|
|
|
|
day = datetime.now().strftime("%m-%d %H:%M") + " 星期" + "一二三四五六日"[datetime.now().weekday()]
|
|
|
|
resin_recovery_time = (
|
|
|
|
(datetime.now() + daily_info.stamina_recover_time).strftime("%m-%d %H:%M")
|
|
|
|
if daily_info.max_stamina - daily_info.current_stamina
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
|
|
|
|
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.now().astimezone() + remained_time).strftime("%m-%d %H:%M")
|
|
|
|
|
|
|
|
render_data = {
|
2023-08-26 13:21:26 +00:00
|
|
|
"uid": mask_number(client.player_id),
|
2023-04-26 12:02:57 +00:00
|
|
|
"day": day,
|
|
|
|
"resin_recovery_time": resin_recovery_time,
|
|
|
|
"current_resin": daily_info.current_stamina,
|
|
|
|
"max_resin": daily_info.max_stamina,
|
2023-09-01 06:03:37 +00:00
|
|
|
"current_reserve_stamina": daily_info.current_reserve_stamina,
|
|
|
|
"is_reserve_stamina_full": daily_info.is_reserve_stamina_full,
|
2023-04-26 12:02:57 +00:00
|
|
|
"expeditions": bool(daily_info.expeditions),
|
|
|
|
"remained_time": remained_time,
|
|
|
|
"current_expeditions": len(daily_info.expeditions),
|
2023-05-02 16:16:14 +00:00
|
|
|
"max_expeditions": daily_info.total_expedition_num,
|
2023-07-20 06:29:25 +00:00
|
|
|
"current_train_score": daily_info.current_train_score,
|
|
|
|
"max_train_score": daily_info.max_train_score,
|
|
|
|
"remaining_weekly_discounts": daily_info.remaining_weekly_discounts,
|
|
|
|
"max_weekly_discounts": daily_info.max_weekly_discounts,
|
|
|
|
"current_rogue_score": daily_info.current_rogue_score,
|
|
|
|
"max_rogue_score": daily_info.max_rogue_score,
|
2023-04-26 12:02:57 +00:00
|
|
|
}
|
|
|
|
render_result = await self.template_service.render(
|
|
|
|
"starrail/daily_note/daily_note.html",
|
|
|
|
render_data,
|
2023-09-04 11:31:23 +00:00
|
|
|
{"width": 600, "height": 530},
|
2023-04-26 12:02:57 +00:00
|
|
|
full_page=False,
|
|
|
|
ttl=8 * 60,
|
|
|
|
)
|
|
|
|
return render_result
|
|
|
|
|
2024-02-19 12:55:13 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_task_button(bot_username: str) -> InlineKeyboardMarkup:
|
|
|
|
return InlineKeyboardMarkup(
|
|
|
|
[[InlineKeyboardButton(">> 设置状态提醒 <<", url=create_deep_linked_url(bot_username, "daily_note_tasks"))]]
|
|
|
|
)
|
|
|
|
|
2024-03-25 12:43:11 +00:00
|
|
|
@handler.command("dailynote", cookie=True, block=False)
|
|
|
|
@handler.message(filters.Regex("^当前状态(.*)"), cookie=True, block=False)
|
2024-02-19 12:55:13 +00:00
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> Optional[int]:
|
2024-03-10 12:50:32 +00:00
|
|
|
user_id = await self.get_real_user_id(update)
|
2023-04-26 12:02:57 +00:00
|
|
|
message = update.effective_message
|
2024-03-10 12:50:32 +00:00
|
|
|
self.log_user(update, logger.info, "每日便签命令请求")
|
2023-04-26 12:02:57 +00:00
|
|
|
|
|
|
|
try:
|
2024-03-10 12:50:32 +00:00
|
|
|
async with self.helper.genshin(user_id) as client:
|
2023-07-19 05:52:30 +00:00
|
|
|
render_result = await self._get_daily_note(client)
|
2023-04-26 12:02:57 +00:00
|
|
|
except DataNotPublic:
|
2024-03-16 10:43:20 +00:00
|
|
|
reply_message = await message.reply_text(
|
|
|
|
"查询失败惹,可能是便签功能被禁用了?请尝试通过米游社或者 hoyolab 获取一次便签信息后重试。"
|
|
|
|
)
|
2023-04-26 12:02:57 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
|
|
|
self.add_delete_message_job(reply_message, delay=30)
|
|
|
|
self.add_delete_message_job(message, delay=30)
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
2024-02-19 12:55:13 +00:00
|
|
|
await render_result.reply_photo(
|
|
|
|
message,
|
|
|
|
filename=f"{client.player_id}.png",
|
|
|
|
reply_markup=self.get_task_button(context.bot.username),
|
|
|
|
)
|