1
0
forked from bot/app
LiteyukiBot/liteyuki/liteyuki_main/core.py

124 lines
3.7 KiB
Python
Raw Normal View History

2024-03-29 14:58:24 +08:00
import json
from typing import Any
import aiofiles
import yaml
2024-03-24 22:04:51 +08:00
from nonebot import require
from nonebot.permission import SUPERUSER
2024-03-27 00:05:51 +08:00
from git import Repo
2024-03-29 14:58:24 +08:00
from liteyuki.utils.config import config, load_from_yaml
2024-03-27 00:05:51 +08:00
from liteyuki.utils.ly_typing import T_Bot, T_MessageEvent
2024-03-27 07:57:04 +08:00
from liteyuki.utils.language import get_user_lang
from liteyuki.utils.message import Markdown as md, send_markdown
2024-03-27 00:05:51 +08:00
from .reloader import Reloader
2024-03-29 14:58:24 +08:00
from liteyuki.utils.data_manager import StoredConfig, common_db
2024-03-24 22:04:51 +08:00
require("nonebot_plugin_alconna")
2024-03-29 14:58:24 +08:00
from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand, Arparma
2024-03-24 22:04:51 +08:00
cmd_liteyuki = on_alconna(
Alconna(
"liteyuki"
),
permission=SUPERUSER
)
2024-03-27 00:05:51 +08:00
update_liteyuki = on_alconna(
Alconna(
["update-liteyuki", "更新轻雪"]
),
permission=SUPERUSER
)
reload_liteyuki = on_alconna(
Alconna(
2024-03-27 07:57:04 +08:00
["reload-liteyuki", "restart-liteyuki", "重启轻雪"]
2024-03-27 00:05:51 +08:00
),
permission=SUPERUSER
)
2024-03-29 14:58:24 +08:00
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):
await cmd_liteyuki.finish(f"Hello, Liteyuki!\nBot {bot.self_id}\nLiteyukiID {config.get('liteyuki_id', 'No')}")
2024-03-27 00:05:51 +08:00
@update_liteyuki.handle()
async def _(bot: T_Bot, event: T_MessageEvent):
# 使用git pull更新
2024-03-27 07:57:04 +08:00
ulang = get_user_lang(str(event.user_id))
2024-03-27 00:05:51 +08:00
origins = ["origin", "origin2"]
repo = Repo(".")
for origin in origins:
try:
repo.remotes[origin].pull()
break
except Exception as e:
print(f"Pull from {origin} failed: {e}")
2024-03-27 23:51:35 +08:00
logs = repo.index
2024-03-27 00:05:51 +08:00
reply = "Liteyuki updated!\n"
2024-03-27 07:57:04 +08:00
reply += f"```\n{logs}\n```\n"
2024-03-27 07:58:14 +08:00
btn_restart = md.button(ulang.get("liteyuki.restart_now"), "restart-liteyuki")
2024-03-27 07:57:04 +08:00
reply += f"{ulang.get('liteyuki.update_restart', RESTART=btn_restart)}"
2024-03-27 00:05:51 +08:00
await send_markdown(reply, bot, event=event, at_sender=False)
@reload_liteyuki.handle()
async def _():
await reload_liteyuki.send("Liteyuki reloading")
Reloader.reload(3)
2024-03-29 14:58:24 +08:00
@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)