From adec83108a0760a57737867ffed2abb41b1d5c9f Mon Sep 17 00:00:00 2001 From: omg-xtao <100690902+omg-xtao@users.noreply.github.com> Date: Mon, 17 Oct 2022 14:27:37 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20avatar=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/genshin/avatar_list.py | 28 +++++++++++++++++------ utils/patch/__init__.py | 4 ++-- utils/patch/genshin.py | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 utils/patch/genshin.py diff --git a/plugins/genshin/avatar_list.py b/plugins/genshin/avatar_list.py index 02a67a96..62ad20d6 100644 --- a/plugins/genshin/avatar_list.py +++ b/plugins/genshin/avatar_list.py @@ -69,11 +69,10 @@ class AvatarListPlugin(Plugin, BasePlugin): try: detail = await client.get_character_details(character) except Exception as e: # pylint: disable=W0703 - if character.name == "旅行者": - logger.debug(f"解析旅行者数据时遇到了错误:{e}") - continue - else: + if character.name != "旅行者": raise e + logger.debug(f"解析旅行者数据时遇到了错误:{e}") + continue if character.id == 10000005: # 针对男草主 talents = [] for talent in detail.talents: @@ -139,6 +138,17 @@ class AvatarListPlugin(Plugin, BasePlugin): rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[str(cid)] return namecard, avatar, nickname, rarity + async def get_default_final_data(self, characters: Sequence[Character], update: Update): + nickname = update.effective_user.full_name + rarity = 5 + # 须弥·正明 + namecard = (await self.assets_service.namecard(210132).navbar()).as_uri() + if traveller := next(filter(lambda x: x.id in [10000005, 10000007], characters), None): + avatar = (await self.assets_service.avatar(traveller.id).icon()).as_uri() + else: + avatar = (await self.assets_service.avatar(10000005).icon()).as_uri() + return namecard, avatar, nickname, rarity + @handler.command("avatars", filters.Regex(r"^/avatars\s*(?:(\d+)|(all))?$")) @handler.message(filters.Regex(r"^(全部)?练度统计$")) @restricts(30) @@ -147,9 +157,9 @@ class AvatarListPlugin(Plugin, BasePlugin): user = update.effective_user message = update.effective_message - args = context.match + args = [i.lower() for i in context.match.groups() if i] - all_avatars = any(["all" in args.groups(), "全部" in args.groups()]) # 是否发送全部角色 + all_avatars = any(["all" in args, "全部" in args]) # 是否发送全部角色 logger.info(f"用户 {user.full_name}[{user.id}] [bold]练度统计[/bold]: all={all_avatars}", extra={"markup": True}) @@ -174,7 +184,11 @@ class AvatarListPlugin(Plugin, BasePlugin): return raise e - namecard, avatar, nickname, rarity = await self.get_final_data(client, characters, update) + try: + namecard, avatar, nickname, rarity = await self.get_final_data(client, characters, update) + except Exception as e: + logger.debug(f"卡片信息请求失败: {e}") + namecard, avatar, nickname, rarity = await self.get_default_final_data(characters, update) render_data = { "uid": client.uid, # 玩家uid diff --git a/utils/patch/__init__.py b/utils/patch/__init__.py index ad74e1cc..b10678a2 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, genshin -__all__ = ["aiohttp"] +__all__ = ["aiohttp", "genshin"] diff --git a/utils/patch/genshin.py b/utils/patch/genshin.py new file mode 100644 index 00000000..ddfb7689 --- /dev/null +++ b/utils/patch/genshin.py @@ -0,0 +1,41 @@ +import typing + +import genshin # pylint: disable=W0406 + +from utils.patch.methods import patch, patchable + + +@patch(genshin.client.components.calculator.CalculatorClient) # noqa +class CalculatorClient: + @patchable + async def get_character_details( + self, + character: genshin.types.IDOr[genshin.models.genshin.Character], + *, + uid: typing.Optional[int] = None, + lang: typing.Optional[str] = None, + ): + uid = uid or await self._get_uid(genshin.types.Game.GENSHIN) + + data = await self.request_calculator( + "sync/avatar/detail", + method="GET", + lang=lang, + params=dict( + avatar_id=int(character), + uid=uid, + region=genshin.utility.recognize_genshin_server(uid), + ), + ) + if data.get("weapon") is None: + weapon = { + "id": character.weapon.id, + "name": character.weapon.name, + "icon": character.weapon.icon, + "weapon_cat_id": character.weapon.type, + "weapon_level": character.weapon.rarity, + "max_level": 90, + "level_current": character.weapon.level, + } + data["weapon"] = weapon + return genshin.models.genshin.CalculatorCharacterDetails(**data)