🐛 Fix GenshinException exception and blank return when no cookie is set in daily_material

修复了 client 为 None 时返回空白的信息
修复了未授权使用养成计算器引发的异常导致无法获取信息的问题
This commit is contained in:
SiHuan 2022-11-01 16:28:59 +08:00 committed by 洛水居室
parent 11be3f232c
commit dea8a4128c
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC

View File

@ -260,59 +260,69 @@ 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 等异常 calculator_sync: bool = True # 默认养成计算器同步为开启
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_]: # 遍历每个区域的信息:蒙德、璃月、稻妻、须弥 items = []
items = [] for id_ in area_data["items"]: # 遍历所有该区域下当天weekday可以培养的角色、武器
for id_ in area_data["items"]: # 遍历所有该区域下当天weekday可以培养的角色、武器 added = False
added = False for i in user_data[type_]: # 从已经获取的角色数据中查找对应角色、武器
for i in user_data[type_]: # 从已经获取的角色数据中查找对应角色、武器 if id_ == str(i.id):
if id_ == str(i.id): if i.rarity > 3: # 跳过 3 星及以下的武器
if i.rarity > 3: # 跳过 3 星及以下的武器 if type_ == "avatar" and client and calculator_sync: # client 不为 None 时给角色添加天赋信息
if type_ == "avatar": # 给角色添加天赋信息 try:
skills = await self._get_skills_data(client, i.gid) skills = await self._get_skills_data(client, i.gid)
i.skills = skills i.skills = skills
items.append(i) except GenshinException as e:
added = True if e.retcode == -502002:
if added: calculator_sync = False # 发现角色养成计算器没启用 设置状态为 False 并防止下次继续获取
continue self._add_delete_message_job(context, notice.chat_id, notice.message_id, 5)
try: await notice.edit_text(
item = HONEY_DATA[type_][id_] "获取角色天赋信息失败,如果想要显示角色天赋信息,请先在米游社/HoYoLab中使用一次<b>养成计算器</b>后再使用此功能~",
except KeyError: # 跳过不存在或者已忽略的角色、武器 parse_mode=ParseMode.HTML,
logger.warning(f"未在 honey 数据中找到 {type_} {id_} 的信息") )
continue else:
if item[2] < 4: # 跳过 3 星及以下的武器 raise e
continue items.append(i)
items.append( added = True
ItemData( # 添加角色数据中未找到的 if added:
id=id_, continue
name=item[1], try:
rarity=item[2], item = HONEY_DATA[type_][id_]
icon=(await getattr(self.assets_service, type_)(id_).icon()).as_uri(), except KeyError: # 跳过不存在或者已忽略的角色、武器
) logger.warning(f"未在 honey 数据中找到 {type_} {id_} 的信息")
) continue
materials = [] if item[2] < 4: # 跳过 3 星及以下的武器
for mid in area_data["materials"]: # 添加这个区域当天weekday的培养素材 continue
try: items.append(
path = (await self.assets_service.material(mid).icon()).as_uri() ItemData( # 添加角色数据中未找到的
material = HONEY_DATA["material"][mid] id=id_,
materials.append(ItemData(id=mid, icon=path, name=material[1], rarity=material[2])) name=item[1],
except AssetsCouldNotFound as exc: rarity=item[2],
logger.error(f"出错了呜呜呜 ~ {repr(exc)}") icon=(await getattr(self.assets_service, type_)(id_).icon()).as_uri(),
await notice.edit_text("出错了呜呜呜 ~ 派蒙找不到一些素材")
return
areas.append(
AreaData(
name=area_data["name"],
materials=materials,
# template previewer pickle cannot serialize generator
items=list(sort_item(items)),
material_name=get_material_serial_name(map(lambda x: x.name, materials)),
) )
) )
setattr(render_data, {"avatar": "character"}.get(type_, type_), areas) materials = []
for mid in area_data["materials"]: # 添加这个区域当天weekday的培养素材
try:
path = (await self.assets_service.material(mid).icon()).as_uri()
material = HONEY_DATA["material"][mid]
materials.append(ItemData(id=mid, icon=path, name=material[1], rarity=material[2]))
except AssetsCouldNotFound as exc:
logger.error(f"出错了呜呜呜 ~ {repr(exc)}")
await notice.edit_text("出错了呜呜呜 ~ 派蒙找不到一些素材")
return
areas.append(
AreaData(
name=area_data["name"],
materials=materials,
# template previewer pickle cannot serialize generator
items=list(sort_item(items)),
material_name=get_material_serial_name(map(lambda x: x.name, materials)),
)
)
setattr(render_data, {"avatar": "character"}.get(type_, type_), areas)
await message.reply_chat_action(ChatAction.TYPING) await message.reply_chat_action(ChatAction.TYPING)