mirror of
https://github.com/TriM-Organization/LiteyukiBot-TriM.git
synced 2024-11-25 00:25:04 +08:00
✨ 插件管理添加翻页按钮,支持从内存快速获取当前session状态
This commit is contained in:
parent
0e02af59ca
commit
0e47e3c163
@ -8,6 +8,11 @@ from liteyuki.utils.base.data import LiteModel
|
|||||||
from liteyuki.utils.base.data_manager import GlobalPlugin, Group, User, group_db, plugin_db, user_db
|
from liteyuki.utils.base.data_manager import GlobalPlugin, Group, User, group_db, plugin_db, user_db
|
||||||
from liteyuki.utils.base.ly_typing import T_MessageEvent
|
from liteyuki.utils.base.ly_typing import T_MessageEvent
|
||||||
|
|
||||||
|
__group_data = {} # 群数据缓存, {group_id: Group}
|
||||||
|
__user_data = {} # 用户数据缓存, {user_id: User}
|
||||||
|
__default_enable = {} # 插件默认启用状态缓存, {plugin_name: bool} static
|
||||||
|
__global_enable = {} # 插件全局启用状态缓存, {plugin_name: bool} dynamic
|
||||||
|
|
||||||
|
|
||||||
class PluginTag(LiteModel):
|
class PluginTag(LiteModel):
|
||||||
label: str
|
label: str
|
||||||
@ -63,7 +68,7 @@ async def get_store_plugin(plugin_name: str) -> Optional[StorePlugin]:
|
|||||||
|
|
||||||
def get_plugin_default_enable(plugin_name: str) -> bool:
|
def get_plugin_default_enable(plugin_name: str) -> bool:
|
||||||
"""
|
"""
|
||||||
获取插件默认启用状态,由插件定义,不存在则默认为启用
|
获取插件默认启用状态,由插件定义,不存在则默认为启用,优先从缓存中获取
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
plugin_name (str): 插件模块名
|
plugin_name (str): 插件模块名
|
||||||
@ -71,9 +76,12 @@ def get_plugin_default_enable(plugin_name: str) -> bool:
|
|||||||
Returns:
|
Returns:
|
||||||
bool: 插件默认状态
|
bool: 插件默认状态
|
||||||
"""
|
"""
|
||||||
plug = nonebot.plugin.get_plugin(plugin_name)
|
if plugin_name not in __default_enable:
|
||||||
return (plug.metadata.extra.get("default_enable", True)
|
plug = nonebot.plugin.get_plugin(plugin_name)
|
||||||
if plug.metadata else True) if plug else True
|
default_enable = (plug.metadata.extra.get("default_enable", True) if plug.metadata else True) if plug else True
|
||||||
|
__default_enable[plugin_name] = default_enable
|
||||||
|
|
||||||
|
return __default_enable[plugin_name]
|
||||||
|
|
||||||
|
|
||||||
def get_plugin_session_enable(event: T_MessageEvent, plugin_name: str) -> bool:
|
def get_plugin_session_enable(event: T_MessageEvent, plugin_name: str) -> bool:
|
||||||
@ -88,9 +96,19 @@ def get_plugin_session_enable(event: T_MessageEvent, plugin_name: str) -> bool:
|
|||||||
bool: 插件当前状态
|
bool: 插件当前状态
|
||||||
"""
|
"""
|
||||||
if event.message_type == "group":
|
if event.message_type == "group":
|
||||||
session: Group = group_db.first(Group(), "group_id = ?", event.group_id, default=Group(group_id=str(event.group_id)))
|
group_id = str(event.group_id)
|
||||||
|
if group_id not in __group_data:
|
||||||
|
group: Group = group_db.first(Group(), "group_id = ?", group_id, default=Group(group_id=group_id))
|
||||||
|
__group_data[str(event.group_id)] = group
|
||||||
|
|
||||||
|
session = __group_data[group_id]
|
||||||
else:
|
else:
|
||||||
session: User = user_db.first(User(), "user_id = ?", event.user_id, default=User(user_id=str(event.user_id)))
|
# session: User = user_db.first(User(), "user_id = ?", event.user_id, default=User(user_id=str(event.user_id)))
|
||||||
|
user_id = str(event.user_id)
|
||||||
|
if user_id not in __user_data:
|
||||||
|
user: User = user_db.first(User(), "user_id = ?", user_id, default=User(user_id=user_id))
|
||||||
|
__user_data[user_id] = user
|
||||||
|
session = __user_data[user_id]
|
||||||
# 默认停用插件在启用列表内表示启用
|
# 默认停用插件在启用列表内表示启用
|
||||||
# 默认停用插件不在启用列表内表示停用
|
# 默认停用插件不在启用列表内表示停用
|
||||||
# 默认启用插件在停用列表内表示停用
|
# 默认启用插件在停用列表内表示停用
|
||||||
@ -102,13 +120,81 @@ def get_plugin_session_enable(event: T_MessageEvent, plugin_name: str) -> bool:
|
|||||||
return plugin_name in session.enabled_plugins
|
return plugin_name in session.enabled_plugins
|
||||||
|
|
||||||
|
|
||||||
|
def set_plugin_session_enable(event: T_MessageEvent, plugin_name: str, enable: bool):
|
||||||
|
"""
|
||||||
|
设置插件会话启用状态,同时更新数据库和缓存
|
||||||
|
Args:
|
||||||
|
event:
|
||||||
|
plugin_name:
|
||||||
|
enable:
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
if event.message_type == "group":
|
||||||
|
session = group_db.first(Group(), "group_id = ?", str(event.group_id), default=Group(group_id=str(event.group_id)))
|
||||||
|
else:
|
||||||
|
session = user_db.first(User(), "user_id = ?", str(event.user_id), default=User(user_id=str(event.user_id)))
|
||||||
|
default_enable = get_plugin_default_enable(plugin_name)
|
||||||
|
if default_enable:
|
||||||
|
if enable:
|
||||||
|
session.disabled_plugins.remove(plugin_name)
|
||||||
|
else:
|
||||||
|
session.disabled_plugins.append(plugin_name)
|
||||||
|
else:
|
||||||
|
if enable:
|
||||||
|
session.enabled_plugins.append(plugin_name)
|
||||||
|
else:
|
||||||
|
session.enabled_plugins.remove(plugin_name)
|
||||||
|
|
||||||
|
if event.message_type == "group":
|
||||||
|
__group_data[str(event.group_id)] = session
|
||||||
|
print(session)
|
||||||
|
group_db.upsert(session)
|
||||||
|
else:
|
||||||
|
__user_data[str(event.user_id)] = session
|
||||||
|
user_db.upsert(session)
|
||||||
|
|
||||||
|
|
||||||
def get_plugin_global_enable(plugin_name: str) -> bool:
|
def get_plugin_global_enable(plugin_name: str) -> bool:
|
||||||
nonebot.plugin.get_plugin(plugin_name)
|
"""
|
||||||
return plugin_db.first(
|
获取插件全局启用状态, 优先从缓存中获取
|
||||||
|
Args:
|
||||||
|
plugin_name:
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
if plugin_name not in __global_enable:
|
||||||
|
plugin = plugin_db.first(
|
||||||
|
GlobalPlugin(),
|
||||||
|
"module_name = ?",
|
||||||
|
plugin_name,
|
||||||
|
default=GlobalPlugin(module_name=plugin_name, enabled=True))
|
||||||
|
__global_enable[plugin_name] = plugin.enabled
|
||||||
|
|
||||||
|
return __global_enable[plugin_name]
|
||||||
|
|
||||||
|
|
||||||
|
def set_plugin_global_enable(plugin_name: str, enable: bool):
|
||||||
|
"""
|
||||||
|
设置插件全局启用状态,同时更新数据库和缓存
|
||||||
|
Args:
|
||||||
|
plugin_name:
|
||||||
|
enable:
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
plugin = plugin_db.first(
|
||||||
GlobalPlugin(),
|
GlobalPlugin(),
|
||||||
"module_name = ?",
|
"module_name = ?",
|
||||||
plugin_name,
|
plugin_name,
|
||||||
default=GlobalPlugin(module_name=plugin_name, enabled=True)).enabled
|
default=GlobalPlugin(module_name=plugin_name, enabled=True))
|
||||||
|
plugin.enabled = enable
|
||||||
|
|
||||||
|
plugin_db.upsert(plugin)
|
||||||
|
__global_enable[plugin_name] = enable
|
||||||
|
|
||||||
|
|
||||||
def get_plugin_can_be_toggle(plugin_name: str) -> bool:
|
def get_plugin_can_be_toggle(plugin_name: str) -> bool:
|
||||||
@ -135,5 +221,25 @@ def get_group_enable(group_id: str) -> bool:
|
|||||||
Returns:
|
Returns:
|
||||||
bool: 群组是否启用插件
|
bool: 群组是否启用插件
|
||||||
"""
|
"""
|
||||||
session: Group = group_db.first(Group(), "group_id = ?", group_id, default=Group(group_id=group_id))
|
group_id = str(group_id)
|
||||||
return session.enable
|
if group_id not in __group_data:
|
||||||
|
group: Group = group_db.first(Group(), "group_id = ?", group_id, default=Group(group_id=group_id))
|
||||||
|
__group_data[group_id] = group
|
||||||
|
|
||||||
|
return __group_data[group_id].enable
|
||||||
|
|
||||||
|
|
||||||
|
def set_group_enable(group_id: str, enable: bool):
|
||||||
|
"""
|
||||||
|
设置群组是否启用插机器人
|
||||||
|
|
||||||
|
Args:
|
||||||
|
group_id (str): 群组ID
|
||||||
|
enable (bool): 是否启用
|
||||||
|
"""
|
||||||
|
group_id = str(group_id)
|
||||||
|
group: Group = group_db.first(Group(), "group_id = ?", group_id, default=Group(group_id=group_id))
|
||||||
|
group.enable = enable
|
||||||
|
|
||||||
|
__group_data[group_id] = group
|
||||||
|
group_db.upsert(group)
|
||||||
|
@ -101,8 +101,6 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher):
|
|||||||
|
|
||||||
session_enable = get_plugin_session_enable(event, plugin_name) # 获取插件当前状态
|
session_enable = get_plugin_session_enable(event, plugin_name) # 获取插件当前状态
|
||||||
|
|
||||||
default_enable = get_plugin_default_enable(plugin_name) # 获取插件默认状态
|
|
||||||
|
|
||||||
can_be_toggled = get_plugin_can_be_toggle(plugin_name) # 获取插件是否可以被启用/停用
|
can_be_toggled = get_plugin_can_be_toggle(plugin_name) # 获取插件是否可以被启用/停用
|
||||||
|
|
||||||
if not plugin_exist:
|
if not plugin_exist:
|
||||||
@ -123,22 +121,9 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher):
|
|||||||
else:
|
else:
|
||||||
raise FinishedException(ulang.get("Permission Denied"))
|
raise FinishedException(ulang.get("Permission Denied"))
|
||||||
try:
|
try:
|
||||||
if toggle:
|
set_plugin_session_enable(event, plugin_name, toggle)
|
||||||
if default_enable:
|
|
||||||
session.disabled_plugins.remove(plugin_name)
|
|
||||||
else:
|
|
||||||
session.enabled_plugins.append(plugin_name)
|
|
||||||
else:
|
|
||||||
if default_enable:
|
|
||||||
session.disabled_plugins.append(plugin_name)
|
|
||||||
else:
|
|
||||||
session.enabled_plugins.remove(plugin_name)
|
|
||||||
if event.message_type == "private":
|
|
||||||
user_db.upsert(session)
|
|
||||||
else:
|
|
||||||
group_db.upsert(session)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
nonebot.logger.error(e)
|
||||||
await npm.finish(
|
await npm.finish(
|
||||||
ulang.get(
|
ulang.get(
|
||||||
"npm.toggle_failed",
|
"npm.toggle_failed",
|
||||||
@ -173,14 +158,8 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher):
|
|||||||
ulang.get("npm.plugin_already", NAME=plugin_name, STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable")))
|
ulang.get("npm.plugin_already", NAME=plugin_name, STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable")))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
storePlugin = plugin_db.first(GlobalPlugin(), "module_name = ?", plugin_name, default=GlobalPlugin(module_name=plugin_name))
|
set_plugin_global_enable(plugin_name, toggle)
|
||||||
if toggle:
|
|
||||||
storePlugin.enabled = True
|
|
||||||
else:
|
|
||||||
storePlugin.enabled = False
|
|
||||||
plugin_db.upsert(storePlugin)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
|
||||||
await npm.finish(
|
await npm.finish(
|
||||||
ulang.get(
|
ulang.get(
|
||||||
"npm.toggle_failed",
|
"npm.toggle_failed",
|
||||||
@ -348,6 +327,10 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher):
|
|||||||
|
|
||||||
reply += f" {btn_uninstall} {btn_toggle_global}"
|
reply += f" {btn_uninstall} {btn_toggle_global}"
|
||||||
reply += "\n\n***\n"
|
reply += "\n\n***\n"
|
||||||
|
# 根据页数添加翻页按钮。第一页显示上一页文本而不是按钮,最后一页显示下一页文本而不是按钮
|
||||||
|
btn_prev = md.btn_cmd(ulang.get("npm.prev_page"), f"npm list {page - 1} {num_per_page}") if page > 1 else ulang.get("npm.prev_page")
|
||||||
|
btn_next = md.btn_cmd(ulang.get("npm.next_page"), f"npm list {page + 1} {num_per_page}") if page < total else ulang.get("npm.next_page")
|
||||||
|
reply += f"\n{btn_prev} {page}/{total} {btn_next}"
|
||||||
await md.send_md(reply, bot, event=event)
|
await md.send_md(reply, bot, event=event)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@ -385,16 +368,12 @@ async def _(bot: T_Bot, event: T_MessageEvent, gm: Matcher, result: Arparma):
|
|||||||
await gm.finish(ulang.get("liteyuki.invalid_command"), liteyuki_pass=True)
|
await gm.finish(ulang.get("liteyuki.invalid_command"), liteyuki_pass=True)
|
||||||
|
|
||||||
enabled = get_group_enable(group_id)
|
enabled = get_group_enable(group_id)
|
||||||
|
print(enabled, to_enable)
|
||||||
if enabled == to_enable:
|
if enabled == to_enable:
|
||||||
await gm.finish(ulang.get("liteyuki.group_already", STATUS=ulang.get("npm.enable") if to_enable else ulang.get("npm.disable"), GROUP=group_id),
|
await gm.finish(ulang.get("liteyuki.group_already", STATUS=ulang.get("npm.enable") if to_enable else ulang.get("npm.disable"), GROUP=group_id),
|
||||||
liteyuki_pass=True)
|
liteyuki_pass=True)
|
||||||
else:
|
else:
|
||||||
group: Group = group_db.first(Group(), "group_id = ?", group_id, default=Group(group_id=group_id))
|
set_group_enable(group_id, to_enable)
|
||||||
if to_enable:
|
|
||||||
group.enable = True
|
|
||||||
else:
|
|
||||||
group.enable = False
|
|
||||||
group_db.upsert(group)
|
|
||||||
await gm.finish(
|
await gm.finish(
|
||||||
ulang.get("liteyuki.group_success", STATUS=ulang.get("npm.enable") if to_enable else ulang.get("npm.disable"), GROUP=group_id),
|
ulang.get("liteyuki.group_success", STATUS=ulang.get("npm.enable") if to_enable else ulang.get("npm.disable"), GROUP=group_id),
|
||||||
liteyuki_pass=True
|
liteyuki_pass=True
|
||||||
@ -438,10 +417,10 @@ async def _(result: Arparma, matcher: Matcher, event: T_MessageEvent, bot: T_Bot
|
|||||||
reply = [
|
reply = [
|
||||||
mdc.heading(escape_md(loaded_plugin.metadata.name)),
|
mdc.heading(escape_md(loaded_plugin.metadata.name)),
|
||||||
mdc.quote(mdc.bold(ulang.get("npm.author")) + " " +
|
mdc.quote(mdc.bold(ulang.get("npm.author")) + " " +
|
||||||
mdc.link(store_plugin.author, f"https://github/{store_plugin.author}") if store_plugin.author else "Unknown"),
|
(mdc.link(store_plugin.author, f"https://github.com/{store_plugin.author}") if store_plugin.author else "Unknown")),
|
||||||
mdc.quote(mdc.bold(ulang.get("npm.description")) + " " + mdc.paragraph(max(loaded_plugin.metadata.description, store_plugin.desc))),
|
mdc.quote(mdc.bold(ulang.get("npm.description")) + " " + mdc.paragraph(max(loaded_plugin.metadata.description, store_plugin.desc))),
|
||||||
mdc.heading(ulang.get("npm.usage"), 2),
|
mdc.heading(ulang.get("npm.usage"), 2),
|
||||||
mdc.paragraph(loaded_plugin.metadata.usage),
|
mdc.paragraph(escape_md(loaded_plugin.metadata.usage)),
|
||||||
]
|
]
|
||||||
await md.send_md(compile_md(reply), bot, event=event)
|
await md.send_md(compile_md(reply), bot, event=event)
|
||||||
else:
|
else:
|
||||||
|
@ -5,7 +5,7 @@ from nonebot import require
|
|||||||
|
|
||||||
from liteyuki.utils.base.data import LiteModel
|
from liteyuki.utils.base.data import LiteModel
|
||||||
from liteyuki.utils.base.data_manager import User, user_db
|
from liteyuki.utils.base.data_manager import User, user_db
|
||||||
from liteyuki.utils.base.language import Language, get_all_lang, get_user_lang
|
from liteyuki.utils.base.language import Language, change_user_lang, get_all_lang, get_user_lang
|
||||||
from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent
|
from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent
|
||||||
from liteyuki.utils.message.message import MarkdownMessage as md
|
from liteyuki.utils.message.message import MarkdownMessage as md
|
||||||
from .const import representative_timezones_list
|
from .const import representative_timezones_list
|
||||||
@ -46,7 +46,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
|||||||
if result.subcommands.get("set"):
|
if result.subcommands.get("set"):
|
||||||
if result.subcommands["set"].args.get("value"):
|
if result.subcommands["set"].args.get("value"):
|
||||||
# 对合法性进行校验后设置
|
# 对合法性进行校验后设置
|
||||||
r = set_profile(result.args["key"], result.args["value"])
|
r = set_profile(result.args["key"], result.args["value"], str(event.user_id))
|
||||||
if r:
|
if r:
|
||||||
user.profile[result.args["key"]] = result.args["value"]
|
user.profile[result.args["key"]] = result.args["value"]
|
||||||
user_db.upsert(user) # 数据库保存
|
user_db.upsert(user) # 数据库保存
|
||||||
@ -126,9 +126,10 @@ def get_profile_menu(key: str, ulang: Language) -> Optional[str]:
|
|||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
|
||||||
def set_profile(key: str, value: str) -> bool:
|
def set_profile(key: str, value: str, user_id: str) -> bool:
|
||||||
"""设置属性,使用if分支对每一个合法性进行检查
|
"""设置属性,使用if分支对每一个合法性进行检查
|
||||||
Args:
|
Args:
|
||||||
|
user_id:
|
||||||
key:
|
key:
|
||||||
value:
|
value:
|
||||||
|
|
||||||
@ -138,6 +139,7 @@ def set_profile(key: str, value: str) -> bool:
|
|||||||
"""
|
"""
|
||||||
if key == "lang":
|
if key == "lang":
|
||||||
if value in get_all_lang():
|
if value in get_all_lang():
|
||||||
|
change_user_lang(user_id, value)
|
||||||
return True
|
return True
|
||||||
elif key == "timezone":
|
elif key == "timezone":
|
||||||
if value in pytz.all_timezones:
|
if value in pytz.all_timezones:
|
||||||
|
@ -57,6 +57,7 @@ class Database:
|
|||||||
"""
|
"""
|
||||||
table_name = model.TABLE_NAME
|
table_name = model.TABLE_NAME
|
||||||
model_type = type(model)
|
model_type = type(model)
|
||||||
|
nonebot.logger.debug(f"Selecting {model_type} WHERE {condition} {args}")
|
||||||
if not table_name:
|
if not table_name:
|
||||||
raise ValueError(f"数据模型{model_type.__name__}未提供表名")
|
raise ValueError(f"数据模型{model_type.__name__}未提供表名")
|
||||||
|
|
||||||
@ -85,6 +86,7 @@ class Database:
|
|||||||
"""
|
"""
|
||||||
table_list = [item[0] for item in self.cursor.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()]
|
table_list = [item[0] for item in self.cursor.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()]
|
||||||
for model in args:
|
for model in args:
|
||||||
|
nonebot.logger.debug(f"Upserting {model}")
|
||||||
if not model.TABLE_NAME:
|
if not model.TABLE_NAME:
|
||||||
raise ValueError(f"数据模型 {model.__class__.__name__} 未提供表名")
|
raise ValueError(f"数据模型 {model.__class__.__name__} 未提供表名")
|
||||||
elif model.TABLE_NAME not in table_list:
|
elif model.TABLE_NAME not in table_list:
|
||||||
@ -164,13 +166,9 @@ class Database:
|
|||||||
return new_obj
|
return new_obj
|
||||||
elif isinstance(obj, (list, set, tuple)):
|
elif isinstance(obj, (list, set, tuple)):
|
||||||
|
|
||||||
print(" - Load as List")
|
|
||||||
|
|
||||||
new_obj = []
|
new_obj = []
|
||||||
for item in obj:
|
for item in obj:
|
||||||
|
|
||||||
print(" - Loading Item", item)
|
|
||||||
|
|
||||||
if isinstance(item, bytes):
|
if isinstance(item, bytes):
|
||||||
|
|
||||||
# 对bytes进行尝试解析,解析失败则返回原始bytes
|
# 对bytes进行尝试解析,解析失败则返回原始bytes
|
||||||
@ -179,8 +177,6 @@ class Database:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
new_obj.append(self._load(item))
|
new_obj.append(self._load(item))
|
||||||
|
|
||||||
print(" - Load as Bytes | Result:", new_obj[-1])
|
|
||||||
|
|
||||||
elif isinstance(item, str) and item.startswith(self.FOREIGN_KEY_PREFIX):
|
elif isinstance(item, str) and item.startswith(self.FOREIGN_KEY_PREFIX):
|
||||||
new_obj.append(self._load(self._get_foreign_data(item)))
|
new_obj.append(self._load(self._get_foreign_data(item)))
|
||||||
else:
|
else:
|
||||||
@ -202,6 +198,7 @@ class Database:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
table_name = model.TABLE_NAME
|
table_name = model.TABLE_NAME
|
||||||
|
nonebot.logger.debug(f"Deleting {model} WHERE {condition} {args}")
|
||||||
if not table_name:
|
if not table_name:
|
||||||
raise ValueError(f"数据模型{model.__class__.__name__}未提供表名")
|
raise ValueError(f"数据模型{model.__class__.__name__}未提供表名")
|
||||||
if model.id is not None:
|
if model.id is not None:
|
||||||
|
@ -145,20 +145,21 @@ class Language:
|
|||||||
|
|
||||||
def change_user_lang(user_id: str, lang_code: str):
|
def change_user_lang(user_id: str, lang_code: str):
|
||||||
"""
|
"""
|
||||||
修改用户的语言
|
修改用户的语言,同时储存到数据库和内存中
|
||||||
"""
|
"""
|
||||||
user = user_db.first(User(), "user_id = ?", user_id, default=User(user_id=user_id))
|
user = user_db.first(User(), "user_id = ?", user_id, default=User(user_id=user_id))
|
||||||
user.profile["lang"] = lang_code
|
user.profile["lang"] = lang_code
|
||||||
user_db.update(user, "user_id = ?", user_id)
|
user_db.update(user, "user_id = ?", user_id)
|
||||||
|
_user_lang[user_id] = lang_code
|
||||||
|
|
||||||
|
|
||||||
def get_user_lang(user_id: str) -> Language:
|
def get_user_lang(user_id: str) -> Language:
|
||||||
"""
|
"""
|
||||||
获取用户的语言实例,优先从内存中获取
|
获取用户的语言实例,优先从内存中获取
|
||||||
"""
|
"""
|
||||||
if user_id in _user_lang:
|
user_id = str(user_id)
|
||||||
return Language(_user_lang[user_id])
|
|
||||||
else:
|
if user_id not in _user_lang:
|
||||||
user = user_db.first(
|
user = user_db.first(
|
||||||
User(), "user_id = ?", user_id, default=User(
|
User(), "user_id = ?", user_id, default=User(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
@ -167,7 +168,8 @@ def get_user_lang(user_id: str) -> Language:
|
|||||||
)
|
)
|
||||||
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
|
||||||
return Language(lang_code)
|
|
||||||
|
return Language(_user_lang[user_id])
|
||||||
|
|
||||||
|
|
||||||
def get_system_lang_code() -> str:
|
def get_system_lang_code() -> str:
|
||||||
|
@ -18,11 +18,10 @@ def escape_md(text: str) -> str:
|
|||||||
Returns:
|
Returns:
|
||||||
str: 转义后文本
|
str: 转义后文本
|
||||||
"""
|
"""
|
||||||
text = text.replace("\n", r"\n").replace('"', r'\\\"')
|
|
||||||
spacial_chars = r"\`*_{}[]()#+-.!"
|
spacial_chars = r"\`*_{}[]()#+-.!"
|
||||||
for char in spacial_chars:
|
for char in spacial_chars:
|
||||||
text = text.replace(char, "\\" + char)
|
text = text.replace(char, "\\\\" + char)
|
||||||
return text
|
return text.replace("\n", r"\n").replace('"', r'\\\"')
|
||||||
|
|
||||||
|
|
||||||
def escape_decorator(func):
|
def escape_decorator(func):
|
||||||
@ -41,7 +40,6 @@ def compile_md(comps: list[str]) -> str:
|
|||||||
Returns:
|
Returns:
|
||||||
str: 编译后文本
|
str: 编译后文本
|
||||||
"""
|
"""
|
||||||
print("".join(comps))
|
|
||||||
return "".join(comps)
|
return "".join(comps)
|
||||||
|
|
||||||
|
|
||||||
@ -80,7 +78,7 @@ class MarkdownComponent:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def quote(text: str) -> str:
|
def quote(text: str) -> str:
|
||||||
"""引用"""
|
"""引用"""
|
||||||
return f"> {text}\n"
|
return f"> {text}\n\n"
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def link(text: str, url: str, symbol: bool = True) -> str:
|
def link(text: str, url: str, symbol: bool = True) -> str:
|
||||||
|
@ -9,7 +9,7 @@ nb-cli==1.4.1
|
|||||||
nonebot2[fastapi,httpx,websockets]==2.2.1
|
nonebot2[fastapi,httpx,websockets]==2.2.1
|
||||||
nonebot-plugin-htmlrender==0.3.1
|
nonebot-plugin-htmlrender==0.3.1
|
||||||
nonebot-adapter-onebot==2.4.3
|
nonebot-adapter-onebot==2.4.3
|
||||||
nonebot-plugin-alconna==0.42.3
|
nonebot-plugin-alconna==0.42.4
|
||||||
nonebot_plugin_apscheduler==0.4.0
|
nonebot_plugin_apscheduler==0.4.0
|
||||||
playwright==1.17.2
|
playwright==1.17.2
|
||||||
psutil==5.9.8
|
psutil==5.9.8
|
||||||
|
Loading…
Reference in New Issue
Block a user