2022-07-31 08:15:09 +00:00
|
|
|
|
import os
|
2022-09-01 09:04:06 +00:00
|
|
|
|
import re
|
2022-10-14 04:05:40 +00:00
|
|
|
|
from datetime import datetime
|
2022-09-10 14:46:49 +00:00
|
|
|
|
from typing import Dict
|
2022-07-31 08:15:09 +00:00
|
|
|
|
|
2022-09-01 09:04:06 +00:00
|
|
|
|
from bs4 import BeautifulSoup
|
2022-07-31 08:15:09 +00:00
|
|
|
|
from pyppeteer import launch
|
|
|
|
|
from telegram import Update
|
|
|
|
|
from telegram.constants import ChatAction
|
2022-09-10 14:46:49 +00:00
|
|
|
|
from telegram.ext import CallbackContext, CommandHandler, MessageHandler, filters
|
2022-07-31 08:15:09 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.baseplugin import BasePlugin
|
|
|
|
|
from core.plugin import Plugin, handler
|
2022-08-06 12:37:41 +00:00
|
|
|
|
from core.template import TemplateService
|
2022-10-08 00:59:08 +00:00
|
|
|
|
from modules.apihelper.hyperion import GachaInfo
|
2022-08-09 12:25:25 +00:00
|
|
|
|
from plugins.genshin.gacha.wish import WishCountInfo, get_one
|
2022-07-31 08:15:09 +00:00
|
|
|
|
from utils.bot import get_all_args
|
|
|
|
|
from utils.decorators.error import error_callable
|
|
|
|
|
from utils.decorators.restricts import restricts
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from utils.log import logger
|
2022-07-31 08:15:09 +00:00
|
|
|
|
|
|
|
|
|
|
2022-09-28 00:47:26 +00:00
|
|
|
|
class GachaNotFound(Exception):
|
|
|
|
|
"""卡池未找到"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, gacha_name):
|
|
|
|
|
super().__init__(f"{gacha_name} gacha not found")
|
|
|
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
class Gacha(Plugin, BasePlugin):
|
2022-07-31 08:15:09 +00:00
|
|
|
|
"""抽卡模拟器(非首模拟器/减寿模拟器)"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, template_service: TemplateService = None):
|
|
|
|
|
self.gacha = GachaInfo()
|
|
|
|
|
self.template_service = template_service
|
|
|
|
|
self.browser: launch = None
|
|
|
|
|
self.current_dir = os.getcwd()
|
|
|
|
|
self.resources_dir = os.path.join(self.current_dir, "resources")
|
|
|
|
|
self.character_gacha_card = {}
|
|
|
|
|
self.user_time = {}
|
|
|
|
|
|
|
|
|
|
async def gacha_info(self, gacha_name: str = "角色活动", default: bool = False):
|
|
|
|
|
gacha_list_info = await self.gacha.get_gacha_list_info()
|
|
|
|
|
gacha_id = ""
|
2022-10-14 04:05:40 +00:00
|
|
|
|
now = datetime.now()
|
|
|
|
|
for gacha in gacha_list_info:
|
|
|
|
|
if gacha.gacha_name == gacha_name and gacha.begin_time <= now <= gacha.end_time:
|
|
|
|
|
gacha_id = gacha.gacha_id
|
2022-07-31 08:15:09 +00:00
|
|
|
|
if gacha_id == "":
|
2022-10-14 04:05:40 +00:00
|
|
|
|
if default and len(gacha_list_info) > 0:
|
|
|
|
|
gacha_id = gacha_list_info[0].gacha_id
|
2022-07-31 08:15:09 +00:00
|
|
|
|
else:
|
2022-09-28 00:47:26 +00:00
|
|
|
|
raise GachaNotFound(gacha_name)
|
2022-07-31 08:15:09 +00:00
|
|
|
|
gacha_info = await self.gacha.get_gacha_info(gacha_id)
|
|
|
|
|
gacha_info["gacha_id"] = gacha_id
|
|
|
|
|
return gacha_info
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
@handler(CommandHandler, command="gacha", block=False)
|
|
|
|
|
@handler(MessageHandler, filters=filters.Regex("^非首模拟器(.*)"), block=False)
|
2022-09-10 11:57:23 +00:00
|
|
|
|
@restricts(restricts_time=3, restricts_time_of_groups=20)
|
2022-07-31 08:15:09 +00:00
|
|
|
|
@error_callable
|
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> None:
|
2022-09-18 08:35:37 +00:00
|
|
|
|
message = update.effective_message
|
2022-07-31 08:15:09 +00:00
|
|
|
|
user = update.effective_user
|
|
|
|
|
args = get_all_args(context)
|
|
|
|
|
gacha_name = "角色活动"
|
|
|
|
|
if len(args) >= 1:
|
|
|
|
|
gacha_name = args[0]
|
|
|
|
|
if gacha_name not in ("角色活动-2", "武器活动", "常驻", "角色活动"):
|
|
|
|
|
for key, value in {"2": "角色活动-2", "武器": "武器活动", "普通": "常驻"}.items():
|
|
|
|
|
if key == gacha_name:
|
|
|
|
|
gacha_name = value
|
|
|
|
|
break
|
2022-09-28 00:47:26 +00:00
|
|
|
|
try:
|
|
|
|
|
gacha_info = await self.gacha_info(gacha_name)
|
|
|
|
|
except GachaNotFound:
|
2022-07-31 08:15:09 +00:00
|
|
|
|
await message.reply_text(f"没有找到名为 {gacha_name} 的卡池")
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
gacha_info = await self.gacha_info(default=True)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.info(f"用户 {user.full_name}[{user.id}] 抽卡模拟器命令请求 || 参数 {gacha_name}")
|
2022-07-31 08:15:09 +00:00
|
|
|
|
# 用户数据储存和处理
|
|
|
|
|
gacha_id: str = gacha_info["gacha_id"]
|
2022-09-10 14:46:49 +00:00
|
|
|
|
user_gacha: Dict[str, WishCountInfo] = context.user_data.get("gacha")
|
2022-07-31 08:15:09 +00:00
|
|
|
|
if user_gacha is None:
|
|
|
|
|
user_gacha = context.user_data["gacha"] = {}
|
|
|
|
|
user_gacha_count: WishCountInfo = user_gacha.get(gacha_id)
|
|
|
|
|
if user_gacha_count is None:
|
|
|
|
|
user_gacha_count = user_gacha[gacha_id] = WishCountInfo(user_id=user.id)
|
|
|
|
|
# 用户数据储存和处理
|
2022-09-01 09:04:06 +00:00
|
|
|
|
title = gacha_info["title"]
|
|
|
|
|
re_color = re.search(r"<color=#(.*?)>", title, flags=0)
|
|
|
|
|
if re_color is None:
|
|
|
|
|
title_html = BeautifulSoup(title, "lxml")
|
|
|
|
|
pool_name = title_html.text
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.warning(f"卡池信息 title 提取 color 失败 title[{title}]")
|
2022-09-01 09:04:06 +00:00
|
|
|
|
else:
|
|
|
|
|
color = re_color.group(1)
|
|
|
|
|
title_html = BeautifulSoup(title, "lxml")
|
|
|
|
|
title_html.color.name = "span"
|
|
|
|
|
title_html.span["style"] = f"color:#{color};"
|
|
|
|
|
pool_name = title_html.p
|
2022-07-31 08:15:09 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
|
|
|
|
data = {
|
|
|
|
|
"_res_path": f"file://{self.resources_dir}",
|
|
|
|
|
"name": f"{user.full_name}",
|
|
|
|
|
"info": gacha_name,
|
2022-09-01 09:04:06 +00:00
|
|
|
|
"poolName": pool_name,
|
2022-07-31 08:15:09 +00:00
|
|
|
|
"items": [],
|
|
|
|
|
}
|
|
|
|
|
for _ in range(10):
|
|
|
|
|
item = get_one(user_gacha_count, gacha_info)
|
|
|
|
|
# 下面为忽略的代码,因为metadata未完善,具体武器和角色类型无法显示
|
|
|
|
|
# item_name = item["item_name"]
|
|
|
|
|
# item_type = item["item_type"]
|
|
|
|
|
# if item_type == "角色":
|
|
|
|
|
# gacha_card = self.character_gacha_card.get(item_name)
|
|
|
|
|
# if gacha_card is None:
|
|
|
|
|
# await message.reply_text(f"获取角色 {item_name} GachaCard信息失败")
|
|
|
|
|
# return
|
|
|
|
|
# item["item_character_img"] = await url_to_file(gacha_card)
|
|
|
|
|
data["items"].append(item)
|
|
|
|
|
|
|
|
|
|
def take_rang(elem: dict):
|
|
|
|
|
return elem["rank"]
|
|
|
|
|
|
|
|
|
|
data["items"].sort(key=take_rang, reverse=True)
|
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
|
|
|
|
# 因为 gacha_info["title"] 返回的是 HTML 标签 尝试关闭自动转义
|
|
|
|
|
png_data = await self.template_service.render(
|
2022-10-12 13:39:47 +00:00
|
|
|
|
"genshin/gacha/gacha.html", data, {"width": 1157, "height": 603}, False
|
2022-09-02 08:27:54 +00:00
|
|
|
|
)
|
2022-07-31 08:15:09 +00:00
|
|
|
|
|
|
|
|
|
reply_message = await message.reply_photo(png_data)
|
|
|
|
|
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)
|