From 52de48fedfc3fdc14524b07a697190cbf57fa365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sat, 8 Oct 2022 21:35:46 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=E8=A7=A3=E5=86=B3=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=9F=A5=E8=AF=A2=E6=97=B6UID=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/genshin/userstats.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/plugins/genshin/userstats.py b/plugins/genshin/userstats.py index efbb085..8415e76 100644 --- a/plugins/genshin/userstats.py +++ b/plugins/genshin/userstats.py @@ -1,7 +1,7 @@ import secrets from typing import Optional -from genshin import GenshinException, Client +from genshin import Client from genshin.models import GenshinUserStats from telegram import Update from telegram.constants import ChatAction @@ -42,23 +42,20 @@ class UserStatsPlugins(Plugin, BasePlugin): user = update.effective_user message = update.effective_message logger.info(f"用户 {user.full_name}[{user.id}] 查询游戏用户命令请求") - uid: int = -1 + uid: Optional[int] = None try: args = context.args if args is not None and len(args) >= 1: uid = int(args[0]) except ValueError as exc: - logger.error("获取 uid 发生错误! 错误信息为") - logger.exception(exc) + logger.warning(f"获取 uid 发生错误! 错误信息为 {repr(exc)}") await message.reply_text("输入错误") return ConversationHandler.END try: try: client = await get_genshin_client(user.id) except CookiesNotFoundError: - client, _uid = await get_public_genshin_client(user.id) - if uid == -1: - uid = _uid + client, uid = await get_public_genshin_client(user.id) png_data = await self.render(client, uid) except UserNotFoundError: reply_message = await message.reply_text("未查询到账号信息,请先私聊派蒙绑定账号") @@ -84,15 +81,12 @@ class UserStatsPlugins(Plugin, BasePlugin): png_data, filename=f"{client.uid}.png", allow_sending_without_reply=True ) - async def render(self, client: Client, uid: int = -1) -> bytes: - if uid == -1 and client.uid: + async def render(self, client: Client, uid: Optional[int] = None) -> bytes: + if uid is None: uid = client.uid - try: - user_info = await client.get_genshin_user(uid) - logger.debug(user_info) - except GenshinException as exc: - raise exc + user_info = await client.get_genshin_user(uid) + logger.debug(user_info) # 因为需要替换线上图片地址为本地地址,先克隆数据,避免修改原数据 user_info = user_info.copy(deep=True)