From 9e23c1d20d9a2a37d1d93ad093805bdd6f8878d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Fri, 28 Apr 2023 14:34:54 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20the=20`KeyError`=20excepti?= =?UTF-8?q?on=20caused=20by=20the=20character=20rarity=20not=20found?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/genshin/avatar_list.py | 22 ++++++++++++++-------- plugins/tools/player_info.py | 5 ++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/plugins/genshin/avatar_list.py b/plugins/genshin/avatar_list.py index ba6c334a..f8464e08 100644 --- a/plugins/genshin/avatar_list.py +++ b/plugins/genshin/avatar_list.py @@ -167,14 +167,20 @@ class AvatarListPlugin(Plugin): name_card: Optional[str] = None avatar: Optional[str] = None rarity: int = 5 - if player_info is not None: - if player_info.nickname is not None: - nickname = player_info.nickname - if player_info.name_card is not None: - name_card = (await self.assets_service.namecard(int(player_info.name_card)).navbar()).as_uri() - if player_info.hand_image is not None: - avatar = (await self.assets_service.avatar(player_info.hand_image).icon()).as_uri() - rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[str(player_info.hand_image)] + try: + if player_info is not None: + if player_info.nickname is not None: + nickname = player_info.nickname + if player_info.name_card is not None: + name_card = (await self.assets_service.namecard(int(player_info.name_card)).navbar()).as_uri() + if player_info.hand_image is not None: + avatar = (await self.assets_service.avatar(player_info.hand_image).icon()).as_uri() + try: + rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[player_info.hand_image] + except KeyError: + logger.warning("未找到角色 %s 的星级", player_info.hand_image) + except Exception as exc: # pylint: disable=W0703 + logger.error("卡片信息请求失败 %s", str(exc)) if name_card is not None: # 默认 name_card = (await self.assets_service.namecard(210001).navbar()).as_uri() return name_card, avatar, nickname, rarity diff --git a/plugins/tools/player_info.py b/plugins/tools/player_info.py index de92efb5..90485d3b 100644 --- a/plugins/tools/player_info.py +++ b/plugins/tools/player_info.py @@ -36,7 +36,10 @@ class PlayerInfoSystem(Plugin): name_card = (await self.assets_service.namecard(int(player_info.name_card)).navbar()).as_uri() if player_info.hand_image is not None: avatar = (await self.assets_service.avatar(player_info.hand_image).icon()).as_uri() - rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[player_info.hand_image] + try: + rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[player_info.hand_image] + except KeyError: + logger.warning("未找到角色 %s 的等级", player_info.hand_image) except Exception as exc: # pylint: disable=W0703 logger.error("卡片信息请求失败 %s", str(exc)) if name_card is not None: # 默认