1
0
forked from bot/app

Merge pull request #51 from EillesWan/main

🥠修复状态缓存出错的问题 & 📃规范化文言文本地化文本
This commit is contained in:
远野千束 2024-07-15 02:26:07 +08:00 committed by GitHub
commit a6b1d1c9e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 100 additions and 66 deletions

View File

@ -17,8 +17,8 @@ from src.utils import satori_utils
from .counter_for_satori import satori_counter from .counter_for_satori import satori_counter
from git import Repo from git import Repo
require("nonebot_plugin_apscheduler") # require("nonebot_plugin_apscheduler")
from nonebot_plugin_apscheduler import scheduler # from nonebot_plugin_apscheduler import scheduler
commit_hash = Repo(".").head.commit.hexsha commit_hash = Repo(".").head.commit.hexsha
@ -63,27 +63,27 @@ data
- percent: float - percent: float
- total: int - total: int
""" """
status_card_cache = {} # lang -> bytes # status_card_cache = {} # lang -> bytes
# 60s刷新一次 # 60s刷新一次
# 之前写的什么鬼玩意,这么重要的功能这样写??? # 之前写的什么鬼玩意,这么重要的功能这样写???
@scheduler.scheduled_job("cron", second="*/40") # @scheduler.scheduled_job("cron", second="*/40")
async def refresh_status_card(): # async def refresh_status_card():
nonebot.logger.debug("Refreshing status card cache.") # nonebot.logger.debug("Refreshing status card cache.")
global status_card_cache # global status_card_cache
status_card_cache = {} # status_card_cache = {}
# bot_data = await get_bots_data() # bot_data = await get_bots_data()
# hardware_data = await get_hardware_data() # hardware_data = await get_hardware_data()
# liteyuki_data = await get_liteyuki_data() # liteyuki_data = await get_liteyuki_data()
# for lang in status_card_cache.keys(): # for lang in status_card_cache.keys():
# status_card_cache[lang] = await generate_status_card( # status_card_cache[lang] = await generate_status_card(
# bot_data, # bot_data,
# hardware_data, # hardware_data,
# liteyuki_data, # liteyuki_data,
# lang=lang, # lang=lang,
# use_cache=False # use_cache=False
# ) # )
# 获取状态卡片 # 获取状态卡片

View File

@ -18,22 +18,35 @@ status_alc = on_alconna(
"process", "process",
alias={"proc", "p", "进程"}, alias={"proc", "p", "进程"},
), ),
Subcommand(
"refresh",
alias={"refr", "r", "刷新"},
),
), ),
) )
status_card_cache = {} # lang -> bytes
@status_alc.handle() @status_alc.handle()
async def _(event: T_MessageEvent, bot: T_Bot): async def _(event: T_MessageEvent, bot: T_Bot):
ulang = get_user_lang(event_utils.get_user_id(event)) ulang = get_user_lang(event_utils.get_user_id(event))
if ulang.lang_code not in status_card_cache.keys(): global status_card_cache
status_card_cache[ulang.lang_code] = await generate_status_card( 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(), bot=await get_bots_data(),
hardware=await get_hardware_data(), hardware=await get_hardware_data(),
liteyuki=await get_liteyuki_data(), liteyuki=await get_liteyuki_data(),
lang=ulang.lang_code, lang=ulang.lang_code,
bot_id=bot.self_id, 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)) await status_alc.finish(UniMessage.image(raw=image))

View File

@ -29,31 +29,31 @@ liteyuki.reload_resources=复誊件资
liteyuki.list_resources=件资 liteyuki.list_resources=件资
liteyuki.reload_resources_success=件资所誊即成,其数有 {NUM} liteyuki.reload_resources_success=件资所誊即成,其数有 {NUM}
liteyuki.loaded_resources=誊件资 {NUM} 而循优先之次 liteyuki.loaded_resources=誊件资 {NUM} 而循优先之次
liteyuki.unloaded_resources=件资已 liteyuki.unloaded_resources=件资已
liteyuki.load_resource_success=件资曰 {NAME} 者,方誊成 liteyuki.load_resource_success=件资曰 {NAME} 者,方誊成
liteyuki.unload_resource_success=件资曰 {NAME} 者,方 liteyuki.unload_resource_success=件资曰 {NAME} 者,方
liteyuki.load_resource_failed=件资曰 {NAME} 者,无以誊 liteyuki.load_resource_failed=件资曰 {NAME} 者,无以誊
liteyuki.unload_resource_failed=件资曰 {NAME} 者,无以 liteyuki.unload_resource_failed=件资曰 {NAME} 者,无以
liteyuki.resource_not_found=件资曰 {NAME} 者,不可求 liteyuki.resource_not_found=件资曰 {NAME} 者,不可求
liteyuki.resource_already_loaded=件资曰 {NAME} 者,方誊 liteyuki.resource_already_loaded=件资曰 {NAME} 者,方誊
liteyuki.resource_already_unloaded=件资曰 {NAME} ,方 liteyuki.resource_already_unloaded=件资曰 {NAME} ,方
liteyuki.need_reload=谨{BTN}复誊以新 liteyuki.need_reload=谨{BTN}复誊以新
liteyuki.dont_repeat=勿复行 liteyuki.dont_repeat=勿复行
liteyuki.change_priority_success=方变件资曰 {NAME} 者优先之次 liteyuki.change_priority_success=方变件资曰 {NAME} 者优先之次
liteyuki.change_priority_failed=件资曰 {NAME} 者优先之次无以变 liteyuki.change_priority_failed=件资曰 {NAME} 者优先之次无以变
liteyuki.group_already=羣号曰 {GROUP} 者,已 {STATUS} ,勿复行 liteyuki.group_already=羣号曰 {GROUP} 者,已{STATUS},勿复行
liteyuki.group_success=羣号曰 {GROUP} 者,方 {STATUS} liteyuki.group_success=羣号曰 {GROUP} 者,方{STATUS}成
liteyuki.permission_denied=权不及也 liteyuki.permission_denied=权不及也
liteyuki.config_remove_success=规制 {KEY} 方去也 liteyuki.config_remove_success=规制 {KEY} 方去也
main.current_language=往复四海皆蛮夷,惟我华夏泱泱。行见闻之广,无可匹敌。故而此番作 {LANG} 之文,以彰圣朝大德。 main.current_language=往复四海皆蛮夷,惟我华夏泱泱。行见闻之广,无可匹敌。故而此番作 {LANG} 之文,以彰圣朝大德。
main.enable_webdash=令 {URL} 为遥讯监 main.enable_webdash=令 {URL} 为遥讯监
main.monitor.title=轻雪监 main.monitor.title=轻雪监
main.monitor.description=工造机巧曰轻雪者,今有司赏监察之大德,为其置一新衙,曰:轻雪监司 main.monitor.description=工造机巧曰轻雪者,今有司赏监察之大德,为其新添一隅,曰:轻雪监阁
main.monitor.cpu=央核之机 main.monitor.cpu=卷枢
main.monitor.memory=机置所存 main.monitor.memory=暂贮
main.monitor.swap=变易之区 main.monitor.swap=变
main.monitor.disk=贮形长存 main.monitor.disk=
main.monitor.usage=计用 main.monitor.usage=计用
main.monitor.total=合 main.monitor.total=合
main.monitor.used=占 main.monitor.used=占
@ -115,8 +115,8 @@ user.profile.lang=语文
user.profile.lang.desc=凡语者,乃语言之所谓,掌人之音及思之习;凡文者,乃文字之所指,操人之德及行之法 user.profile.lang.desc=凡语者,乃语言之所谓,掌人之音及思之习;凡文者,乃文字之所指,操人之德及行之法
user.profile.timezone=时区 user.profile.timezone=时区
user.profile.timezone.desc=所谓时区者,郑和西洋之学所成,乃由英吉利某天监司衙始,分天下为十二时辰之区,以令四海之时得以各循所便 user.profile.timezone.desc=所谓时区者,郑和西洋之学所成,乃由英吉利某天监司衙始,分天下为十二时辰之区,以令四海之时得以各循所便
user.profile.theme= user.profile.theme=
user.profile.theme.desc=昔《周易》乃传阴阳之术,各色纷发而共赏诸方,乃决次色之设,为爱一方也 user.profile.theme.desc=昔《周易》乃传阴阳之术,各色纷发而共赏诸方,乃决次色之设,为爱一方也;誊录锦帛之上,存乎色泽
user.profile.location=居所 user.profile.location=居所
user.profile.location.desc=君子之所常居,道合山水之学。昔孟母三迁择邻里,为人有道成功名。 user.profile.location.desc=君子之所常居,道合山水之学。昔孟母三迁择邻里,为人有道成功名。
user.profile.nickname=字号 user.profile.nickname=字号
@ -137,13 +137,13 @@ status.friends=友
status.groups=羣 status.groups=羣
status.plugins=件 status.plugins=件
status.resources=件资 status.resources=件资
status.bots=工造机 status.bots=工造
status.message_sent=方传 status.message_sent=方传
status.message_received=方受 status.message_received=方受
status.cpu=央核之机 status.cpu=卷枢
status.memory=机置所存 status.memory=暂贮
status.swap=变易之区 status.swap=变
status.disk=贮形长存 status.disk=
status.usage=计用 status.usage=计用
status.total=合 status.total=合
status.used=占 status.used=占
@ -153,7 +153,7 @@ status.days=日
status.hours=小时 status.hours=小时
status.minutes=分 status.minutes=分
status.seconds=秒 status.seconds=秒
status.cores=轮核 status.cores=轮核
status.threads=机线条量 status.threads=程线
status.process=行轨 status.process=行轨
status.description=轻雪端司 status.description=轻雪灵机台

View File

@ -18,9 +18,7 @@ _language_data = {
} }
} }
_user_lang = { _user_lang = {"user_id": "zh-CN"}
"user_id": "zh-CN"
}
def load_from_lang(file_path: str, lang_code: str = None): def load_from_lang(file_path: str, lang_code: str = None):
@ -115,11 +113,16 @@ class Language:
self.fallback_lang_code = fallback_lang_code self.fallback_lang_code = fallback_lang_code
if self.fallback_lang_code is None: 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参数为默认文本 获取当前语言文本kwargs中的default参数为默认文本
**请不要重写本函数**
Args: Args:
item: 文本键 item: 文本键
*args: 格式化参数 *args: 格式化参数
@ -142,6 +145,20 @@ class Language:
return trans return trans
return default or item 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]: def get_many(self, *args: str, **kwargs) -> dict[str, str]:
""" """
获取多个文本 获取多个文本
@ -153,7 +170,9 @@ class Language:
dict: 多个文本 dict: 多个文本
""" """
args_data = {item: self.get(item) for item in args} 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) args_data.update(kwargs_data)
return args_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.profile["lang"] = lang_code
user_db.save(user) user_db.save(user)
_user_lang[user_id] = lang_code _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: if user_id not in _user_lang:
nonebot.logger.debug(f"Loading user language for {user_id}") nonebot.logger.debug(f"Loading user language for {user_id}")
user = user_db.where_one( user = user_db.where_one(
User(), "user_id = ?", user_id, default=User( User(),
user_id=user_id, "user_id = ?",
username="Unknown" user_id,
) default=User(user_id=user_id, username="Unknown"),
) )
lang_code = user.profile.get("lang", get_default_lang_code()) lang_code = user.profile.get("lang", get_default_lang_code())
_user_lang[user_id] = 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: def get_default_lang_code() -> str: