From 304eb5e7405cf9e233978c23c9e279f231c43d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sat, 19 Nov 2022 16:08:35 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20weapon=20plugin=20not=20ha?= =?UTF-8?q?ndling=20notice=20correctly=20in=20`AssetsCouldNotFound`=20exce?= =?UTF-8?q?ption?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/base/assets.py | 11 +++++++---- plugins/genshin/daily/material.py | 2 +- plugins/genshin/weapon.py | 12 ++++++++++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/core/base/assets.py b/core/base/assets.py index 1cc2b8b8..13de9904 100644 --- a/core/base/assets.py +++ b/core/base/assets.py @@ -45,7 +45,10 @@ class AssetsServiceError(Exception): class AssetsCouldNotFound(AssetsServiceError): - pass + def __init__(self, message: str, target: str): + self.message = message + self.target = target + super().__init__(f"{message}: target={message}") class _AssetsService(ABC): @@ -223,7 +226,7 @@ class _AvatarAssets(_AssetsService): except ValueError: target = roleToId(target) if isinstance(target, str) or target is None: - raise AssetsCouldNotFound(f"找不到对应的角色: target={temp}") + raise AssetsCouldNotFound("找不到对应的角色", temp) result.id = target result._enka_api = self._enka_api return result @@ -288,7 +291,7 @@ class _WeaponAssets(_AssetsService): if isinstance(target, str): target = int(target) if target.isnumeric() else weaponToId(target) if isinstance(target, str) or target is None: - raise AssetsCouldNotFound(f"找不到对应的武器: target={temp}") + raise AssetsCouldNotFound("找不到对应的武器", temp) result.id = target return result @@ -329,7 +332,7 @@ class _MaterialAssets(_AssetsService): else: target = {v["name"]: int(k) for k, v in MATERIAL_DATA.items()}.get(target) if isinstance(target, str) or target is None: - raise AssetsCouldNotFound(f"找不到对应的素材: target={temp}") + raise AssetsCouldNotFound("找不到对应的素材", temp) result.id = target return result diff --git a/plugins/genshin/daily/material.py b/plugins/genshin/daily/material.py index 4fa64b01..130e957c 100644 --- a/plugins/genshin/daily/material.py +++ b/plugins/genshin/daily/material.py @@ -312,7 +312,7 @@ class DailyMaterial(Plugin, BasePlugin): 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)}") + logger.warning("%s mid[%s]", exc.message, exc.target) await notice.edit_text("出错了呜呜呜 ~ 派蒙找不到一些素材") return areas.append( diff --git a/plugins/genshin/weapon.py b/plugins/genshin/weapon.py index ba5062ec..0c8b02b8 100644 --- a/plugins/genshin/weapon.py +++ b/plugins/genshin/weapon.py @@ -2,7 +2,7 @@ from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update from telegram.constants import ChatAction from telegram.ext import CallbackContext, CommandHandler, MessageHandler, filters -from core.base.assets import AssetsService +from core.base.assets import AssetsService, AssetsCouldNotFound from core.baseplugin import BasePlugin from core.plugin import Plugin, handler from core.template import TemplateService @@ -109,7 +109,15 @@ class WeaponPlugin(Plugin, BasePlugin): } return _template_data - template_data = await input_template_data(weapon_data) + try: + template_data = await input_template_data(weapon_data) + except AssetsCouldNotFound as exc: + logger.warning("%s weapon_name[%s]", exc.message, weapon_name) + reply_message = await message.reply_text(f"数据库中没有找到 {weapon_name}") + if filters.ChatType.GROUPS.filter(reply_message): + self._add_delete_message_job(context, message.chat_id, message.message_id) + self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id) + return png_data = await self.template_service.render( "genshin/weapon/weapon.html", template_data, {"width": 540, "height": 540}, ttl=31 * 24 * 60 * 60 )