diff --git a/core/services/cookies/services.py b/core/services/cookies/services.py index a80c74fb..91192484 100644 --- a/core/services/cookies/services.py +++ b/core/services/cookies/services.py @@ -11,7 +11,7 @@ from gram_core.services.cookies.services import ( ) from simnet import GenshinClient, Region, Game -from simnet.errors import InvalidCookies, TooManyRequests, BadRequest as SimnetBadRequest +from simnet.errors import InvalidCookies, TooManyRequests, BadRequest as SimnetBadRequest, NeedChallenge from utils.log import logger @@ -74,15 +74,16 @@ class PublicCookiesService(BaseService, BasePublicCookiesService): await self._repository.update(cookies) await self._cache.delete_public_cookies(cookies.user_id, region) raise NeedContinue + except NeedChallenge: + logger.warning("用户 [%s] 触发验证", public_id) + await self.set_device_valid(client.account_id, False) + await self._cache.delete_public_cookies(cookies.user_id, region) + raise NeedContinue except SimnetBadRequest as exc: if "invalid content type" in exc.message: raise exc - if exc.ret_code == 1034: - logger.warning("用户 [%s] 触发验证", public_id) - await self.set_device_valid(client.account_id, False) - else: - logger.warning("用户 [%s] 获取账号信息发生错误,错误信息为", public_id) - logger.exception(exc) + logger.warning("用户 [%s] 获取账号信息发生错误,错误信息为", public_id) + logger.exception(exc) await self._cache.delete_public_cookies(cookies.user_id, region) raise NeedContinue except RuntimeError as exc: diff --git a/plugins/tools/challenge.py b/plugins/tools/challenge.py index ab311c25..15d51e7b 100644 --- a/plugins/tools/challenge.py +++ b/plugins/tools/challenge.py @@ -2,7 +2,7 @@ from typing import Tuple, Optional from simnet import Region from simnet.client.cookies import Cookies -from simnet.errors import BadRequest as SIMNetBadRequest +from simnet.errors import NeedChallenge from core.basemodel import RegionEnum from core.dependence.redisdb import RedisDB @@ -62,9 +62,8 @@ class ChallengeSystem(Plugin): if need_verify: try: await client.get_genshin_notes() - except SIMNetBadRequest as exc: - if exc.retcode != 1034: - raise exc + except NeedChallenge: + pass else: raise ChallengeSystemException("账户正常,无需验证") finally: diff --git a/plugins/tools/genshin.py b/plugins/tools/genshin.py index 292c031d..6b2aec28 100644 --- a/plugins/tools/genshin.py +++ b/plugins/tools/genshin.py @@ -7,7 +7,7 @@ from typing import TYPE_CHECKING, Union from pydantic import ValidationError from simnet import GenshinClient, Region -from simnet.errors import BadRequest as SimnetBadRequest, InvalidCookies, NetworkError, CookieException +from simnet.errors import BadRequest as SimnetBadRequest, InvalidCookies, NetworkError, CookieException, NeedChallenge from simnet.models.genshin.calculator import CalculatorCharacterDetails from simnet.models.genshin.chronicle.characters import Character from simnet.utils.player import recognize_game_biz @@ -294,8 +294,8 @@ class GenshinHelper(Plugin): if refresh: raise CookieException(message="The cookie has been refreshed.") from exc raise exc - except SimnetBadRequest as exc: - if exc.ret_code == 1034 and devices is not None: + except NeedChallenge as exc: + if devices is not None: devices.is_valid = False await self.devices_service.update(devices) raise exc @@ -372,10 +372,9 @@ class GenshinHelper(Plugin): ) as client: try: yield client - except SimnetBadRequest as exc: - if exc.ret_code == 1034: - await self.public_cookies_service.undo(user_id) - await self.public_cookies_service.set_device_valid(client.account_id, False) + except NeedChallenge as exc: + await self.public_cookies_service.undo(user_id) + await self.public_cookies_service.set_device_valid(client.account_id, False) raise exc @asynccontextmanager @@ -396,7 +395,5 @@ class GenshinHelper(Plugin): try: client.public = True yield client - except SimnetBadRequest as exc: - if exc.ret_code == 1034: - raise CookiesNotFoundError(user_id) from exc - raise exc + except NeedChallenge as exc: + raise CookiesNotFoundError(user_id) from exc