From e9992969faa1eb05851e82f30081292375671d29 Mon Sep 17 00:00:00 2001 From: omg-xtao <100690902+omg-xtao@users.noreply.github.com> Date: Wed, 19 Jul 2023 20:28:19 +0800 Subject: [PATCH] :bug: Fix region class error --- plugins/system/errorhandler.py | 10 +++++++++- plugins/tools/challenge.py | 3 ++- plugins/tools/sign.py | 5 ++--- utils/patch/__init__.py | 4 ++-- utils/patch/simnet.py | 24 ++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 utils/patch/simnet.py diff --git a/plugins/system/errorhandler.py b/plugins/system/errorhandler.py index 0599aa1..1d90a91 100644 --- a/plugins/system/errorhandler.py +++ b/plugins/system/errorhandler.py @@ -12,6 +12,8 @@ from simnet.errors import ( InvalidCookies, TooManyRequests, CookieException, + TimedOut as SIMNetTimedOut, + SIMNetException, ) from telegram import ReplyKeyboardRemove, Update, InlineKeyboardMarkup, InlineKeyboardButton from telegram.constants import ParseMode @@ -106,10 +108,16 @@ class ErrorHandler(Plugin): @error_handler() async def process_genshin_exception(self, update: object, context: CallbackContext): - if not isinstance(context.error, SIMNetBadRequest) or not isinstance(update, Update): + if not isinstance(context.error, SIMNetException) or not isinstance(update, Update): return exc = context.error notice: Optional[str] = None + if isinstance(exc, SIMNetTimedOut): + notice = self.ERROR_MSG_PREFIX + " 服务器熟啦 ~ 请稍后再试" + self.create_notice_task(update, context, notice) + raise ApplicationHandlerStop + if not isinstance(exc, SIMNetBadRequest) or not isinstance(update, Update): + return if isinstance(exc, TooManyRequests): notice = self.ERROR_MSG_PREFIX + "Cookie 无效,请尝试重新绑定" elif isinstance(exc, InvalidCookies): diff --git a/plugins/tools/challenge.py b/plugins/tools/challenge.py index 41b0160..49a6b0a 100644 --- a/plugins/tools/challenge.py +++ b/plugins/tools/challenge.py @@ -3,6 +3,7 @@ from typing import Tuple, Optional from simnet import Region from simnet.errors import BadRequest as SIMNetBadRequest +from core.basemodel import RegionEnum from core.dependence.redisdb import RedisDB from core.plugin import Plugin from core.services.cookies import CookiesService @@ -92,7 +93,7 @@ class ChallengeSystem(Plugin): player = await self.players_service.get_player(user_id) if player is None: raise ChallengeSystemException("用户未找到") - if player.region != Region.CHINESE: + if player.region != RegionEnum.HYPERION: raise ChallengeSystemException("非法用户") cookie_model = await self.cookies_service.get(player.user_id, player.account_id, player.region) if cookie_model is None: diff --git a/plugins/tools/sign.py b/plugins/tools/sign.py index 06b9674..8ccb73f 100644 --- a/plugins/tools/sign.py +++ b/plugins/tools/sign.py @@ -5,10 +5,9 @@ import time from enum import Enum from typing import Optional, Tuple, List, TYPE_CHECKING -from aiohttp import ClientConnectorError from httpx import TimeoutException from simnet import Game -from simnet.errors import BadRequest as SimnetBadRequest, AlreadyClaimed, InvalidCookies +from simnet.errors import BadRequest as SimnetBadRequest, AlreadyClaimed, InvalidCookies, TimedOut as SimnetTimedOut from simnet.utils.player import recognize_genshin_server from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram.constants import ParseMode @@ -299,7 +298,7 @@ class SignSystem(Plugin): except SimnetBadRequest as exc: text = f"自动签到执行失败,API返回信息为 {str(exc)}" sign_db.status = SignStatusEnum.GENSHIN_EXCEPTION - except ClientConnectorError: + except SimnetTimedOut: text = "签到失败了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ " sign_db.status = SignStatusEnum.TIMEOUT_ERROR except NeedChallenge: diff --git a/utils/patch/__init__.py b/utils/patch/__init__.py index ad74e1c..2873e27 100644 --- a/utils/patch/__init__.py +++ b/utils/patch/__init__.py @@ -1,3 +1,3 @@ -from utils.patch import aiohttp +from utils.patch import aiohttp, simnet -__all__ = ["aiohttp"] +__all__ = ["aiohttp", "simnet"] diff --git a/utils/patch/simnet.py b/utils/patch/simnet.py new file mode 100644 index 0000000..b06256b --- /dev/null +++ b/utils/patch/simnet.py @@ -0,0 +1,24 @@ +import simnet +from httpx import AsyncClient, Timeout, Limits + +from core.config import config +from utils.patch.methods import patch, patchable + + +@patch(simnet.GenshinClient) +class GenshinClient: + @patchable + def __init__(self, *args, **kwargs): + self.old___init__(*args, **kwargs) + self.client: AsyncClient + if config.connect_timeout and config.read_timeout and config.write_timeout and config.pool_timeout: + self.client.timeout = Timeout( + connect=config.connect_timeout, + read=config.read_timeout, + write=config.write_timeout, + pool=config.pool_timeout, + ) + self.client.limits = Limits( + max_connections=config.connection_pool_size, + max_keepalive_connections=config.connection_pool_size, + )