2023-09-15 15:47:57 +00:00
|
|
|
|
from typing import Optional, TYPE_CHECKING
|
2022-11-12 14:23:12 +00:00
|
|
|
|
|
2023-09-30 07:26:17 +00:00
|
|
|
|
from simnet.errors import RedemptionInvalid, RegionNotSupported, RedemptionClaimed, RedemptionCooldown
|
2022-11-20 11:32:56 +00:00
|
|
|
|
from telegram import Update, ReplyKeyboardRemove, Message, User, WebAppInfo, ReplyKeyboardMarkup, KeyboardButton
|
2022-10-30 08:46:07 +00:00
|
|
|
|
from telegram.constants import ChatAction
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from telegram.ext import CallbackContext, CommandHandler
|
2022-07-26 10:07:31 +00:00
|
|
|
|
from telegram.helpers import escape_markdown
|
|
|
|
|
|
2022-11-12 14:23:12 +00:00
|
|
|
|
from core.config import config
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.plugin import handler, Plugin
|
2023-07-19 05:52:30 +00:00
|
|
|
|
from core.services.players import PlayersService
|
2023-03-14 01:27:22 +00:00
|
|
|
|
from plugins.tools.challenge import ChallengeSystem, ChallengeSystemException
|
|
|
|
|
from plugins.tools.genshin import PlayerNotFoundError, CookiesNotFoundError, GenshinHelper
|
|
|
|
|
from plugins.tools.sign import SignSystem, NeedChallenge
|
2022-10-30 13:35:53 +00:00
|
|
|
|
from utils.log import logger
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
2023-09-15 15:47:57 +00:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from simnet import StarRailClient
|
|
|
|
|
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
class StartPlugin(Plugin):
|
2023-07-19 05:52:30 +00:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
player: PlayersService,
|
|
|
|
|
sign_system: SignSystem,
|
|
|
|
|
challenge_system: ChallengeSystem,
|
|
|
|
|
genshin_helper: GenshinHelper,
|
|
|
|
|
):
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.challenge_system = challenge_system
|
|
|
|
|
self.sign_system = sign_system
|
|
|
|
|
self.genshin_helper = genshin_helper
|
2023-07-19 05:52:30 +00:00
|
|
|
|
self.players_service = player
|
2022-10-30 13:35:53 +00:00
|
|
|
|
|
2022-11-12 17:56:38 +00:00
|
|
|
|
@handler.command("start", block=False)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
async def start(self, update: Update, context: CallbackContext) -> None:
|
|
|
|
|
user = update.effective_user
|
|
|
|
|
message = update.effective_message
|
|
|
|
|
args = context.args
|
2022-10-12 09:35:59 +00:00
|
|
|
|
if args is not None and len(args) >= 1:
|
|
|
|
|
if args[0] == "inline_message":
|
|
|
|
|
await message.reply_markdown_v2(
|
2023-04-26 08:48:05 +00:00
|
|
|
|
f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是彦卿 !')}\n"
|
2022-10-12 09:35:59 +00:00
|
|
|
|
f"{escape_markdown('发送 /help 命令即可查看命令帮助')}"
|
|
|
|
|
)
|
|
|
|
|
elif args[0] == "set_cookie":
|
|
|
|
|
await message.reply_markdown_v2(
|
2023-04-26 08:48:05 +00:00
|
|
|
|
f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是彦卿 !')}\n"
|
2022-10-12 09:35:59 +00:00
|
|
|
|
f"{escape_markdown('发送 /setcookie 命令进入绑定账号流程')}"
|
|
|
|
|
)
|
|
|
|
|
elif args[0] == "set_uid":
|
|
|
|
|
await message.reply_markdown_v2(
|
2023-04-26 08:48:05 +00:00
|
|
|
|
f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是彦卿 !')}\n"
|
2022-10-12 09:35:59 +00:00
|
|
|
|
f"{escape_markdown('发送 /setuid 或 /setcookie 命令进入绑定账号流程')}"
|
|
|
|
|
)
|
2022-11-12 12:59:42 +00:00
|
|
|
|
elif args[0] == "verify_verification":
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.info("用户 %s[%s] 通过start命令 获取认证信息", user.full_name, user.id)
|
2022-11-12 14:23:12 +00:00
|
|
|
|
await self.process_validate(message, user, bot_username=context.bot.username)
|
2022-10-30 11:37:57 +00:00
|
|
|
|
elif args[0] == "sign":
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.info("用户 %s[%s] 通过start命令 获取签到信息", user.full_name, user.id)
|
2023-03-15 04:34:46 +00:00
|
|
|
|
await self.get_sign_button(message, user, bot_username=context.bot.username)
|
2022-10-30 08:46:07 +00:00
|
|
|
|
elif args[0].startswith("challenge_"):
|
2022-11-12 12:59:42 +00:00
|
|
|
|
_data = args[0].split("_")
|
|
|
|
|
_command = _data[1]
|
|
|
|
|
_challenge = _data[2]
|
|
|
|
|
if _command == "sign":
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.info("用户 %s[%s] 通过start命令 进入签到流程", user.full_name, user.id)
|
2022-11-12 12:59:42 +00:00
|
|
|
|
await self.process_sign_validate(message, user, _challenge)
|
2023-09-15 15:47:57 +00:00
|
|
|
|
elif args[0].startswith("redeem_"):
|
|
|
|
|
_code = args[0].split("_")[1]
|
|
|
|
|
logger.info("用户 %s[%s] 通过start命令 进入兑换码兑换流程 code[%s]", user.full_name, user.id, _code)
|
|
|
|
|
await self.process_redeem(message, user, _code)
|
2022-10-22 13:54:04 +00:00
|
|
|
|
else:
|
2023-04-26 08:48:05 +00:00
|
|
|
|
await message.reply_html(f"你好 {user.mention_html()} !我是彦卿 !\n请点击 /{args[0]} 命令进入对应流程")
|
2022-10-22 13:54:04 +00:00
|
|
|
|
return
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.info("用户 %s[%s] 发出start命令", user.full_name, user.id)
|
2023-04-26 08:48:05 +00:00
|
|
|
|
await message.reply_markdown_v2(f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是彦卿 !')}")
|
2022-09-08 01:08:37 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def unknown_command(update: Update, _: CallbackContext) -> None:
|
|
|
|
|
await update.effective_message.reply_text("前面的区域,以后再来探索吧!")
|
|
|
|
|
|
|
|
|
|
@handler(CommandHandler, command="ping", block=False)
|
|
|
|
|
async def ping(self, update: Update, _: CallbackContext) -> None:
|
|
|
|
|
await update.effective_message.reply_text("online! ヾ(✿゚▽゚)ノ")
|
|
|
|
|
|
|
|
|
|
@handler(CommandHandler, command="reply_keyboard_remove", block=False)
|
|
|
|
|
async def reply_keyboard_remove(self, update: Update, _: CallbackContext) -> None:
|
|
|
|
|
await update.message.reply_text("移除远程键盘成功", reply_markup=ReplyKeyboardRemove())
|
2022-10-30 13:35:53 +00:00
|
|
|
|
|
|
|
|
|
async def process_sign_validate(self, message: Message, user: User, validate: str):
|
|
|
|
|
try:
|
2023-07-19 05:52:30 +00:00
|
|
|
|
async with self.genshin_helper.genshin(user.id) as client:
|
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
|
|
|
|
_, challenge = await self.sign_system.get_challenge(client.player_id)
|
|
|
|
|
if not challenge:
|
|
|
|
|
await message.reply_text("验证请求已过期。", allow_sending_without_reply=True)
|
|
|
|
|
return
|
|
|
|
|
sign_text = await self.sign_system.start_sign(client, challenge=challenge, validate=validate)
|
|
|
|
|
await message.reply_text(sign_text, allow_sending_without_reply=True)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
except (PlayerNotFoundError, CookiesNotFoundError):
|
2022-11-12 12:59:42 +00:00
|
|
|
|
logger.warning("用户 %s[%s] 账号信息未找到", user.full_name, user.id)
|
2022-10-30 13:35:53 +00:00
|
|
|
|
except NeedChallenge:
|
|
|
|
|
await message.reply_text("回调错误,请重新签到", allow_sending_without_reply=True)
|
2022-11-12 12:59:42 +00:00
|
|
|
|
|
2022-11-20 11:32:56 +00:00
|
|
|
|
async def process_validate(self, message: Message, user: User, bot_username: Optional[str] = None):
|
2022-11-17 02:16:45 +00:00
|
|
|
|
await message.reply_text(
|
|
|
|
|
"由于官方对第三方工具限制以及账户安全的考虑,频繁使用第三方工具会导致账号被风控并要求用过验证才能进行访问。\n"
|
|
|
|
|
"如果出现频繁验证请求,建议暂停使用本Bot在内的第三方工具查询功能。\n"
|
|
|
|
|
"在暂停使用期间依然出现频繁认证,建议修改密码以保护账号安全。"
|
|
|
|
|
)
|
2022-11-20 11:32:56 +00:00
|
|
|
|
try:
|
2023-03-14 01:27:22 +00:00
|
|
|
|
uid, gt, challenge = await self.challenge_system.create_challenge(user.id, ajax=True)
|
|
|
|
|
except ChallengeSystemException as exc:
|
|
|
|
|
await message.reply_text(exc.message)
|
2022-11-13 05:45:01 +00:00
|
|
|
|
return
|
2023-03-14 01:27:22 +00:00
|
|
|
|
if gt == "ajax":
|
|
|
|
|
await message.reply_text("验证成功")
|
|
|
|
|
return
|
|
|
|
|
url = (
|
|
|
|
|
f"{config.pass_challenge_user_web}/webapp?"
|
2023-06-03 04:20:18 +00:00
|
|
|
|
f"gt={gt}&username={bot_username}&command=verify&challenge={challenge}&uid={uid}"
|
2023-03-14 01:27:22 +00:00
|
|
|
|
)
|
2022-11-20 11:32:56 +00:00
|
|
|
|
await message.reply_text(
|
|
|
|
|
"请尽快在10秒内完成手动验证\n或发送 /web_cancel 取消操作",
|
|
|
|
|
reply_markup=ReplyKeyboardMarkup.from_button(
|
|
|
|
|
KeyboardButton(
|
|
|
|
|
text="点我手动验证",
|
|
|
|
|
web_app=WebAppInfo(url=url),
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
2023-03-15 04:34:46 +00:00
|
|
|
|
async def get_sign_button(self, message: Message, user: User, bot_username: str):
|
2023-07-19 05:52:30 +00:00
|
|
|
|
player = await self.players_service.get_player(user.id)
|
|
|
|
|
if player is None:
|
2023-03-14 01:27:22 +00:00
|
|
|
|
logger.warning("用户 %s[%s] 账号信息未找到", user.full_name, user.id)
|
2023-07-19 05:52:30 +00:00
|
|
|
|
return
|
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
|
|
|
|
button = await self.sign_system.get_challenge_button(bot_username, player.player_id, user.id, callback=False)
|
|
|
|
|
if not button:
|
|
|
|
|
await message.reply_text("验证请求已过期。", allow_sending_without_reply=True)
|
|
|
|
|
return
|
|
|
|
|
await message.reply_text("请尽快点击下方按钮进行验证。", allow_sending_without_reply=True, reply_markup=button)
|
2023-09-15 15:47:57 +00:00
|
|
|
|
|
|
|
|
|
async def process_redeem(self, message: Message, user: User, code: str):
|
|
|
|
|
try:
|
|
|
|
|
if not code:
|
|
|
|
|
raise RedemptionInvalid
|
|
|
|
|
async with self.genshin_helper.genshin(user.id) as client:
|
|
|
|
|
client: "StarRailClient"
|
|
|
|
|
await client.redeem_code_by_hoyolab(code)
|
|
|
|
|
msg = "兑换码兑换成功。"
|
|
|
|
|
except RegionNotSupported:
|
|
|
|
|
msg = "此服务器暂不支持进行兑换哦~"
|
|
|
|
|
except RedemptionInvalid:
|
|
|
|
|
msg = "兑换码格式不正确,请确认。"
|
|
|
|
|
except RedemptionClaimed:
|
|
|
|
|
msg = "此兑换码已经兑换过了。"
|
2023-09-30 07:26:17 +00:00
|
|
|
|
except RedemptionCooldown as e:
|
|
|
|
|
msg = e.message
|
2023-09-15 15:47:57 +00:00
|
|
|
|
await message.reply_text(msg)
|