mirror of
https://github.com/LiteyukiStudio/LiteyukiBot.git
synced 2025-07-27 23:50:56 +00:00
feat: 配置项目的热修改
This commit is contained in:
@ -1,17 +1,23 @@
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
import aiofiles
|
||||
import yaml
|
||||
from nonebot import require
|
||||
from nonebot.permission import SUPERUSER
|
||||
from git import Repo
|
||||
|
||||
from liteyuki.utils.config import config
|
||||
from liteyuki.utils.config import config, load_from_yaml
|
||||
from liteyuki.utils.ly_typing import T_Bot, T_MessageEvent
|
||||
|
||||
from liteyuki.utils.language import get_user_lang
|
||||
from liteyuki.utils.message import Markdown as md, send_markdown
|
||||
|
||||
from .reloader import Reloader
|
||||
from liteyuki.utils.data_manager import StoredConfig, common_db
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
from nonebot_plugin_alconna import on_alconna, Alconna
|
||||
from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand, Arparma
|
||||
|
||||
cmd_liteyuki = on_alconna(
|
||||
Alconna(
|
||||
@ -34,6 +40,24 @@ reload_liteyuki = on_alconna(
|
||||
permission=SUPERUSER
|
||||
)
|
||||
|
||||
cmd_config = on_alconna(
|
||||
Alconna(
|
||||
["config", "配置"],
|
||||
Subcommand(
|
||||
"set",
|
||||
Args["key", str]["value", Any],
|
||||
alias=["设置"],
|
||||
|
||||
),
|
||||
Subcommand(
|
||||
"get",
|
||||
Args["key", str, None],
|
||||
alias=["查询"]
|
||||
)
|
||||
),
|
||||
permission=SUPERUSER
|
||||
)
|
||||
|
||||
|
||||
@cmd_liteyuki.handle()
|
||||
async def _(bot: T_Bot):
|
||||
@ -64,3 +88,36 @@ async def _(bot: T_Bot, event: T_MessageEvent):
|
||||
async def _():
|
||||
await reload_liteyuki.send("Liteyuki reloading")
|
||||
Reloader.reload(3)
|
||||
|
||||
|
||||
@cmd_config.handle()
|
||||
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
||||
ulang = get_user_lang(str(event.user_id))
|
||||
stored_config: StoredConfig = common_db.first(StoredConfig(), default=StoredConfig())
|
||||
if result.subcommands.get("set"):
|
||||
key, value = result.subcommands.get("set").args.get("key"), result.subcommands.get("set").args.get("value")
|
||||
try:
|
||||
value = eval(value)
|
||||
except:
|
||||
pass
|
||||
stored_config.config[key] = value
|
||||
common_db.upsert(stored_config)
|
||||
await cmd_config.finish(f"{ulang.get('liteyuki.config_set_success', KEY=key, VAL=value)}")
|
||||
elif result.subcommands.get("get"):
|
||||
key = result.subcommands.get("get").args.get("key")
|
||||
file_config = load_from_yaml("config.yml")
|
||||
reply = f"{ulang.get('liteyuki.current_config')}"
|
||||
if key:
|
||||
reply += f"```dotenv\n{key}={file_config.get(key, stored_config.config.get(key))}\n```"
|
||||
else:
|
||||
reply = f"{ulang.get('liteyuki.current_config')}"
|
||||
reply += f"\n{ulang.get('liteyuki.static_config')}\n```dotenv"
|
||||
for k, v in file_config.items():
|
||||
reply += f"\n{k}={v}"
|
||||
reply += "\n```"
|
||||
if len(stored_config.config) > 0:
|
||||
reply += f"\n{ulang.get('liteyuki.stored_config')}\n```dotenv"
|
||||
for k, v in stored_config.config.items():
|
||||
reply += f"\n{k}={v}"
|
||||
reply += "\n```"
|
||||
await send_markdown(reply, bot, event=event)
|
||||
|
@ -9,6 +9,7 @@ log.success=Success
|
||||
liteyuki.restart=Restart
|
||||
liteyuki.restart_now=Restart now
|
||||
liteyuki.update_restart=Update completely, You can {RESTART} or later to apply the changes
|
||||
liteyuki.current_config=Current Config
|
||||
|
||||
main.current_language=Current config language: {LANG}
|
||||
main.enable_webdash=Web dashboard is enabled: {URL}
|
||||
|
@ -9,6 +9,10 @@ log.success=成功
|
||||
liteyuki.restart=重启
|
||||
liteyuki.restart_now=立即重启
|
||||
liteyuki.update_restart=更新完成,你可以{RESTART}或稍后手动重启以应用这些更新
|
||||
liteyuki.current_config=当前配置项如下
|
||||
liteyuki.static_config=静态文件配置项
|
||||
liteyuki.stored_config=储存的配置项
|
||||
liteyuki.config_set_success=配置项 {KEY}={VAL} 设置成功
|
||||
|
||||
main.current_language=当前配置语言为: {LANG}
|
||||
main.enable_webdash=已启用网页监控面板: {URL}
|
||||
|
@ -15,9 +15,9 @@ class LiteModel(BaseModel):
|
||||
|
||||
def dump(self, *args, **kwargs):
|
||||
if pydantic.__version__ < "1.8.2":
|
||||
return self.dict(by_alias=True)
|
||||
return self.dict(*args, **kwargs)
|
||||
else:
|
||||
return self.model_dump(by_alias=True)
|
||||
return self.model_dump(*args, **kwargs)
|
||||
|
||||
|
||||
class Database:
|
||||
@ -30,7 +30,7 @@ class Database:
|
||||
self.conn = sqlite3.connect(db_name)
|
||||
self.cursor = self.conn.cursor()
|
||||
|
||||
def first(self, model: LiteModel, condition: str, *args: Any, default: Any = None) -> LiteModel | Any | None:
|
||||
def first(self, model: LiteModel, condition: str = "", *args: Any, default: Any = None) -> LiteModel | Any | None:
|
||||
"""查询第一个
|
||||
Args:
|
||||
model: 数据模型实例
|
||||
|
@ -43,9 +43,14 @@ class GlobalPlugin(LiteModel):
|
||||
enabled: bool = Field(True, alias="enabled")
|
||||
|
||||
|
||||
class StoredConfig(LiteModel):
|
||||
TABLE_NAME = "stored_config"
|
||||
config: dict = {}
|
||||
|
||||
|
||||
def auto_migrate():
|
||||
print("Migrating databases...")
|
||||
user_db.auto_migrate(User())
|
||||
group_db.auto_migrate(Group())
|
||||
plugin_db.auto_migrate(InstalledPlugin(), GlobalPlugin())
|
||||
common_db.auto_migrate(GlobalPlugin())
|
||||
common_db.auto_migrate(GlobalPlugin(), StoredConfig())
|
||||
|
||||
|
@ -8,7 +8,7 @@ from .ly_typing import T_Bot, T_MessageEvent
|
||||
|
||||
async def send_markdown(markdown: str, bot: T_Bot, *, message_type: str = None, session_id: str | int = None, event: T_MessageEvent = None, **kwargs) -> dict[
|
||||
str, Any]:
|
||||
formatted_md = v11.unescape(markdown).replace("\n", r"\n").replace("\"", r'\\\"')
|
||||
formatted_md = v11.unescape(markdown).replace("\n", r"\n").replace('"', r'\\\"')
|
||||
if event is not None and message_type is None:
|
||||
message_type = event.message_type
|
||||
session_id = event.user_id if event.message_type == "private" else event.group_id
|
||||
|
Reference in New Issue
Block a user