PaiGram/plugins/gacha.py

111 lines
4.7 KiB
Python
Raw Normal View History

2022-04-14 07:18:45 +00:00
import os
2022-06-09 07:12:06 +00:00
from pyppeteer import launch
2022-04-14 07:18:45 +00:00
from telegram import Update
from telegram.constants import ChatAction
from telegram.ext import ConversationHandler, filters, CommandHandler, MessageHandler
2022-04-14 07:18:45 +00:00
from logger import Log
2022-06-26 06:17:43 +00:00
from manager import listener_plugins_class
2022-06-22 13:33:07 +00:00
from plugins.base import BasePlugins, restricts
from plugins.errorhandler import conversation_error_handler
from service.wish import WishCountInfo, get_one
from utils.base import PaimonContext
2022-04-14 07:18:45 +00:00
2022-06-26 06:17:43 +00:00
@listener_plugins_class()
2022-04-14 07:18:45 +00:00
class Gacha(BasePlugins):
2022-06-09 12:52:59 +00:00
"""
抽卡模拟器非首模拟器/减寿模拟器
"""
@classmethod
def create_handlers(cls) -> list:
gacha = cls()
2022-06-26 06:17:43 +00:00
return [
CommandHandler("gacha", gacha.command_start, block=False),
MessageHandler(filters.Regex("^抽卡模拟器(.*)"), gacha.command_start, block=False),
MessageHandler(filters.Regex("^非首模拟器(.*)"), gacha.command_start, block=False),
]
def __init__(self):
2022-04-14 07:18:45 +00:00
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 = {}
2022-04-14 07:18:45 +00:00
CHECK_SERVER, COMMAND_RESULT = range(10600, 10602)
@conversation_error_handler
2022-06-26 05:47:50 +00:00
@restricts(filters.ChatType.GROUPS, restricts_time=20, try_delete_message=True)
@restricts(filters.ChatType.PRIVATE)
async def command_start(self, update: Update, context: PaimonContext) -> int:
2022-04-14 07:18:45 +00:00
message = update.message
user = update.effective_user
2022-06-05 08:13:10 +00:00
args = context.args
2022-06-24 07:57:24 +00:00
match = context.match
service = context.service
2022-05-18 15:35:02 +00:00
gacha_name = "角色活动"
2022-06-24 07:57:24 +00:00
if args is None:
if match is not None:
2022-06-24 12:26:15 +00:00
match_data = match.group(1)
if match_data != "":
gacha_name = match_data
2022-06-24 07:57:24 +00:00
else:
2022-06-21 13:29:44 +00:00
if len(args) >= 1:
gacha_name = args[0]
if gacha_name not in ("角色活动-2", "武器活动", "常驻", "角色活动"):
2022-06-24 07:57:24 +00:00
for key, value in {"2": "角色活动-2", "武器": "武器活动", "普通": "常驻"}.items():
if key == gacha_name:
gacha_name = value
break
Log.info(f"用户 {user.full_name}[{user.id}] 抽卡模拟器命令请求 || 参数 {gacha_name}")
gacha_info = await service.gacha.gacha_info(gacha_name, default=False if len(args) >= 1 else True)
2022-04-14 07:18:45 +00:00
# 用户数据储存和处理
if gacha_info.get("gacha_id") is None:
2022-05-18 15:35:02 +00:00
await message.reply_text(f"没有找到名为 {gacha_name} 的卡池")
2022-04-14 07:18:45 +00:00
return ConversationHandler.END
gacha_id: str = gacha_info["gacha_id"]
user_gacha: dict[str, WishCountInfo] = context.user_data.get("gacha")
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-05-30 08:08:56 +00:00
await message.reply_chat_action(ChatAction.TYPING)
2022-04-14 07:18:45 +00:00
data = {
"_res_path": f"file://{self.resources_dir}",
2022-04-20 08:06:16 +00:00
"name": f"{user.full_name}",
2022-05-18 15:35:02 +00:00
"info": gacha_name,
2022-04-14 07:18:45 +00:00
"poolName": gacha_info["title"],
"items": [],
}
2022-06-09 12:16:01 +00:00
for _ in range(10):
2022-04-14 07:18:45 +00:00
item = get_one(user_gacha_count, gacha_info)
2022-06-09 12:52:59 +00:00
# 下面为忽略的代码因为metadata未完善具体武器和角色类型无法显示
2022-04-14 07:18:45 +00:00
# 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 service.template.render('genshin/gacha', "gacha.html", data,
{"width": 1157, "height": 603}, False, False)
2022-04-14 07:18:45 +00:00
reply_message = await message.reply_photo(png_data)
2022-04-20 08:18:55 +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)