-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
117 lines (105 loc) · 3.59 KB
/
index.py
File metadata and controls
117 lines (105 loc) · 3.59 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
import os
import sys
import asyncio
import argparse
import logging
from typing import Optional, Dict
from ymbotpy import logging as botpy_logging
# 本地模块导入
import libs.main as BotMain
import libs.audit as BotAudit
_log = botpy_logging.get_logger()
CONFIG_TEMPLATE = '''"""
机器人配置文件 (自动生成)
"""
APPID = "{appid}"
SECRET = "{secret}"
AUDIT = {audit}
WSKEY = {wskey}
'''
def load_config(config_path: str) -> Optional[Dict]:
"""加载配置文件"""
try:
from config import APPID, SECRET, AUDIT,WSKEY
return {
'APPID': APPID,
'SECRET': SECRET,
'AUDIT': AUDIT,
'WSKEY': WSKEY
}
except ImportError as e:
_log.error(f"配置加载失败: {str(e)}")
return None
except Exception as e:
_log.error(f"配置文件格式错误: {str(e)}")
return None
def save_config(config_path: str, appid: str, secret: str, audit: bool, wskey: str):
"""保存配置文件"""
try:
with open(config_path, 'w', encoding='utf-8') as f:
f.write(CONFIG_TEMPLATE.format(
appid=appid,
secret=secret,
audit='True' if audit else 'False',
wskey=wskey
))
_log.info("配置文件已更新")
except IOError as e:
_log.error(f"配置文件保存失败: {str(e)}")
sys.exit(1)
def parse_args():
"""解析命令行参数"""
parser = argparse.ArgumentParser(description='QQ机器人启动程序')
parser.add_argument('--webhook', action='store_true',
help='启用Webhook模式')
parser.add_argument('--sandbox', action='store_true',
help='强制启用沙箱模式')
return parser.parse_args()
async def run_main(appid: str, secret: str, wskey:str,sandbox: bool, webhook: bool):
"""主运行逻辑"""
try:
if webhook:
_log.info("以Webhook模式启动...")
else:
_log.info("以Websocket模式启动...")
await BotMain.main(appid, secret,wskey, sandbox,webhook)
except KeyboardInterrupt:
_log.info("程序已手动终止")
except Exception as e:
_log.error(f"运行时发生严重错误: {str(e)}")
sys.exit(1)
def interactive_setup(config_path: str):
"""交互式配置向导"""
print("\n== 首次配置向导 ==")
appid = input("请输入AppID(机器人ID): ").strip()
secret = input("请输入AppSecret(机器人密钥): ").strip()
wskey = input("请输入WebSocketKey(主控密钥): ").strip()
while True:
audit = input("机器人是否已通过审核?(y/n): ").lower()
if audit in ('y', 'n'):
save_config(config_path, appid, secret, audit == 'y', wskey)
return
print("请输入 y 或 n")
if __name__ == '__main__':
args = parse_args()
config_path = os.path.join(os.getcwd(), 'config.py')
# 配置检查与初始化
if not os.path.isfile(config_path):
interactive_setup(config_path)
sys.exit("请重新启动程序以应用配置")
config = load_config(config_path)
if not config:
sys.exit("配置文件加载失败,请检查config.py格式")
# 模式判断逻辑
if config['AUDIT'] or args.sandbox:
sandbox = args.sandbox if args.sandbox else False
asyncio.run(run_main(
config['APPID'],
config['SECRET'],
config['WSKEY'],
sandbox,
args.webhook
))
else:
# 审核模式处理流程
BotAudit.main(config['APPID'], config['SECRET'])