🐛 avatar 修复错误

This commit is contained in:
omg-xtao 2022-10-17 14:27:37 +08:00 committed by GitHub
parent 5b09ccdf76
commit adec83108a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 9 deletions

View File

@ -69,11 +69,10 @@ class AvatarListPlugin(Plugin, BasePlugin):
try: try:
detail = await client.get_character_details(character) detail = await client.get_character_details(character)
except Exception as e: # pylint: disable=W0703 except Exception as e: # pylint: disable=W0703
if character.name == "旅行者": if character.name != "旅行者":
raise e
logger.debug(f"解析旅行者数据时遇到了错误:{e}") logger.debug(f"解析旅行者数据时遇到了错误:{e}")
continue continue
else:
raise e
if character.id == 10000005: # 针对男草主 if character.id == 10000005: # 针对男草主
talents = [] talents = []
for talent in detail.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)] rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[str(cid)]
return namecard, avatar, nickname, rarity 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.command("avatars", filters.Regex(r"^/avatars\s*(?:(\d+)|(all))?$"))
@handler.message(filters.Regex(r"^(全部)?练度统计$")) @handler.message(filters.Regex(r"^(全部)?练度统计$"))
@restricts(30) @restricts(30)
@ -147,9 +157,9 @@ class AvatarListPlugin(Plugin, BasePlugin):
user = update.effective_user user = update.effective_user
message = update.effective_message 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}) 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 return
raise e raise e
try:
namecard, avatar, nickname, rarity = await self.get_final_data(client, characters, update) 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 = { render_data = {
"uid": client.uid, # 玩家uid "uid": client.uid, # 玩家uid

View File

@ -1,3 +1,3 @@
from utils.patch import aiohttp from utils.patch import aiohttp, genshin
__all__ = ["aiohttp"] __all__ = ["aiohttp", "genshin"]

41
utils/patch/genshin.py Normal file
View File

@ -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)