mirror of
https://github.com/LiteyukiStudio/LiteyukiBot.git
synced 2025-07-28 09:11:20 +00:00
docs: 添加了命令手册
This commit is contained in:
@ -1,8 +1,12 @@
|
||||
from nonebot import require
|
||||
from nonebot.permission import SUPERUSER
|
||||
from git import Repo
|
||||
|
||||
from liteyuki.utils.config import config
|
||||
from liteyuki.utils.ly_typing import T_Bot
|
||||
from liteyuki.utils.ly_typing import T_Bot, T_MessageEvent
|
||||
|
||||
from .reloader import Reloader
|
||||
from ..utils.message import send_markdown
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
from nonebot_plugin_alconna import on_alconna, Alconna
|
||||
@ -14,7 +18,46 @@ cmd_liteyuki = on_alconna(
|
||||
permission=SUPERUSER
|
||||
)
|
||||
|
||||
update_liteyuki = on_alconna(
|
||||
Alconna(
|
||||
["update-liteyuki", "更新轻雪"]
|
||||
),
|
||||
permission=SUPERUSER
|
||||
)
|
||||
|
||||
reload_liteyuki = on_alconna(
|
||||
Alconna(
|
||||
["reload-liteyuki", "重启轻雪"]
|
||||
),
|
||||
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')}")
|
||||
|
||||
|
||||
@update_liteyuki.handle()
|
||||
async def _(bot: T_Bot, event: T_MessageEvent):
|
||||
# 使用git pull更新
|
||||
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}")
|
||||
logs = repo.git.log('--pretty=format:%H %s')
|
||||
reply = "Liteyuki updated!\n"
|
||||
reply += f"```\n{logs}\n```"
|
||||
print(list(repo.iter_commits()))
|
||||
|
||||
await send_markdown(reply, bot, event=event, at_sender=False)
|
||||
|
||||
|
||||
@reload_liteyuki.handle()
|
||||
async def _():
|
||||
await reload_liteyuki.send("Liteyuki reloading")
|
||||
Reloader.reload(3)
|
||||
|
67
liteyuki/liteyuki_main/reloader.py
Normal file
67
liteyuki/liteyuki_main/reloader.py
Normal file
@ -0,0 +1,67 @@
|
||||
import threading
|
||||
from multiprocessing import get_context
|
||||
|
||||
import nonebot
|
||||
from nonebot import logger
|
||||
from typing import List, Optional
|
||||
|
||||
from nonebot import get_driver
|
||||
from pydantic import BaseSettings
|
||||
|
||||
|
||||
reboot_grace_time_limit: int = 20
|
||||
|
||||
_nb_run = nonebot.run
|
||||
|
||||
|
||||
class Reloader:
|
||||
event: threading.Event = None
|
||||
|
||||
@classmethod
|
||||
def reload(cls, delay: int = 0):
|
||||
if cls.event is None:
|
||||
raise RuntimeError()
|
||||
if delay > 0:
|
||||
threading.Timer(delay, function=cls.event.set).start()
|
||||
return
|
||||
cls.event.set()
|
||||
|
||||
|
||||
def _run(ev: threading.Event, *args, **kwargs):
|
||||
Reloader.event = ev
|
||||
_nb_run(*args, **kwargs)
|
||||
|
||||
|
||||
def run(*args, **kwargs):
|
||||
should_exit = False
|
||||
ctx = get_context("spawn")
|
||||
while not should_exit:
|
||||
event = ctx.Event()
|
||||
process = ctx.Process(
|
||||
target=_run,
|
||||
args=(
|
||||
event,
|
||||
*args,
|
||||
),
|
||||
kwargs=kwargs,
|
||||
)
|
||||
process.start()
|
||||
while not should_exit:
|
||||
if event.wait(1):
|
||||
logger.info("Receive reboot event")
|
||||
process.terminate()
|
||||
process.join(reboot_grace_time_limit)
|
||||
if process.is_alive():
|
||||
logger.warning(
|
||||
f"Cannot shutdown gracefully in {reboot_grace_time_limit} second, force kill process."
|
||||
)
|
||||
process.kill()
|
||||
break
|
||||
elif process.is_alive():
|
||||
continue
|
||||
else:
|
||||
# Process stoped without setting event
|
||||
should_exit = True
|
||||
|
||||
|
||||
nonebot.run = run
|
Reference in New Issue
Block a user