From ffbb224644203ba2c6e8277f27a06294954468eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sun, 13 Nov 2022 13:45:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Update=20log=20output=20and=20ex?= =?UTF-8?q?ception=20handler=20in=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/genshin/verification.py | 37 +++++++++++++++++++++-------- plugins/system/start.py | 41 +++++++++++++++++++++++++-------- 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/plugins/genshin/verification.py b/plugins/genshin/verification.py index 0f59e835..c9fa36ff 100644 --- a/plugins/genshin/verification.py +++ b/plugins/genshin/verification.py @@ -12,10 +12,12 @@ from core.cookies.error import CookiesNotFoundError from core.plugin import Plugin, handler from core.user import UserService from core.user.error import UserNotFoundError +from modules.apihelper.error import ResponseException, APIHelperException from modules.apihelper.hyperion import Verification from utils.decorators.error import error_callable from utils.decorators.restricts import restricts from utils.helpers import get_genshin_client +from utils.log import logger class VerificationSystem: @@ -47,6 +49,7 @@ class VerificationPlugins(Plugin, BasePlugin): async def verify(self, update: Update, context: CallbackContext) -> None: user = update.effective_user message = update.effective_message + logger.info(f"用户 %s[%s] 发出verify命令", user.full_name, user.id) try: client = await get_genshin_client(user.id) if client.region != Region.CHINESE: @@ -63,10 +66,16 @@ class VerificationPlugins(Plugin, BasePlugin): validate = context.args[0] _, challenge = await self.system.get_challenge(client.uid) if challenge: - await verification.verify(challenge, validate) - await message.reply_text("验证成功") + try: + await verification.verify(challenge, validate) + logger.success(f"用户 %s[%s] 验证成功", user.full_name, user.id) + await message.reply_text("验证成功") + except ResponseException as exc: + logger.warning(f"用户 %s[%s] 验证失效 API返回 [%s]%s", user.full_name, user.id, exc.code, exc.message) + await message.reply_text(f"验证失败 错误信息为 [{exc.code}]{exc.message} 请稍后重试") else: - await message.reply_text("验证失效") + logger.warning(f"用户 %s[%s] 验证失效 请求已经过期", user.full_name, user.id) + await message.reply_text("验证失效 请求已经过期 请稍后重试") return try: await client.get_genshin_notes() @@ -76,14 +85,24 @@ class VerificationPlugins(Plugin, BasePlugin): else: await message.reply_text("账户正常,无需认证") return - data = await verification.create() + try: + data = await verification.create() + logger.success(f"用户 %s[%s] 创建验证成功", user.full_name, user.id) + except ResponseException as exc: + logger.warning(f"用户 %s[%s] 创建验证失效 API返回 [%s]%s 请稍后重试", user.full_name, user.id, exc.code, exc.message) + await message.reply_text(f"创建验证失败 错误信息为 [{exc.code}]{exc.message} 请稍后重试") + return challenge = data["challenge"] gt = data["gt"] - validate = await verification.ajax(referer="https://webstatic.mihoyo.com/", gt=gt, challenge=challenge) - if validate: - await verification.verify(challenge, validate) - await message.reply_text("验证成功") - return + try: + validate = await verification.ajax(referer="https://webstatic.mihoyo.com/", gt=gt, challenge=challenge) + if validate: + await verification.verify(challenge, validate) + logger.success(f"用户 %s[%s] 通过 ajax 验证", user.full_name, user.id) + await message.reply_text("验证成功") + return + except APIHelperException as exc: + logger.warning(f"用户 %s[%s] ajax 验证失效 错误信息为 %s", user.full_name, user.id, repr(exc)) await self.system.set_challenge(client.uid, gt, challenge) url = f"{config.pass_challenge_user_web}?username={context.bot.username}&command=verify>={gt}&challenge={challenge}&uid={client.uid}" button = InlineKeyboardMarkup([[InlineKeyboardButton("验证", url=url)]]) diff --git a/plugins/system/start.py b/plugins/system/start.py index cef02b63..a29b18df 100644 --- a/plugins/system/start.py +++ b/plugins/system/start.py @@ -13,6 +13,7 @@ from core.cookies.error import CookiesNotFoundError from core.plugin import handler, Plugin from core.user import UserService from core.user.error import UserNotFoundError +from modules.apihelper.error import ResponseException, APIHelperException from modules.apihelper.hyperion import Verification from plugins.genshin.sign import SignSystem, NeedChallenge from plugins.genshin.verification import VerificationSystem @@ -53,20 +54,25 @@ class StartPlugin(Plugin): f"{escape_markdown('发送 /setuid 或 /setcookie 命令进入绑定账号流程')}" ) elif args[0] == "verify_verification": + logger.info(f"用户 %s[%s] 通过start命令 获取认证信息", user.full_name, user.id) await self.process_validate(message, user, bot_username=context.bot.username) elif args[0] == "sign": + logger.info(f"用户 %s[%s] 通过start命令 获取签到信息", user.full_name, user.id) await self.gen_sign_button(message, user) elif args[0].startswith("challenge_"): _data = args[0].split("_") _command = _data[1] _challenge = _data[2] if _command == "sign": + logger.info(f"用户 %s[%s] 通过start命令 进入签到流程", user.full_name, user.id) await self.process_sign_validate(message, user, _challenge) elif _command == "verify": + logger.info(f"用户 %s[%s] 通过start命令 进入认证流程", user.full_name, user.id) await self.process_validate(message, user, validate=_challenge) else: await message.reply_html(f"你好 {user.mention_html()} !我是派蒙 !\n请点击 /{args[0]} 命令进入对应流程") return + logger.info(f"用户 %s[%s] 发出start命令", user.full_name, user.id) await message.reply_markdown_v2(f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是派蒙 !')}") @staticmethod @@ -142,19 +148,36 @@ class StartPlugin(Plugin): if validate: _, challenge = await self.verification_system.get_challenge(client.uid) if challenge: - await verification.verify(challenge, validate) - await message.reply_text("验证成功") + try: + await verification.verify(challenge, validate) + logger.success(f"用户 %s[%s] 验证成功", user.full_name, user.id) + await message.reply_text("验证成功") + except ResponseException as exc: + logger.warning(f"用户 %s[%s] 验证失效 API返回 [%s]%s", user.full_name, user.id, exc.code, exc.message) + await message.reply_text(f"验证失败 错误信息为 [{exc.code}]{exc.message} 请稍后重试") else: - await message.reply_text("验证失效") + logger.warning(f"用户 %s[%s] 验证失效 请求已经过期", user.full_name, user.id) + await message.reply_text("验证失效 请求已经过期 请稍后重试") + return if bot_username: - data = await verification.create() + try: + data = await verification.create() + logger.success(f"用户 %s[%s] 创建验证成功", user.full_name, user.id) + except ResponseException as exc: + logger.warning(f"用户 %s[%s] 创建验证失效 API返回 [%s]%s", user.full_name, user.id, exc.code, exc.message) + await message.reply_text(f"创建验证失败 错误信息为 [{exc.code}]{exc.message} 请稍后重试") + return challenge = data["challenge"] gt = data["gt"] - validate = await verification.ajax(referer="https://webstatic.mihoyo.com/", gt=gt, challenge=challenge) - if validate: - await verification.verify(challenge, validate) - await message.reply_text("验证成功") - return + try: + validate = await verification.ajax(referer="https://webstatic.mihoyo.com/", gt=gt, challenge=challenge) + if validate: + await verification.verify(challenge, validate) + logger.success(f"用户 %s[%s] 通过 ajax 验证", user.full_name, user.id) + await message.reply_text("验证成功") + return + except APIHelperException as exc: + logger.warning(f"用户 %s[%s] ajax 验证失效 错误信息为 %s", user.full_name, user.id, repr(exc)) await self.sign_system.set_challenge(client.uid, gt, challenge) url = f"{config.pass_challenge_user_web}?username={bot_username}&command=verify>={gt}&challenge={challenge}&uid={client.uid}" button = InlineKeyboardMarkup([[InlineKeyboardButton("验证", url=url)]])