From 576d8c23b33d1424954b03c9b1cec6bc59911320 Mon Sep 17 00:00:00 2001 From: EillesWan Date: Mon, 15 Jul 2024 02:07:15 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=A0=E4=BF=AE=E5=A4=8D=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=BC=93=E5=AD=98=E5=87=BA=E9=94=99=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20=F0=9F=A7=BE=E8=A7=84=E8=8C=83=E5=8C=96=E6=96=87?= =?UTF-8?q?=E8=A8=80=E6=96=87=E6=9C=AC=E5=9C=B0=E5=8C=96=E6=96=87=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/liteyuki_status/api.py | 38 +++++++------- src/plugins/liteyuki_status/status.py | 29 ++++++++--- .../vanilla_language/lang/zh-WY.lang | 48 ++++++++--------- src/utils/base/language.py | 51 +++++++++++++------ 4 files changed, 100 insertions(+), 66 deletions(-) diff --git a/src/plugins/liteyuki_status/api.py b/src/plugins/liteyuki_status/api.py index eb0cf943..c6783150 100644 --- a/src/plugins/liteyuki_status/api.py +++ b/src/plugins/liteyuki_status/api.py @@ -17,8 +17,8 @@ from src.utils import satori_utils from .counter_for_satori import satori_counter from git import Repo -require("nonebot_plugin_apscheduler") -from nonebot_plugin_apscheduler import scheduler +# require("nonebot_plugin_apscheduler") +# from nonebot_plugin_apscheduler import scheduler commit_hash = Repo(".").head.commit.hexsha @@ -63,27 +63,27 @@ data - percent: float - total: int """ -status_card_cache = {} # lang -> bytes +# status_card_cache = {} # lang -> bytes # 60s刷新一次 # 之前写的什么鬼玩意,这么重要的功能这样写??? -@scheduler.scheduled_job("cron", second="*/40") -async def refresh_status_card(): - nonebot.logger.debug("Refreshing status card cache.") - global status_card_cache - status_card_cache = {} - # bot_data = await get_bots_data() - # hardware_data = await get_hardware_data() - # liteyuki_data = await get_liteyuki_data() - # for lang in status_card_cache.keys(): - # status_card_cache[lang] = await generate_status_card( - # bot_data, - # hardware_data, - # liteyuki_data, - # lang=lang, - # use_cache=False - # ) +# @scheduler.scheduled_job("cron", second="*/40") +# async def refresh_status_card(): +# nonebot.logger.debug("Refreshing status card cache.") +# global status_card_cache +# status_card_cache = {} +# bot_data = await get_bots_data() +# hardware_data = await get_hardware_data() +# liteyuki_data = await get_liteyuki_data() +# for lang in status_card_cache.keys(): +# status_card_cache[lang] = await generate_status_card( +# bot_data, +# hardware_data, +# liteyuki_data, +# lang=lang, +# use_cache=False +# ) # 获取状态卡片 diff --git a/src/plugins/liteyuki_status/status.py b/src/plugins/liteyuki_status/status.py index c98fc2e5..96d2e2c1 100644 --- a/src/plugins/liteyuki_status/status.py +++ b/src/plugins/liteyuki_status/status.py @@ -18,22 +18,35 @@ status_alc = on_alconna( "process", alias={"proc", "p", "进程"}, ), + Subcommand( + "refresh", + alias={"refr", "r", "刷新"}, + ), ), ) +status_card_cache = {} # lang -> bytes + @status_alc.handle() async def _(event: T_MessageEvent, bot: T_Bot): ulang = get_user_lang(event_utils.get_user_id(event)) - if ulang.lang_code not in status_card_cache.keys(): - status_card_cache[ulang.lang_code] = await generate_status_card( - bot=await get_bots_data(), - hardware=await get_hardware_data(), - liteyuki=await get_liteyuki_data(), - lang=ulang.lang_code, - bot_id=bot.self_id, + global status_card_cache + if ulang.lang_code not in status_card_cache.keys() or ( + ulang.lang_code in status_card_cache.keys() + and time.time() - status_card_cache[ulang.lang_code][1] > 60 + ): + status_card_cache[ulang.lang_code] = ( + await generate_status_card( + bot=await get_bots_data(), + hardware=await get_hardware_data(), + liteyuki=await get_liteyuki_data(), + lang=ulang.lang_code, + bot_id=bot.self_id, + ), + time.time(), ) - image = status_card_cache[ulang.lang_code] + image = status_card_cache[ulang.lang_code][0] await status_alc.finish(UniMessage.image(raw=image)) diff --git a/src/resources/vanilla_language/lang/zh-WY.lang b/src/resources/vanilla_language/lang/zh-WY.lang index 11d24ab9..85d30278 100644 --- a/src/resources/vanilla_language/lang/zh-WY.lang +++ b/src/resources/vanilla_language/lang/zh-WY.lang @@ -29,31 +29,31 @@ liteyuki.reload_resources=复誊件资 liteyuki.list_resources=件资 liteyuki.reload_resources_success=件资所誊即成,其数有 {NUM} liteyuki.loaded_resources=誊件资 {NUM} 而循优先之次 -liteyuki.unloaded_resources=件资已去 +liteyuki.unloaded_resources=件资已除 liteyuki.load_resource_success=件资曰 {NAME} 者,方誊成 -liteyuki.unload_resource_success=件资曰 {NAME} 者,方去也 +liteyuki.unload_resource_success=件资曰 {NAME} 者,方除也 liteyuki.load_resource_failed=件资曰 {NAME} 者,无以誊 -liteyuki.unload_resource_failed=件资曰 {NAME} 者,无以去 +liteyuki.unload_resource_failed=件资曰 {NAME} 者,无以除 liteyuki.resource_not_found=件资曰 {NAME} 者,不可求 liteyuki.resource_already_loaded=件资曰 {NAME} 者,方誊 -liteyuki.resource_already_unloaded=件资曰 {NAME} ,方去 +liteyuki.resource_already_unloaded=件资曰 {NAME} ,方除 liteyuki.need_reload=谨{BTN}复誊以新 liteyuki.dont_repeat=勿复行 liteyuki.change_priority_success=方变件资曰 {NAME} 者优先之次 liteyuki.change_priority_failed=件资曰 {NAME} 者优先之次无以变 -liteyuki.group_already=羣号曰 {GROUP} 者,已 {STATUS} ,勿复行 -liteyuki.group_success=羣号曰 {GROUP} 者,方 {STATUS} 成 +liteyuki.group_already=羣号曰 {GROUP} 者,已{STATUS},勿复行 +liteyuki.group_success=羣号曰 {GROUP} 者,方{STATUS}成 liteyuki.permission_denied=权不及也 liteyuki.config_remove_success=规制 {KEY} 方去也 main.current_language=往复四海皆蛮夷,惟我华夏泱泱。行见闻之广,无可匹敌。故而此番作 {LANG} 之文,以彰圣朝大德。 -main.enable_webdash=令 {URL} 为遥讯监司 -main.monitor.title=轻雪监司 -main.monitor.description=工造机巧曰轻雪者,今有司赏监察之大德,为其置一新衙,曰:轻雪监司 -main.monitor.cpu=央核之机 -main.monitor.memory=机置所存 -main.monitor.swap=变易之区 -main.monitor.disk=贮形长存 +main.enable_webdash=令 {URL} 为遥讯监阁 +main.monitor.title=轻雪监阁 +main.monitor.description=工造机巧曰轻雪者,今有司赏监察之大德,为其新添一隅,曰:轻雪监阁 +main.monitor.cpu=卷枢 +main.monitor.memory=暂贮 +main.monitor.swap=变贮 +main.monitor.disk=久贮 main.monitor.usage=计用 main.monitor.total=合 main.monitor.used=占 @@ -115,8 +115,8 @@ user.profile.lang=语文 user.profile.lang.desc=凡语者,乃语言之所谓,掌人之音及思之习;凡文者,乃文字之所指,操人之德及行之法 user.profile.timezone=时区 user.profile.timezone.desc=所谓时区者,郑和西洋之学所成,乃由英吉利某天监司衙始,分天下为十二时辰之区,以令四海之时得以各循所便 -user.profile.theme=色泽 -user.profile.theme.desc=昔《周易》乃传阴阳之术,各色纷发而共赏诸方,乃决次色之设,为爱一方也 +user.profile.theme=锦泽 +user.profile.theme.desc=昔《周易》乃传阴阳之术,各色纷发而共赏诸方,乃决次色之设,为爱一方也;誊录锦帛之上,存乎色泽 user.profile.location=居所 user.profile.location.desc=君子之所常居,道合山水之学。昔孟母三迁择邻里,为人有道成功名。 user.profile.nickname=字号 @@ -137,13 +137,13 @@ status.friends=友 status.groups=羣 status.plugins=件 status.resources=件资 -status.bots=工造机巧 +status.bots=工造灵机 status.message_sent=方传 status.message_received=方受 -status.cpu=央核之机 -status.memory=机置所存 -status.swap=变易之区 -status.disk=贮形长存 +status.cpu=卷枢 +status.memory=暂贮 +status.swap=变贮 +status.disk=久贮 status.usage=计用 status.total=合 status.used=占 @@ -153,7 +153,7 @@ status.days=日 status.hours=小时 status.minutes=分 status.seconds=秒 -status.cores=机轮核数 -status.threads=机线条量 -status.process=机行轨度 -status.description=轻雪端司 \ No newline at end of file +status.cores=轮核 +status.threads=程线 +status.process=行轨 +status.description=轻雪灵机台 \ No newline at end of file diff --git a/src/utils/base/language.py b/src/utils/base/language.py index fe0319d1..962d9ad7 100644 --- a/src/utils/base/language.py +++ b/src/utils/base/language.py @@ -13,14 +13,12 @@ from .config import config, get_config from .data_manager import User, user_db _language_data = { - "en": { - "name": "English", - } + "en": { + "name": "English", + } } -_user_lang = { - "user_id": "zh-CN" -} +_user_lang = {"user_id": "zh-CN"} def load_from_lang(file_path: str, lang_code: str = None): @@ -115,11 +113,16 @@ class Language: self.fallback_lang_code = fallback_lang_code if self.fallback_lang_code is None: - self.fallback_lang_code = config.get("default_language", get_system_lang_code()) + self.fallback_lang_code = config.get( + "default_language", get_system_lang_code() + ) - def get(self, item: str, *args, **kwargs) -> str | Any: + def _get(self, item: str, *args, **kwargs) -> str | Any: """ 获取当前语言文本,kwargs中的default参数为默认文本 + + **请不要重写本函数** + Args: item: 文本键 *args: 格式化参数 @@ -142,6 +145,20 @@ class Language: return trans return default or item + def get(self, item: str, *args, **kwargs) -> str | Any: + """ + 获取当前语言文本,kwargs中的default参数为默认文本 + Args: + item: 文本键 + *args: 格式化参数 + **kwargs: 格式化参数 + + Returns: + str: 当前语言的文本 + + """ + return self._get(item, *args, **kwargs) + def get_many(self, *args: str, **kwargs) -> dict[str, str]: """ 获取多个文本 @@ -153,7 +170,9 @@ class Language: dict: 多个文本 """ args_data = {item: self.get(item) for item in args} - kwargs_data = {item: self.get(item, default=default) for item, default in kwargs.items()} + kwargs_data = { + item: self.get(item, default=default) for item, default in kwargs.items() + } args_data.update(kwargs_data) return args_data @@ -162,7 +181,9 @@ def change_user_lang(user_id: str, lang_code: str): """ 修改用户的语言,同时储存到数据库和内存中 """ - user = user_db.where_one(User(), "user_id = ?", user_id, default=User(user_id=user_id)) + user = user_db.where_one( + User(), "user_id = ?", user_id, default=User(user_id=user_id) + ) user.profile["lang"] = lang_code user_db.save(user) _user_lang[user_id] = lang_code @@ -177,10 +198,10 @@ def get_user_lang(user_id: str) -> Language: if user_id not in _user_lang: nonebot.logger.debug(f"Loading user language for {user_id}") user = user_db.where_one( - User(), "user_id = ?", user_id, default=User( - user_id=user_id, - username="Unknown" - ) + User(), + "user_id = ?", + user_id, + default=User(user_id=user_id, username="Unknown"), ) lang_code = user.profile.get("lang", get_default_lang_code()) _user_lang[user_id] = lang_code @@ -192,7 +213,7 @@ def get_system_lang_code() -> str: """ 获取系统语言代码 """ - return locale.getdefaultlocale()[0].replace('_', '-') + return locale.getdefaultlocale()[0].replace("_", "-") def get_default_lang_code() -> str: