2022-10-30 13:35:53 +00:00
|
|
|
|
import asyncio
|
2022-07-31 05:25:02 +00:00
|
|
|
|
import datetime
|
2022-10-30 13:35:53 +00:00
|
|
|
|
import random
|
2022-10-10 07:46:39 +00:00
|
|
|
|
import time
|
2022-10-10 05:34:06 +00:00
|
|
|
|
from json import JSONDecodeError
|
2022-11-17 08:07:18 +00:00
|
|
|
|
from typing import Optional, Tuple
|
2022-07-31 05:25:02 +00:00
|
|
|
|
|
|
|
|
|
from genshin import Game, GenshinException, AlreadyClaimed, Client
|
2022-10-30 13:35:53 +00:00
|
|
|
|
from genshin.utility import recognize_genshin_server
|
2022-10-10 07:46:39 +00:00
|
|
|
|
from httpx import AsyncClient, TimeoutException
|
2022-10-12 09:35:59 +00:00
|
|
|
|
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
2022-10-10 05:34:06 +00:00
|
|
|
|
from telegram.constants import ChatAction
|
2022-10-30 11:37:57 +00:00
|
|
|
|
from telegram.ext import CommandHandler, CallbackContext, CallbackQueryHandler
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from telegram.ext import MessageHandler, filters
|
2022-11-28 08:38:49 +00:00
|
|
|
|
from telegram.helpers import create_deep_linked_url
|
2022-07-31 05:25:02 +00:00
|
|
|
|
|
2022-09-09 17:06:23 +00:00
|
|
|
|
from core.admin.services import BotAdminService
|
2022-10-30 08:46:07 +00:00
|
|
|
|
from core.base.redisdb import RedisDB
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.baseplugin import BasePlugin
|
2022-10-30 08:46:07 +00:00
|
|
|
|
from core.bot import bot
|
2022-10-10 05:34:06 +00:00
|
|
|
|
from core.config import config
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.cookies.error import CookiesNotFoundError
|
2022-08-06 12:37:41 +00:00
|
|
|
|
from core.cookies.services import CookiesService
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.plugin import Plugin, handler
|
2022-08-06 12:37:41 +00:00
|
|
|
|
from core.sign.models import Sign as SignUser, SignStatusEnum
|
|
|
|
|
from core.sign.services import SignServices
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.user.error import UserNotFoundError
|
2022-08-06 12:37:41 +00:00
|
|
|
|
from core.user.services import UserService
|
2022-12-10 12:37:43 +00:00
|
|
|
|
from modules.apihelper.client.components.verify import Verify
|
2022-12-01 02:27:27 +00:00
|
|
|
|
from utils.bot import get_args
|
2022-07-31 05:25:02 +00:00
|
|
|
|
from utils.decorators.error import error_callable
|
|
|
|
|
from utils.decorators.restricts import restricts
|
|
|
|
|
from utils.helpers import get_genshin_client
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from utils.log import logger
|
2022-07-31 05:25:02 +00:00
|
|
|
|
|
|
|
|
|
|
2022-10-30 13:35:53 +00:00
|
|
|
|
class NeedChallenge(Exception):
|
|
|
|
|
def __init__(self, uid: int, gt: str = "", challenge: str = ""):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.uid = uid
|
|
|
|
|
self.gt = gt
|
|
|
|
|
self.challenge = challenge
|
2022-10-30 08:46:07 +00:00
|
|
|
|
|
2022-10-30 13:35:53 +00:00
|
|
|
|
|
|
|
|
|
class SignSystem:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
REFERER = (
|
|
|
|
|
"https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html?"
|
|
|
|
|
"bbs_auth_required=true&act_id=e202009291139501&utm_source=bbs&utm_medium=mys&utm_campaign=icon"
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-30 13:35:53 +00:00
|
|
|
|
def __init__(self, redis: RedisDB):
|
|
|
|
|
self.cache = redis.client
|
|
|
|
|
self.qname = "plugin:sign:"
|
2022-12-10 12:37:43 +00:00
|
|
|
|
self.verify = Verify()
|
2022-10-30 13:35:53 +00:00
|
|
|
|
|
|
|
|
|
async def get_challenge(self, uid: int) -> Tuple[Optional[str], Optional[str]]:
|
|
|
|
|
data = await self.cache.get(f"{self.qname}{uid}")
|
2022-10-30 11:37:57 +00:00
|
|
|
|
if not data:
|
|
|
|
|
return None, None
|
|
|
|
|
data = data.decode("utf-8").split("|")
|
|
|
|
|
return data[0], data[1]
|
2022-10-30 08:46:07 +00:00
|
|
|
|
|
2022-10-30 13:35:53 +00:00
|
|
|
|
async def set_challenge(self, uid: int, gt: str, challenge: str):
|
|
|
|
|
await self.cache.set(f"{self.qname}{uid}", f"{gt}|{challenge}")
|
|
|
|
|
await self.cache.expire(f"{self.qname}{uid}", 10 * 60)
|
2022-10-30 08:46:07 +00:00
|
|
|
|
|
2022-11-12 13:38:28 +00:00
|
|
|
|
async def get_challenge_button(
|
2022-11-12 17:33:32 +00:00
|
|
|
|
self, uid: int, user_id: int, gt: Optional[str] = None, challenge: Optional[str] = None, callback: bool = True
|
2022-10-30 13:35:53 +00:00
|
|
|
|
) -> Optional[InlineKeyboardMarkup]:
|
|
|
|
|
if not config.pass_challenge_user_web:
|
|
|
|
|
return None
|
2022-11-12 16:38:37 +00:00
|
|
|
|
if challenge and gt:
|
|
|
|
|
await self.set_challenge(uid, gt, challenge)
|
2022-11-12 13:38:28 +00:00
|
|
|
|
if not challenge or not gt:
|
|
|
|
|
gt, challenge = await self.get_challenge(uid)
|
|
|
|
|
if not challenge or not gt:
|
|
|
|
|
return None
|
|
|
|
|
if callback:
|
2022-10-30 13:35:53 +00:00
|
|
|
|
data = f"sign|{user_id}|{uid}"
|
|
|
|
|
return InlineKeyboardMarkup([[InlineKeyboardButton("请尽快点我进行手动验证", callback_data=data)]])
|
2022-11-12 13:38:28 +00:00
|
|
|
|
else:
|
2022-11-12 16:38:37 +00:00
|
|
|
|
url = f"{config.pass_challenge_user_web}?username={bot.app.bot.username}&command=sign>={gt}&challenge={challenge}&uid={uid}"
|
2022-11-12 13:38:28 +00:00
|
|
|
|
return InlineKeyboardMarkup([[InlineKeyboardButton("请尽快点我进行手动验证", url=url)]])
|
2022-07-31 08:17:27 +00:00
|
|
|
|
|
2022-11-17 08:07:18 +00:00
|
|
|
|
async def recognize(self, gt: str, challenge: str, referer: str = None) -> Optional[str]:
|
2022-10-10 05:34:06 +00:00
|
|
|
|
if not referer:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
referer = self.REFERER
|
|
|
|
|
if not gt or not challenge:
|
2022-10-10 05:34:06 +00:00
|
|
|
|
return None
|
|
|
|
|
pass_challenge_params = {
|
|
|
|
|
"gt": gt,
|
|
|
|
|
"challenge": challenge,
|
|
|
|
|
"referer": referer,
|
|
|
|
|
}
|
|
|
|
|
if config.pass_challenge_app_key:
|
|
|
|
|
pass_challenge_params["appkey"] = config.pass_challenge_app_key
|
2022-11-17 08:07:18 +00:00
|
|
|
|
headers = {
|
|
|
|
|
"Accept": "*/*",
|
|
|
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
|
|
|
|
|
"Chrome/107.0.0.0 Safari/537.36",
|
|
|
|
|
}
|
2022-10-10 07:46:39 +00:00
|
|
|
|
try:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
async with AsyncClient(headers=headers) as client:
|
2022-10-10 05:34:06 +00:00
|
|
|
|
resp = await client.post(
|
|
|
|
|
config.pass_challenge_api,
|
|
|
|
|
params=pass_challenge_params,
|
2022-10-12 03:45:06 +00:00
|
|
|
|
timeout=60,
|
2022-10-10 05:34:06 +00:00
|
|
|
|
)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.debug("recognize 请求返回:%s", resp.text)
|
2022-10-10 07:46:39 +00:00
|
|
|
|
data = resp.json()
|
|
|
|
|
status = data.get("status")
|
2022-11-17 08:07:18 +00:00
|
|
|
|
if status != 0:
|
|
|
|
|
logger.error("recognize 解析错误:[%s]%s", data.get("code"), data.get("msg"))
|
2022-10-10 07:46:39 +00:00
|
|
|
|
if data.get("code", 0) != 0:
|
|
|
|
|
raise RuntimeError
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.info("recognize 解析成功")
|
|
|
|
|
return data["data"]["validate"]
|
2022-10-10 07:46:39 +00:00
|
|
|
|
except JSONDecodeError:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.warning("recognize 请求 JSON 解析失败")
|
2022-10-12 03:45:06 +00:00
|
|
|
|
except TimeoutException as exc:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.warning("recognize 请求超时")
|
2022-10-12 03:45:06 +00:00
|
|
|
|
raise exc
|
2022-10-10 07:46:39 +00:00
|
|
|
|
except KeyError:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.warning("recognize 请求数据错误")
|
2022-10-10 07:46:39 +00:00
|
|
|
|
except RuntimeError:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.warning("recognize 请求失败")
|
2022-10-10 05:34:06 +00:00
|
|
|
|
return None
|
|
|
|
|
|
2022-10-30 11:37:57 +00:00
|
|
|
|
async def start_sign(
|
2022-10-30 13:35:53 +00:00
|
|
|
|
self,
|
|
|
|
|
client: Client,
|
2022-11-17 08:07:18 +00:00
|
|
|
|
challenge: Optional[str] = None,
|
|
|
|
|
validate: Optional[str] = None,
|
2022-10-30 13:35:53 +00:00
|
|
|
|
is_sleep: bool = False,
|
|
|
|
|
is_raise: bool = False,
|
|
|
|
|
title: Optional[str] = "签到结果",
|
|
|
|
|
) -> str:
|
|
|
|
|
if is_sleep:
|
|
|
|
|
if recognize_genshin_server(client.uid) in ("cn_gf01", "cn_qd01"):
|
|
|
|
|
await asyncio.sleep(random.randint(10, 300)) # nosec
|
|
|
|
|
else:
|
|
|
|
|
await asyncio.sleep(random.randint(0, 3)) # nosec
|
2022-07-31 05:25:02 +00:00
|
|
|
|
try:
|
|
|
|
|
rewards = await client.get_monthly_rewards(game=Game.GENSHIN, lang="zh-cn")
|
|
|
|
|
except GenshinException as error:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.warning("UID[%s] 获取签到信息失败,API返回信息为 %s", client.uid, str(error))
|
2022-10-30 13:35:53 +00:00
|
|
|
|
if is_raise:
|
|
|
|
|
raise error
|
|
|
|
|
return f"获取签到信息失败,API返回信息为 {str(error)}"
|
2022-07-31 05:25:02 +00:00
|
|
|
|
try:
|
|
|
|
|
daily_reward_info = await client.get_reward_info(game=Game.GENSHIN, lang="zh-cn") # 获取签到信息失败
|
|
|
|
|
except GenshinException as error:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.warning("UID[%s] 获取签到状态失败,API返回信息为 %s", client.uid, str(error))
|
2022-10-30 13:35:53 +00:00
|
|
|
|
if is_raise:
|
|
|
|
|
raise error
|
|
|
|
|
return f"获取签到状态失败,API返回信息为 {str(error)}"
|
2022-07-31 05:25:02 +00:00
|
|
|
|
if not daily_reward_info.signed_in:
|
|
|
|
|
try:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
if validate:
|
2022-11-18 08:35:14 +00:00
|
|
|
|
logger.info("UID[%s] 正在尝试通过验证码\nchallenge[%s]\nvalidate[%s]", client.uid, challenge, validate)
|
2022-10-10 05:34:06 +00:00
|
|
|
|
request_daily_reward = await client.request_daily_reward(
|
2022-10-30 08:46:07 +00:00
|
|
|
|
"sign",
|
|
|
|
|
method="POST",
|
|
|
|
|
game=Game.GENSHIN,
|
|
|
|
|
lang="zh-cn",
|
2022-11-17 08:07:18 +00:00
|
|
|
|
challenge=challenge,
|
|
|
|
|
validate=validate,
|
2022-10-10 05:34:06 +00:00
|
|
|
|
)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.debug("request_daily_reward 返回 %s", request_daily_reward)
|
2022-08-12 05:55:12 +00:00
|
|
|
|
if request_daily_reward and request_daily_reward.get("success", 0) == 1:
|
2022-10-30 13:35:53 +00:00
|
|
|
|
# 尝试通过 ajax 请求绕过签到
|
2022-11-17 08:07:18 +00:00
|
|
|
|
gt = request_daily_reward.get("gt", "")
|
|
|
|
|
challenge = request_daily_reward.get("challenge", "")
|
2022-11-19 08:15:30 +00:00
|
|
|
|
logger.warning("UID[%s] 触发验证码\ngt[%s]\nchallenge[%s]", client.uid, gt, challenge)
|
2022-12-10 12:37:43 +00:00
|
|
|
|
validate = await self.verify.ajax(
|
2022-11-17 08:07:18 +00:00
|
|
|
|
referer=self.REFERER,
|
|
|
|
|
gt=gt,
|
|
|
|
|
challenge=challenge,
|
2022-10-10 05:34:06 +00:00
|
|
|
|
)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
if validate:
|
2022-11-18 08:35:14 +00:00
|
|
|
|
logger.success("ajax 通过验证成功\nchallenge[%s]\nvalidate[%s]", challenge, validate)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
request_daily_reward = await client.request_daily_reward(
|
|
|
|
|
"sign",
|
|
|
|
|
method="POST",
|
|
|
|
|
game=Game.GENSHIN,
|
|
|
|
|
lang="zh-cn",
|
|
|
|
|
challenge=challenge,
|
|
|
|
|
validate=validate,
|
|
|
|
|
)
|
|
|
|
|
logger.debug("request_daily_reward 返回 %s", request_daily_reward)
|
|
|
|
|
if request_daily_reward and request_daily_reward.get("success", 0) == 1:
|
2022-11-18 08:35:14 +00:00
|
|
|
|
logger.warning("UID[%s] 触发验证码\nchallenge[%s]", client.uid, challenge)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
raise NeedChallenge(
|
|
|
|
|
uid=client.uid,
|
|
|
|
|
gt=request_daily_reward.get("gt", ""),
|
|
|
|
|
challenge=request_daily_reward.get("challenge", ""),
|
|
|
|
|
)
|
|
|
|
|
elif config.pass_challenge_app_key:
|
|
|
|
|
# 如果无法绕过 检查配置文件是否配置识别 API 尝试请求绕过
|
|
|
|
|
# 注意 需要重新获取没有进行任何请求的 Challenge
|
2022-11-18 08:35:14 +00:00
|
|
|
|
logger.info("UID[%s] 正在使用 recognize 重新请求签到", client.uid)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
_request_daily_reward = await client.request_daily_reward(
|
|
|
|
|
"sign",
|
|
|
|
|
method="POST",
|
|
|
|
|
game=Game.GENSHIN,
|
|
|
|
|
lang="zh-cn",
|
2022-10-30 08:46:07 +00:00
|
|
|
|
)
|
2022-11-18 08:35:14 +00:00
|
|
|
|
logger.debug("request_daily_reward 返回\n%s", _request_daily_reward)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
if _request_daily_reward and _request_daily_reward.get("success", 0) == 1:
|
|
|
|
|
_gt = _request_daily_reward.get("gt", "")
|
|
|
|
|
_challenge = _request_daily_reward.get("challenge", "")
|
2022-11-19 08:15:30 +00:00
|
|
|
|
logger.info("UID[%s] 创建验证码\ngt[%s]\nchallenge[%s]", client.uid, _gt, _challenge)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
_validate = await self.recognize(_gt, _challenge)
|
|
|
|
|
if _validate:
|
2022-11-18 08:35:14 +00:00
|
|
|
|
logger.success("recognize 通过验证成功\nchallenge[%s]\nvalidate[%s]", _challenge, _validate)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
request_daily_reward = await client.request_daily_reward(
|
|
|
|
|
"sign",
|
|
|
|
|
method="POST",
|
|
|
|
|
game=Game.GENSHIN,
|
|
|
|
|
lang="zh-cn",
|
|
|
|
|
challenge=_challenge,
|
|
|
|
|
validate=_validate,
|
|
|
|
|
)
|
|
|
|
|
if request_daily_reward and request_daily_reward.get("success", 0) == 1:
|
2022-11-19 08:15:30 +00:00
|
|
|
|
logger.warning("UID[%s] 触发验证码\nchallenge[%s]", client.uid, _challenge)
|
|
|
|
|
gt = request_daily_reward.get("gt", "")
|
|
|
|
|
challenge = request_daily_reward.get("challenge", "")
|
|
|
|
|
logger.success("UID[%s] 创建验证成功\ngt[%s]\nchallenge[%s]", client.uid, gt, challenge)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
raise NeedChallenge(
|
|
|
|
|
uid=client.uid,
|
2022-11-19 08:15:30 +00:00
|
|
|
|
gt=gt,
|
|
|
|
|
challenge=challenge,
|
2022-11-17 08:07:18 +00:00
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
logger.success("UID[%s] 通过 recognize 签到成功", client.uid)
|
|
|
|
|
else:
|
2022-11-18 08:35:14 +00:00
|
|
|
|
request_daily_reward = await client.request_daily_reward(
|
2022-11-19 08:15:30 +00:00
|
|
|
|
"sign", method="POST", game=Game.GENSHIN, lang="zh-cn"
|
2022-11-18 08:35:14 +00:00
|
|
|
|
)
|
|
|
|
|
gt = request_daily_reward.get("gt", "")
|
|
|
|
|
challenge = request_daily_reward.get("challenge", "")
|
2022-11-19 08:15:30 +00:00
|
|
|
|
logger.success("UID[%s] 创建验证成功\ngt[%s]\nchallenge[%s]", client.uid, gt, challenge)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
raise NeedChallenge(uid=client.uid, gt=gt, challenge=challenge)
|
|
|
|
|
else:
|
2022-11-18 08:35:14 +00:00
|
|
|
|
request_daily_reward = await client.request_daily_reward(
|
2022-11-19 08:15:30 +00:00
|
|
|
|
"sign", method="POST", game=Game.GENSHIN, lang="zh-cn"
|
2022-11-18 08:35:14 +00:00
|
|
|
|
)
|
|
|
|
|
gt = request_daily_reward.get("gt", "")
|
|
|
|
|
challenge = request_daily_reward.get("challenge", "")
|
2022-11-19 08:15:30 +00:00
|
|
|
|
logger.success("UID[%s] 创建验证成功\ngt[%s]\nchallenge[%s]", client.uid, gt, challenge)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
raise NeedChallenge(uid=client.uid, gt=gt, challenge=challenge)
|
|
|
|
|
else:
|
|
|
|
|
logger.success("UID[%s] 签到成功", client.uid)
|
2022-10-30 13:35:53 +00:00
|
|
|
|
except TimeoutException as error:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.warning("UID[%s] 签到请求超时", client.uid)
|
2022-10-30 13:35:53 +00:00
|
|
|
|
if is_raise:
|
|
|
|
|
raise error
|
|
|
|
|
return "签到失败了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ "
|
|
|
|
|
except AlreadyClaimed as error:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.warning("UID[%s] 已经签到", client.uid)
|
2022-10-30 13:35:53 +00:00
|
|
|
|
if is_raise:
|
|
|
|
|
raise error
|
2022-07-31 05:25:02 +00:00
|
|
|
|
result = "今天旅行者已经签到过了~"
|
|
|
|
|
except GenshinException as error:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.warning("UID %s 签到失败,API返回信息为 %s", client.uid, str(error))
|
2022-10-30 13:35:53 +00:00
|
|
|
|
if is_raise:
|
|
|
|
|
raise error
|
|
|
|
|
return f"获取签到状态失败,API返回信息为 {str(error)}"
|
2022-07-31 05:25:02 +00:00
|
|
|
|
else:
|
|
|
|
|
result = "OK"
|
|
|
|
|
else:
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.info("UID[%s] 已经签到", client.uid)
|
2022-07-31 05:25:02 +00:00
|
|
|
|
result = "今天旅行者已经签到过了~"
|
2022-11-17 08:07:18 +00:00
|
|
|
|
logger.info("UID[%s] 签到结果 %s", client.uid, result)
|
2022-07-31 05:25:02 +00:00
|
|
|
|
reward = rewards[daily_reward_info.claimed_rewards - (1 if daily_reward_info.signed_in else 0)]
|
|
|
|
|
today = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
|
|
|
|
cn_timezone = datetime.timezone(datetime.timedelta(hours=8))
|
|
|
|
|
now = datetime.datetime.now(cn_timezone)
|
|
|
|
|
missed_days = now.day - daily_reward_info.claimed_rewards
|
|
|
|
|
if not daily_reward_info.signed_in:
|
|
|
|
|
missed_days -= 1
|
2022-10-10 05:34:06 +00:00
|
|
|
|
message = (
|
2022-10-30 13:35:53 +00:00
|
|
|
|
f"#### {title} ####\n"
|
|
|
|
|
f"时间:{today} (UTC+8)\n"
|
2022-10-10 05:34:06 +00:00
|
|
|
|
f"UID: {client.uid}\n"
|
|
|
|
|
f"今日奖励: {reward.name} × {reward.amount}\n"
|
|
|
|
|
f"本月漏签次数:{missed_days}\n"
|
|
|
|
|
f"签到结果: {result}"
|
|
|
|
|
)
|
2022-10-30 13:35:53 +00:00
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Sign(Plugin, BasePlugin):
|
|
|
|
|
"""每日签到"""
|
|
|
|
|
|
|
|
|
|
CHECK_SERVER, COMMAND_RESULT = range(10400, 10402)
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
redis: RedisDB = None,
|
|
|
|
|
user_service: UserService = None,
|
|
|
|
|
cookies_service: CookiesService = None,
|
|
|
|
|
sign_service: SignServices = None,
|
|
|
|
|
bot_admin_service: BotAdminService = None,
|
|
|
|
|
):
|
|
|
|
|
self.bot_admin_service = bot_admin_service
|
|
|
|
|
self.cookies_service = cookies_service
|
|
|
|
|
self.user_service = user_service
|
|
|
|
|
self.sign_service = sign_service
|
|
|
|
|
self.system = SignSystem(redis)
|
2022-07-31 05:25:02 +00:00
|
|
|
|
|
2022-08-06 07:21:18 +00:00
|
|
|
|
async def _process_auto_sign(self, user_id: int, chat_id: int, method: str) -> str:
|
|
|
|
|
try:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await get_genshin_client(user_id)
|
|
|
|
|
except (UserNotFoundError, CookiesNotFoundError):
|
2022-08-06 07:21:18 +00:00
|
|
|
|
return "未查询到账号信息,请先私聊派蒙绑定账号"
|
|
|
|
|
user: SignUser = await self.sign_service.get_by_user_id(user_id)
|
|
|
|
|
if user:
|
|
|
|
|
if method == "关闭":
|
|
|
|
|
await self.sign_service.remove(user)
|
|
|
|
|
return "关闭自动签到成功"
|
|
|
|
|
elif method == "开启":
|
|
|
|
|
if user.chat_id == chat_id:
|
|
|
|
|
return "自动签到已经开启过了"
|
|
|
|
|
user.chat_id = chat_id
|
2022-10-12 13:22:41 +00:00
|
|
|
|
user.status = SignStatusEnum.STATUS_SUCCESS
|
2022-08-06 07:21:18 +00:00
|
|
|
|
await self.sign_service.update(user)
|
|
|
|
|
return "修改自动签到通知对话成功"
|
|
|
|
|
elif method == "关闭":
|
|
|
|
|
return "您还没有开启自动签到"
|
|
|
|
|
elif method == "开启":
|
2022-10-10 05:34:06 +00:00
|
|
|
|
user = SignUser(
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
chat_id=chat_id,
|
|
|
|
|
time_created=datetime.datetime.now(),
|
|
|
|
|
status=SignStatusEnum.STATUS_SUCCESS,
|
|
|
|
|
)
|
2022-08-06 07:21:18 +00:00
|
|
|
|
await self.sign_service.add(user)
|
|
|
|
|
return "开启自动签到成功"
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
@handler(CommandHandler, command="sign", block=False)
|
|
|
|
|
@handler(MessageHandler, filters=filters.Regex("^每日签到(.*)"), block=False)
|
|
|
|
|
@restricts()
|
2022-07-31 05:25:02 +00:00
|
|
|
|
@error_callable
|
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> None:
|
|
|
|
|
user = update.effective_user
|
2022-09-08 01:08:37 +00:00
|
|
|
|
message = update.effective_message
|
2022-12-01 02:27:27 +00:00
|
|
|
|
args = get_args(context)
|
2022-10-30 13:35:53 +00:00
|
|
|
|
validate: Optional[str] = None
|
2022-08-06 07:21:18 +00:00
|
|
|
|
if len(args) >= 1:
|
|
|
|
|
msg = None
|
|
|
|
|
if args[0] == "开启自动签到":
|
2022-09-09 17:06:23 +00:00
|
|
|
|
admin_list = await self.bot_admin_service.get_admin_list()
|
|
|
|
|
if user.id in admin_list:
|
|
|
|
|
msg = await self._process_auto_sign(user.id, message.chat_id, "开启")
|
|
|
|
|
else:
|
|
|
|
|
msg = await self._process_auto_sign(user.id, user.id, "开启")
|
2022-08-06 07:21:18 +00:00
|
|
|
|
elif args[0] == "关闭自动签到":
|
|
|
|
|
msg = await self._process_auto_sign(user.id, message.chat_id, "关闭")
|
2022-10-30 08:46:07 +00:00
|
|
|
|
else:
|
|
|
|
|
validate = args[0]
|
2022-08-06 07:21:18 +00:00
|
|
|
|
if msg:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.info(f"用户 {user.full_name}[{user.id}] 自动签到命令请求 || 参数 {args[0]}")
|
2022-08-06 07:21:18 +00:00
|
|
|
|
reply_message = await message.reply_text(msg)
|
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 30)
|
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id, 30)
|
|
|
|
|
return
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.info(f"用户 {user.full_name}[{user.id}] 每日签到命令请求")
|
2022-07-31 05:25:02 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id)
|
|
|
|
|
try:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
client = await get_genshin_client(user.id)
|
2022-10-10 05:34:06 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2022-11-17 08:07:18 +00:00
|
|
|
|
_, challenge = await self.system.get_challenge(client.uid)
|
2022-11-18 08:35:14 +00:00
|
|
|
|
if validate:
|
|
|
|
|
_, challenge = await self.system.get_challenge(client.uid)
|
|
|
|
|
if challenge:
|
|
|
|
|
sign_text = await self.system.start_sign(client, challenge=challenge, validate=validate)
|
|
|
|
|
else:
|
|
|
|
|
reply_message = await message.reply_text("请求已经过期", allow_sending_without_reply=True)
|
|
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id)
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
sign_text = await self.system.start_sign(client)
|
2022-10-30 13:35:53 +00:00
|
|
|
|
reply_message = await message.reply_text(sign_text, allow_sending_without_reply=True)
|
2022-07-31 05:25:02 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
except (UserNotFoundError, CookiesNotFoundError):
|
2022-11-28 08:38:49 +00:00
|
|
|
|
buttons = [[InlineKeyboardButton("点我绑定账号", url=create_deep_linked_url(context.bot.username, "set_cookie"))]]
|
2022-07-31 05:25:02 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2022-10-12 09:35:59 +00:00
|
|
|
|
reply_message = await message.reply_text(
|
|
|
|
|
"未查询到您所绑定的账号信息,请先私聊派蒙绑定账号", reply_markup=InlineKeyboardMarkup(buttons)
|
|
|
|
|
)
|
2022-07-31 05:25:02 +00:00
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 30)
|
2022-10-12 09:35:59 +00:00
|
|
|
|
|
2022-07-31 05:25:02 +00:00
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id, 30)
|
2022-10-12 09:35:59 +00:00
|
|
|
|
else:
|
2022-10-22 13:54:04 +00:00
|
|
|
|
await message.reply_text("未查询到您所绑定的账号信息,请先绑定账号", reply_markup=InlineKeyboardMarkup(buttons))
|
2022-10-30 13:35:53 +00:00
|
|
|
|
except NeedChallenge as exc:
|
2022-11-12 13:38:28 +00:00
|
|
|
|
button = await self.system.get_challenge_button(
|
2022-11-12 17:33:32 +00:00
|
|
|
|
exc.uid, user.id, exc.gt, exc.challenge, not filters.ChatType.PRIVATE.filter(message)
|
2022-10-30 13:35:53 +00:00
|
|
|
|
)
|
|
|
|
|
reply_message = await message.reply_text(
|
|
|
|
|
f"UID {exc.uid} 签到失败,触发验证码风控,请尝试点击下方按钮重新签到", allow_sending_without_reply=True, reply_markup=button
|
|
|
|
|
)
|
|
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id)
|
2022-10-30 11:37:57 +00:00
|
|
|
|
|
|
|
|
|
@handler(CallbackQueryHandler, pattern=r"^sign\|", block=False)
|
|
|
|
|
@restricts(restricts_time_of_groups=20, without_overlapping=True)
|
|
|
|
|
@error_callable
|
|
|
|
|
async def sign_gen_link(self, update: Update, _: CallbackContext) -> None:
|
|
|
|
|
callback_query = update.callback_query
|
|
|
|
|
user = callback_query.from_user
|
|
|
|
|
|
|
|
|
|
async def get_sign_callback(callback_query_data: str) -> Tuple[int, int]:
|
|
|
|
|
_data = callback_query_data.split("|")
|
|
|
|
|
_user_id = int(_data[1])
|
|
|
|
|
_uid = int(_data[2])
|
2022-10-30 13:35:53 +00:00
|
|
|
|
logger.debug(f"get_sign_callback 函数返回 user_id[{_user_id}] uid[{_uid}]")
|
2022-10-30 11:37:57 +00:00
|
|
|
|
return _user_id, _uid
|
|
|
|
|
|
|
|
|
|
user_id, uid = await get_sign_callback(callback_query.data)
|
|
|
|
|
if user.id != user_id:
|
2023-01-07 20:01:28 +00:00
|
|
|
|
await callback_query.answer(text="这不是你的按钮!\n" + config.notice.user_mismatch, show_alert=True)
|
2022-10-30 11:37:57 +00:00
|
|
|
|
return
|
2022-10-30 13:35:53 +00:00
|
|
|
|
_, challenge = await self.system.get_challenge(uid)
|
2022-10-30 11:37:57 +00:00
|
|
|
|
if not challenge:
|
|
|
|
|
await callback_query.answer(text="验证请求已经过期,请重新发起签到!", show_alert=True)
|
|
|
|
|
return
|
2022-11-28 08:38:49 +00:00
|
|
|
|
await callback_query.answer(url=create_deep_linked_url(bot.app.bot.username, "sign"))
|