diff --git a/nonebot_plugin_pjsk/config.py b/nonebot_plugin_pjsk/config.py index 4f19a0a..139f36e 100644 --- a/nonebot_plugin_pjsk/config.py +++ b/nonebot_plugin_pjsk/config.py @@ -21,7 +21,7 @@ class ConfigModel(BaseModel): "https://raw.githubusercontent.com/Agnes4m/nonebot_plugin_pjsk/main/", ] - pjsk_emoji_source: str = "Apple" + pjsk_emoji_source: EmojiSource = EmojiSource.Apple pjsk_help_as_image: bool = True pjsk_reply: bool = True pjsk_sticker_format: str = "PNG" @@ -41,13 +41,12 @@ def check(url: str) -> str: raise TypeError("Must be a list of str") return [check(url) for url in v] - @validator("pjsk_emoji_source") + @validator("pjsk_emoji_source", pre=True) def check_emoji_source(cls, v): # noqa: N805 try: - getattr(EmojiSource, v) + return getattr(EmojiSource, v) except AttributeError as e: raise ValueError("Invalid emoji source") from e - return v config: ConfigModel = ConfigModel.parse_obj(get_driver().config.dict()) diff --git a/nonebot_plugin_pjsk/draw.py b/nonebot_plugin_pjsk/draw.py index f5167bd..5c90516 100644 --- a/nonebot_plugin_pjsk/draw.py +++ b/nonebot_plugin_pjsk/draw.py @@ -23,7 +23,6 @@ Canvas, Color, EmojiOptions, - EmojiSource, Font, Paint, TextAlign, @@ -36,7 +35,7 @@ from .config import config from .resource import ( CACHE_FOLDER, - FONT_PATHS, + FONT_PATH, LOADED_STICKER_INFO, RESOURCE_FOLDER, StickerInfo, @@ -71,11 +70,8 @@ def ensure_font() -> Font: global FONT if not FONT: FONT = Font( - str(FONT_PATHS[0]), - [str(x) for x in FONT_PATHS[1:]], - emoji_options=EmojiOptions( - source=getattr(EmojiSource, config.pjsk_emoji_source), - ), + str(FONT_PATH), + emoji_options=EmojiOptions(source=config.pjsk_emoji_source), ) return FONT @@ -329,7 +325,7 @@ async def render_help_image(text: str) -> Image.Image: width = 1120 font_size = 24 padding = 24 - font = ImageFont.truetype(str(FONT_PATHS[-1]), font_size) + font = ImageFont.truetype(str(FONT_PATH), font_size) warped_lines: List[str] = list( itertools.chain.from_iterable( diff --git a/nonebot_plugin_pjsk/resource.py b/nonebot_plugin_pjsk/resource.py index a88683c..d79fa9d 100644 --- a/nonebot_plugin_pjsk/resource.py +++ b/nonebot_plugin_pjsk/resource.py @@ -21,9 +21,7 @@ shutil.rmtree(CACHE_FOLDER) CACHE_FOLDER.mkdir(parents=True) -FONT_PATHS = [ - FONT_FOLDER / "YurukaFangTang.ttf", -] +FONT_PATH = FONT_FOLDER / "YurukaFangTang.ttf" for _folder in (DATA_FOLDER, FONT_FOLDER, RESOURCE_FOLDER): if not _folder.exists(): @@ -84,10 +82,11 @@ async def download(font_name: str): logger.opt(colors=True).info(f"Successfully downloaded font {font_name}") - tasks: List[Coroutine] = [ - download(path.name) for path in FONT_PATHS if not path.exists() - ] - await asyncio.gather(*tasks) + # tasks: List[Coroutine] = [ + # download(path.name) for path in FONT_PATHS if not path.exists() + # ] + # await asyncio.gather(*tasks) + await download(FONT_PATH.name) async def load_sticker_info():