2022-10-29 17:00:13 +00:00
|
|
|
|
import re
|
|
|
|
|
from datetime import datetime
|
2023-07-18 09:29:31 +00:00
|
|
|
|
from typing import List, Optional, TYPE_CHECKING
|
2022-10-29 17:00:13 +00:00
|
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
|
from simnet.client.routes import Route
|
|
|
|
|
from simnet.errors import BadRequest as SimnetBadRequest
|
|
|
|
|
from simnet.utils.player import recognize_genshin_server, recognize_genshin_game_biz
|
|
|
|
|
from telegram import InlineKeyboardMarkup, InlineKeyboardButton
|
2023-02-13 05:13:32 +00:00
|
|
|
|
from telegram.constants import ParseMode
|
2023-07-18 09:29:31 +00:00
|
|
|
|
from telegram.ext import filters, MessageHandler, CommandHandler
|
2023-02-13 05:13:32 +00:00
|
|
|
|
from telegram.helpers import create_deep_linked_url
|
2022-10-29 17:00:13 +00:00
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
from core.basemodel import RegionEnum
|
2022-10-29 17:00:13 +00:00
|
|
|
|
from core.plugin import Plugin, handler
|
2023-03-14 01:27:22 +00:00
|
|
|
|
from core.services.cookies import CookiesService
|
|
|
|
|
from core.services.users.services import UserService
|
2022-10-29 17:00:13 +00:00
|
|
|
|
from metadata.genshin import AVATAR_DATA
|
|
|
|
|
from metadata.shortname import roleToId, roleToName
|
2023-02-16 08:47:48 +00:00
|
|
|
|
from modules.apihelper.client.components.calendar import Calendar
|
2023-07-18 09:29:31 +00:00
|
|
|
|
from plugins.tools.genshin import PlayerNotFoundError, CookiesNotFoundError, GenshinHelper
|
2022-10-29 17:00:13 +00:00
|
|
|
|
from utils.log import logger
|
2023-02-13 05:13:32 +00:00
|
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from simnet import GenshinClient
|
|
|
|
|
from telegram import Update
|
|
|
|
|
from telegram.ext import ContextTypes
|
|
|
|
|
|
2023-02-13 05:13:32 +00:00
|
|
|
|
BIRTHDAY_URL = Route(
|
|
|
|
|
"https://hk4e-api.mihoyo.com/event/birthdaystar/account/post_my_draw",
|
|
|
|
|
)
|
2022-10-29 17:00:13 +00:00
|
|
|
|
|
|
|
|
|
|
2022-11-01 14:12:18 +00:00
|
|
|
|
def rm_starting_str(string, starting):
|
|
|
|
|
"""Remove the starting character from a string."""
|
|
|
|
|
while string[0] == str(starting):
|
|
|
|
|
string = string[1:]
|
|
|
|
|
return string
|
|
|
|
|
|
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
class BirthdayPlugin(Plugin):
|
2022-10-29 17:00:13 +00:00
|
|
|
|
"""Birthday."""
|
|
|
|
|
|
2023-02-13 05:13:32 +00:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
2023-03-14 01:27:22 +00:00
|
|
|
|
user_service: UserService,
|
|
|
|
|
helper: GenshinHelper,
|
|
|
|
|
cookie_service: CookiesService,
|
2023-02-13 05:13:32 +00:00
|
|
|
|
):
|
2022-10-29 17:00:13 +00:00
|
|
|
|
"""Load Data."""
|
2023-02-28 14:15:48 +00:00
|
|
|
|
self.birthday_list = {}
|
2023-02-13 05:13:32 +00:00
|
|
|
|
self.user_service = user_service
|
|
|
|
|
self.cookie_service = cookie_service
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.helper = helper
|
2023-02-13 05:13:32 +00:00
|
|
|
|
|
2023-03-14 04:20:54 +00:00
|
|
|
|
async def initialize(self):
|
2023-02-28 14:15:48 +00:00
|
|
|
|
self.birthday_list = await Calendar.async_gen_birthday_list()
|
|
|
|
|
self.birthday_list.get("6_1", []).append("派蒙")
|
|
|
|
|
|
|
|
|
|
async def get_today_birthday(self) -> List[str]:
|
2023-02-13 05:13:32 +00:00
|
|
|
|
key = (
|
|
|
|
|
rm_starting_str(datetime.now().strftime("%m"), "0")
|
|
|
|
|
+ "_"
|
|
|
|
|
+ rm_starting_str(datetime.now().strftime("%d"), "0")
|
|
|
|
|
)
|
2023-02-13 16:12:47 +00:00
|
|
|
|
return (self.birthday_list.get(key, [])).copy()
|
2022-10-29 17:00:13 +00:00
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
@handler.command(command="birthday", block=False)
|
2023-07-18 09:29:31 +00:00
|
|
|
|
async def command_start(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE") -> None:
|
2022-10-29 17:00:13 +00:00
|
|
|
|
message = update.effective_message
|
|
|
|
|
user = update.effective_user
|
2022-11-01 16:47:25 +00:00
|
|
|
|
key = (
|
|
|
|
|
rm_starting_str(datetime.now().strftime("%m"), "0")
|
|
|
|
|
+ "_"
|
|
|
|
|
+ rm_starting_str(datetime.now().strftime("%d"), "0")
|
|
|
|
|
)
|
2023-03-14 04:19:09 +00:00
|
|
|
|
args = self.get_args(context)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
2022-10-29 17:00:13 +00:00
|
|
|
|
if len(args) >= 1:
|
|
|
|
|
msg = args[0]
|
2023-03-14 01:27:22 +00:00
|
|
|
|
logger.info("用户 %s[%s] 查询角色生日命令请求 || 参数 %s", user.full_name, user.id, msg)
|
2022-10-29 17:00:13 +00:00
|
|
|
|
if re.match(r"\d{1,2}.\d{1,2}", msg):
|
|
|
|
|
try:
|
2022-11-01 14:12:18 +00:00
|
|
|
|
month = rm_starting_str(re.findall(r"\d+", msg)[0], "0")
|
|
|
|
|
day = rm_starting_str(re.findall(r"\d+", msg)[1], "0")
|
|
|
|
|
key = f"{month}_{day}"
|
2022-10-29 17:00:13 +00:00
|
|
|
|
day_list = self.birthday_list.get(key, [])
|
2022-11-01 14:12:18 +00:00
|
|
|
|
date = f"{month}月{day}日"
|
2023-02-28 14:15:48 +00:00
|
|
|
|
text = f"{date} 是 {'、'.join(day_list)} 的生日哦~" if day_list else f"{date} 没有角色过生日哦~"
|
2022-10-29 17:00:13 +00:00
|
|
|
|
except IndexError:
|
|
|
|
|
text = "请输入正确的日期格式,如1-1,或输入正确的角色名称。"
|
|
|
|
|
reply_message = await message.reply_text(text)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
2022-10-29 17:00:13 +00:00
|
|
|
|
else:
|
|
|
|
|
try:
|
2022-11-01 14:12:18 +00:00
|
|
|
|
if msg == "派蒙":
|
2023-03-13 14:47:36 +00:00
|
|
|
|
text = "派蒙的生日是6月1日哦~"
|
|
|
|
|
elif roleToName(msg) == "旅行者":
|
|
|
|
|
text = "喂,旅行者!你该不会忘掉自己的生日了吧?"
|
2022-11-01 14:12:18 +00:00
|
|
|
|
else:
|
|
|
|
|
name = roleToName(msg)
|
|
|
|
|
aid = str(roleToId(msg))
|
|
|
|
|
birthday = AVATAR_DATA[aid]["birthday"]
|
2023-03-13 14:47:36 +00:00
|
|
|
|
text = f"{name} 的生日是 {birthday[0]}月{birthday[1]}日 哦~"
|
2022-10-29 17:00:13 +00:00
|
|
|
|
reply_message = await message.reply_text(text)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
2022-10-29 17:00:13 +00:00
|
|
|
|
except KeyError:
|
2022-11-01 14:12:18 +00:00
|
|
|
|
reply_message = await message.reply_text("请输入正确的日期格式,如1-1,或输入正确的角色名称。")
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
2022-10-29 17:00:13 +00:00
|
|
|
|
else:
|
2023-03-14 01:27:22 +00:00
|
|
|
|
logger.info("用户 %s[%s] 查询今日角色生日列表", user.full_name, user.id)
|
2023-02-28 14:15:48 +00:00
|
|
|
|
today_list = await self.get_today_birthday()
|
|
|
|
|
text = f"今天是 {'、'.join(today_list)} 的生日哦~" if today_list else "今天没有角色过生日哦~"
|
2022-10-29 17:00:13 +00:00
|
|
|
|
reply_message = await message.reply_text(text)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
|
|
|
|
self.add_delete_message_job(message)
|
|
|
|
|
self.add_delete_message_job(reply_message)
|
2023-02-13 05:13:32 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2023-07-18 09:29:31 +00:00
|
|
|
|
async def get_card(client: "GenshinClient", role_id: int) -> None:
|
2023-02-13 05:13:32 +00:00
|
|
|
|
"""领取画片"""
|
|
|
|
|
url = BIRTHDAY_URL.get_url()
|
|
|
|
|
params = {
|
2023-07-18 09:29:31 +00:00
|
|
|
|
"game_biz": recognize_genshin_game_biz(client.player_id),
|
2023-02-13 05:13:32 +00:00
|
|
|
|
"lang": "zh-cn",
|
2023-07-18 09:29:31 +00:00
|
|
|
|
"badge_uid": client.player_id,
|
|
|
|
|
"badge_region": recognize_genshin_server(client.player_id),
|
2023-02-13 05:13:32 +00:00
|
|
|
|
"activity_id": "20220301153521",
|
|
|
|
|
}
|
|
|
|
|
json = {
|
|
|
|
|
"role_id": role_id,
|
|
|
|
|
}
|
2023-07-18 09:29:31 +00:00
|
|
|
|
await client.request_lab(url, method="POST", params=params, data=json)
|
2023-02-13 05:13:32 +00:00
|
|
|
|
|
2023-02-28 14:15:48 +00:00
|
|
|
|
@staticmethod
|
2023-02-28 14:25:05 +00:00
|
|
|
|
def role_to_id(name: str) -> Optional[int]:
|
2023-02-28 14:15:48 +00:00
|
|
|
|
if name == "派蒙":
|
|
|
|
|
return -1
|
|
|
|
|
return roleToId(name)
|
|
|
|
|
|
2023-02-13 05:13:32 +00:00
|
|
|
|
@handler(CommandHandler, command="birthday_card", block=False)
|
|
|
|
|
@handler(MessageHandler, filters=filters.Regex("^领取角色生日画片$"), block=False)
|
2023-07-18 09:29:31 +00:00
|
|
|
|
async def command_birthday_card_start(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE") -> None:
|
2023-02-13 05:13:32 +00:00
|
|
|
|
message = update.effective_message
|
|
|
|
|
user = update.effective_user
|
|
|
|
|
logger.info("用户 %s[%s] 领取生日画片命令请求", user.full_name, user.id)
|
2023-02-28 14:15:48 +00:00
|
|
|
|
today_list = await self.get_today_birthday()
|
2023-02-13 05:13:32 +00:00
|
|
|
|
if not today_list:
|
|
|
|
|
reply_message = await message.reply_text("今天没有角色过生日哦~")
|
|
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(message)
|
|
|
|
|
self.add_delete_message_job(reply_message)
|
2023-02-13 05:13:32 +00:00
|
|
|
|
return
|
2023-03-15 08:03:44 +00:00
|
|
|
|
try:
|
2023-07-18 09:29:31 +00:00
|
|
|
|
async with self.helper.genshin(user.id) as client:
|
|
|
|
|
if client.region == RegionEnum.HOYOLAB:
|
|
|
|
|
text = "此功能当前只支持国服账号哦~"
|
|
|
|
|
else:
|
|
|
|
|
game_biz = recognize_genshin_game_biz(client.player_id)
|
|
|
|
|
region = recognize_genshin_server(client.player_id)
|
|
|
|
|
await client.get_hk4e_token_by_cookie_token(game_biz, region)
|
|
|
|
|
for name in today_list.copy():
|
|
|
|
|
if role_id := self.role_to_id(name):
|
|
|
|
|
try:
|
|
|
|
|
await self.get_card(client, role_id)
|
|
|
|
|
except SimnetBadRequest as e:
|
|
|
|
|
if e.ret_code in {-512008, -512009}: # 未过生日、已领取过
|
|
|
|
|
today_list.remove(name)
|
|
|
|
|
if today_list:
|
|
|
|
|
text = f"成功领取了 {'、'.join(today_list)} 的生日画片~"
|
|
|
|
|
else:
|
|
|
|
|
text = "没有领取到生日画片哦 ~ 可能是已经领取过了"
|
|
|
|
|
reply_message = await message.reply_text(text)
|
|
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
|
|
|
|
self.add_delete_message_job(message)
|
|
|
|
|
self.add_delete_message_job(reply_message)
|
2023-03-15 08:03:44 +00:00
|
|
|
|
except (CookiesNotFoundError, PlayerNotFoundError):
|
2023-02-13 05:13:32 +00:00
|
|
|
|
buttons = [[InlineKeyboardButton("点我绑定账号", url=create_deep_linked_url(context.bot.username, "set_cookie"))]]
|
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
|
|
|
|
reply_msg = await message.reply_text(
|
|
|
|
|
"此功能需要绑定<code>cookie</code>后使用,请先私聊派蒙绑定账号",
|
|
|
|
|
reply_markup=InlineKeyboardMarkup(buttons),
|
|
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
|
)
|
2023-04-04 14:53:37 +00:00
|
|
|
|
self.add_delete_message_job(reply_msg, delay=30)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(message, delay=30)
|
2023-02-13 05:13:32 +00:00
|
|
|
|
else:
|
|
|
|
|
await message.reply_text(
|
|
|
|
|
"此功能需要绑定<code>cookie</code>后使用,请先私聊派蒙进行绑定",
|
|
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
|
reply_markup=InlineKeyboardMarkup(buttons),
|
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
return
|