🎨 daily_material: optimize get skills level code

Co-authored-by: 洛水居室 <luoshuijs@outlook.com>
This commit is contained in:
SiHuaN 2022-10-29 11:53:08 +08:00 committed by 洛水居室
parent 31d71fd6a1
commit 755c224ca0
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC

View File

@ -129,22 +129,25 @@ class DailyMaterial(Plugin, BasePlugin):
@staticmethod @staticmethod
async def _get_skills_data(client: Client, character_id: int) -> Optional[List[int]]: async def _get_skills_data(client: Client, character_id: int) -> Optional[List[int]]:
detail = None """获取角色技能的数据"""
for _ in range(5): for _ in range(5):
try: try:
detail = await client.get_character_details(character_id) detail = await client.get_character_details(character_id)
except Exception as e: # pylint: disable=W0703 except Exception as e: # pylint: disable=W0703
logger.warning(f"daily_material 解析角色 id 为 [bold]{character_id}[/]的数据时遇到了错误:{e}", extra={"markup": True}) if isinstance(e, GenshinException):
if isinstance(e, GenshinException) and "Too Many Requests" in e.msg: # 如果是 Too Many Requests 异常,则等待一段时间后重试
if "Too Many Requests" in e.msg:
await asyncio.sleep(0.2) await asyncio.sleep(0.2)
continue continue
# 输入数据不可能是旅行者 # 如果是其他异常,则直接抛出
# if character.name != "旅行者": raise e
# raise e
# logger.debug(f"解析旅行者数据时遇到了错误:{e}")
else: else:
break break
if not detail: else:
# 如果重试了5次都失败了则直接返回 None
logger.warning(
f"daily_material 解析角色 id 为 [bold]{character_id}[/]的数据时遇到了 Too Many Requests 错误", extra={"markup": True}
)
return None return None
# 不用针对旅行者、草主进行特殊处理,因为输入数据不会有旅行者。 # 不用针对旅行者、草主进行特殊处理,因为输入数据不会有旅行者。
# 不用计算命座加成因为这个是展示天赋升级情况10 级为最高。计算命座会引起混淆。 # 不用计算命座加成因为这个是展示天赋升级情况10 级为最高。计算命座会引起混淆。
@ -153,7 +156,6 @@ class DailyMaterial(Plugin, BasePlugin):
async def _get_data_from_user(self, user: User) -> Tuple[Optional[Client], Dict[str, List[Any]]]: async def _get_data_from_user(self, user: User) -> Tuple[Optional[Client], Dict[str, List[Any]]]:
"""获取已经绑定的账号的角色、武器信息""" """获取已经绑定的账号的角色、武器信息"""
client = None
user_data = {"avatar": [], "weapon": []} user_data = {"avatar": [], "weapon": []}
try: try:
logger.debug("尝试获取已绑定的原神账号") logger.debug("尝试获取已绑定的原神账号")
@ -195,7 +197,11 @@ class DailyMaterial(Plugin, BasePlugin):
logger.info(f"未查询到用户({user.full_name} {user.id}) 所绑定的账号信息") logger.info(f"未查询到用户({user.full_name} {user.id}) 所绑定的账号信息")
except InvalidCookies: except InvalidCookies:
logger.info(f"用户({user.full_name} {user.id}) 所绑定的账号信息已失效") logger.info(f"用户({user.full_name} {user.id}) 所绑定的账号信息已失效")
else:
# 没有异常返回数据
return client, user_data return client, user_data
# 有上述异常的, client 会返回 None
return None, user_data
@handler.command("daily_material", block=False) @handler.command("daily_material", block=False)
@restricts(restricts_time_of_groups=20, without_overlapping=True) @restricts(restricts_time_of_groups=20, without_overlapping=True)
@ -253,6 +259,9 @@ class DailyMaterial(Plugin, BasePlugin):
await message.reply_chat_action(ChatAction.TYPING) await message.reply_chat_action(ChatAction.TYPING)
render_data = RenderData(title=title, time=time, uid=client.uid if client else client) render_data = RenderData(title=title, time=time, uid=client.uid if client else client)
# 如果 client 为 None 就没必要进技能处理 因为已经触发 InvalidCookies 等异常
if client:
for type_ in ["avatar", "weapon"]: for type_ in ["avatar", "weapon"]:
areas = [] areas = []
for area_data in local_data[type_]: # 遍历每个区域的信息:蒙德、璃月、稻妻、须弥 for area_data in local_data[type_]: # 遍历每个区域的信息:蒙德、璃月、稻妻、须弥