🎨 重启代码结构优化 (#1737)

This commit is contained in:
HibiKier 2024-11-22 10:27:10 +08:00 committed by GitHub
parent 4e4b4590c4
commit 857999db07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,25 +3,26 @@ import platform
from pathlib import Path from pathlib import Path
import nonebot import nonebot
import aiofiles
from nonebot import on_command from nonebot import on_command
from nonebot.rule import to_me
from nonebot.adapters import Bot from nonebot.adapters import Bot
from nonebot.params import ArgStr from nonebot.params import ArgStr
from nonebot.permission import SUPERUSER from nonebot.permission import SUPERUSER
from nonebot_plugin_uninfo import Uninfo
from nonebot.plugin import PluginMetadata from nonebot.plugin import PluginMetadata
from nonebot.rule import to_me
from nonebot_plugin_session import EventSession
from zhenxun.configs.config import BotConfig
from zhenxun.configs.utils import PluginExtraData
from zhenxun.services.log import logger from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType from zhenxun.utils.enum import PluginType
from zhenxun.configs.config import BotConfig
from zhenxun.utils.message import MessageUtils from zhenxun.utils.message import MessageUtils
from zhenxun.utils.platform import PlatformUtils from zhenxun.utils.platform import PlatformUtils
from zhenxun.configs.utils import PluginExtraData
__plugin_meta__ = PluginMetadata( __plugin_meta__ = PluginMetadata(
name="重启", name="重启",
description="执行脚本重启真寻", description="执行脚本重启真寻",
usage=f""" usage="""
重启 重启
""".strip(), """.strip(),
extra=PluginExtraData( extra=PluginExtraData(
@ -48,15 +49,15 @@ RESTART_FILE = Path() / "restart.sh"
@_matcher.got( @_matcher.got(
"flag", "flag",
prompt=f"确定是否重启{BotConfig.self_nickname}确定请回复[是|好|确定](重启失败咱们将失去联系,请谨慎!)", prompt=f"确定是否重启{BotConfig.self_nickname}\n确定请回复[是|好|确定]\n(重启失败咱们将失去联系,请谨慎!)",
) )
async def _(bot: Bot, session: EventSession, flag: str = ArgStr("flag")): async def _(bot: Bot, session: Uninfo, flag: str = ArgStr("flag")):
if flag.lower() in ["true", "", "", "确定", "确定是"]: if flag.lower() in {"true", "", "", "确定", "确定是"}:
await MessageUtils.build_message( await MessageUtils.build_message(
f"开始重启{BotConfig.self_nickname}..请稍等..." f"开始重启{BotConfig.self_nickname}..请稍等..."
).send() ).send()
with open(RESTART_MARK, "w", encoding="utf8") as f: async with aiofiles.open(RESTART_MARK, "w", encoding="utf8") as f:
f.write(f"{bot.self_id} {session.id1}") await f.write(f"{bot.self_id} {session.user.id}")
logger.info("开始重启真寻...", "重启", session=session) logger.info("开始重启真寻...", "重启", session=session)
if str(platform.system()).lower() == "windows": if str(platform.system()).lower() == "windows":
import sys import sys
@ -64,18 +65,17 @@ async def _(bot: Bot, session: EventSession, flag: str = ArgStr("flag")):
python = sys.executable python = sys.executable
os.execl(python, python, *sys.argv) os.execl(python, python, *sys.argv)
else: else:
os.system("./restart.sh") os.system("./restart.sh") # noqa: ASYNC221
else: else:
await MessageUtils.build_message("已取消操作...").send() await MessageUtils.build_message("已取消操作...").send()
@driver.on_bot_connect @driver.on_bot_connect
async def _(bot: Bot): async def _(bot: Bot):
if str(platform.system()).lower() != "windows": if str(platform.system()).lower() != "windows" and not RESTART_FILE.exists():
if not RESTART_FILE.exists(): async with aiofiles.open(RESTART_FILE, "w", encoding="utf8") as f:
with open(RESTART_FILE, "w", encoding="utf8") as f: await f.write(
f.write( "pid=$(netstat -tunlp | grep "
f"pid=$(netstat -tunlp | grep "
+ str(bot.config.port) + str(bot.config.port)
+ " | awk '{print $7}')\n" + " | awk '{print $7}')\n"
"pid=${pid%/*}\n" "pid=${pid%/*}\n"
@ -83,15 +83,13 @@ async def _(bot: Bot):
"sleep 3\n" "sleep 3\n"
"python3 bot.py" "python3 bot.py"
) )
os.system("chmod +x ./restart.sh") os.system("chmod +x ./restart.sh") # noqa: ASYNC221
logger.info( logger.info("已自动生成 restart.sh(重启) 文件,请检查脚本是否与本地指令符合...")
"已自动生成 restart.sh(重启) 文件,请检查脚本是否与本地指令符合..."
)
if RESTART_MARK.exists(): if RESTART_MARK.exists():
with open(RESTART_MARK, "r", encoding="utf8") as f: async with aiofiles.open(RESTART_MARK, encoding="utf8") as f:
bot_id, session_id = f.read().split() bot_id, user_id = (await f.read()).split()
if bot := nonebot.get_bot(bot_id): if bot := nonebot.get_bot(bot_id):
if target := PlatformUtils.get_target(bot, session_id): if target := PlatformUtils.get_target(bot, user_id):
await MessageUtils.build_message( await MessageUtils.build_message(
f"{BotConfig.self_nickname}已成功重启!" f"{BotConfig.self_nickname}已成功重启!"
).send(target, bot=bot) ).send(target, bot=bot)