-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
120 lines (103 loc) · 3.84 KB
/
Copy pathmain.py
File metadata and controls
120 lines (103 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from . import sv, log, Port, SAMPLE, BOTNAME
import json
import os
from pathlib import Path
import subprocess
import sys
from hoshino.typing import CQEvent
from hoshino import priv
from nonebot import get_bot
from nonebot import on_websocket_connect
@on_websocket_connect
async def start_up(ev: CQEvent):
with open(SAMPLE, 'r', encoding='utf-8') as f:
data = json.loads(f.read())
bot = get_bot()
try:
with open(SAMPLE, 'r', encoding='utf-8') as f:
data = json.load(f)
if not isinstance(data, dict):
data = {}
except (FileNotFoundError, json.JSONDecodeError):
data = {}
try:
if data["message_id"] != 0:
await bot.delete_msg(message_id=data["message_id"])
data["message_id"] = 0
with open(SAMPLE, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
except Exception as e:
log.error("撤回失败")
try:
if data["reboot"] == "True":
group_id = data["group_id"]
await bot.send_group_msg(group_id=group_id, message=(f"[{BOTNAME} 启动成功]"))
data["reboot"] = "False"
with open(SAMPLE, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
except Exception as e:
log.error(f"发送失败:{e}")
@sv.on_prefix('/sudo')
async def sudo(bot, ev: CQEvent):
if not priv.check_priv(ev, priv.SUPERUSER):
try:
await bot.delete_msg(message_id=ev.message_id)
return
except Exception as e:
return
args = str(ev.message).split()
try:
with open(SAMPLE, 'r', encoding='utf-8') as f:
data = json.load(f)
if not isinstance(data, dict):
data = {}
except (FileNotFoundError, json.JSONDecodeError):
data = {}
# 重启相关
if args[0] == "reboot":
msg_id = await bot.send(ev, f"[{BOTNAME} 重启中...]")
data["message_id"] = msg_id.get('message_id', None)
data["reboot"] = "True"
data["group_id"] = int(ev.group_id)
with open(SAMPLE, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
reboot_script = Path(__file__).parent / "libraries" / "reboot_helper.py"
python = sys.executable
Hoshino_ROOT = Path(__file__).parent.parent.parent.parent
run_path = str(Hoshino_ROOT / "run.py")
if not reboot_script.exists():
await bot.send(ev, "重启脚本未找到,重启失败")
log.info("重启脚本未找到:", reboot_script)
return
reboot_command = [
python,
str(reboot_script),
run_path,
str(Port)
]
try:
# Windows
subprocess.Popen(reboot_command, creationflags=subprocess.CREATE_NEW_CONSOLE)
log.info(f"重启脚本已启动: {' '.join(reboot_command)}")
os._exit(0)
except AttributeError:
# Linux 悲报 还是炸了
subprocess.Popen(reboot_command)
log.info(f"重启脚本已启动: {' '.join(reboot_command)}")
os._exit(0)
except Exception as e:
log.info(f"启动重启脚本失败: {e}")
data["reboot"] = "False"
data["group_id"] = 0
with open(SAMPLE, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
await bot.send(ev, f"{BOTNAME} 重启失败")
return
# 死活检测
elif args[0] == "check":
msg = '[BOT运行中......]'
try:
await bot.finish(ev, message=msg)
except Exception as e:
log.info("挂了!!!")
return