@sliverp/qqbot 1.3.1 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/qqbot-cli.js +227 -0
- package/package.json +4 -1
- package/qqbot-1.3.0.tgz +0 -0
package/bin/qqbot-cli.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* QQBot CLI - 用于升级和管理 QQBot 插件
|
|
5
|
+
*
|
|
6
|
+
* 用法:
|
|
7
|
+
* npx @sliverp/qqbot upgrade # 升级插件
|
|
8
|
+
* npx @sliverp/qqbot install # 安装插件
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { execSync } from 'child_process';
|
|
12
|
+
import { existsSync, readFileSync, writeFileSync, rmSync } from 'fs';
|
|
13
|
+
import { homedir } from 'os';
|
|
14
|
+
import { join, dirname } from 'path';
|
|
15
|
+
import { fileURLToPath } from 'url';
|
|
16
|
+
|
|
17
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
18
|
+
const __dirname = dirname(__filename);
|
|
19
|
+
|
|
20
|
+
// 获取包的根目录
|
|
21
|
+
const PKG_ROOT = join(__dirname, '..');
|
|
22
|
+
|
|
23
|
+
const args = process.argv.slice(2);
|
|
24
|
+
const command = args[0];
|
|
25
|
+
|
|
26
|
+
// 检测使用的是 clawdbot 还是 openclaw
|
|
27
|
+
function detectInstallation() {
|
|
28
|
+
const home = homedir();
|
|
29
|
+
if (existsSync(join(home, '.openclaw'))) {
|
|
30
|
+
return 'openclaw';
|
|
31
|
+
}
|
|
32
|
+
if (existsSync(join(home, '.clawdbot'))) {
|
|
33
|
+
return 'clawdbot';
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 清理旧版本插件,返回旧的 qqbot 配置
|
|
39
|
+
function cleanupInstallation(appName) {
|
|
40
|
+
const home = homedir();
|
|
41
|
+
const appDir = join(home, `.${appName}`);
|
|
42
|
+
const configFile = join(appDir, `${appName}.json`);
|
|
43
|
+
const extensionDir = join(appDir, 'extensions', 'qqbot');
|
|
44
|
+
|
|
45
|
+
let oldQqbotConfig = null;
|
|
46
|
+
|
|
47
|
+
console.log(`\n>>> 处理 ${appName} 安装...`);
|
|
48
|
+
|
|
49
|
+
// 1. 先读取旧的 qqbot 配置
|
|
50
|
+
if (existsSync(configFile)) {
|
|
51
|
+
try {
|
|
52
|
+
const config = JSON.parse(readFileSync(configFile, 'utf8'));
|
|
53
|
+
if (config.channels?.qqbot) {
|
|
54
|
+
oldQqbotConfig = { ...config.channels.qqbot };
|
|
55
|
+
console.log('已保存旧的 qqbot 配置');
|
|
56
|
+
}
|
|
57
|
+
} catch (err) {
|
|
58
|
+
console.error('读取配置文件失败:', err.message);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 2. 删除旧的扩展目录
|
|
63
|
+
if (existsSync(extensionDir)) {
|
|
64
|
+
console.log(`删除旧版本插件: ${extensionDir}`);
|
|
65
|
+
rmSync(extensionDir, { recursive: true, force: true });
|
|
66
|
+
} else {
|
|
67
|
+
console.log('未找到旧版本插件目录,跳过删除');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 3. 清理配置文件中的 qqbot 相关字段
|
|
71
|
+
if (existsSync(configFile)) {
|
|
72
|
+
console.log('清理配置文件中的 qqbot 字段...');
|
|
73
|
+
try {
|
|
74
|
+
const config = JSON.parse(readFileSync(configFile, 'utf8'));
|
|
75
|
+
|
|
76
|
+
// 删除 channels.qqbot
|
|
77
|
+
if (config.channels?.qqbot) {
|
|
78
|
+
delete config.channels.qqbot;
|
|
79
|
+
console.log(' - 已删除 channels.qqbot');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 删除 plugins.entries.qqbot
|
|
83
|
+
if (config.plugins?.entries?.qqbot) {
|
|
84
|
+
delete config.plugins.entries.qqbot;
|
|
85
|
+
console.log(' - 已删除 plugins.entries.qqbot');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 删除 plugins.installs.qqbot
|
|
89
|
+
if (config.plugins?.installs?.qqbot) {
|
|
90
|
+
delete config.plugins.installs.qqbot;
|
|
91
|
+
console.log(' - 已删除 plugins.installs.qqbot');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
writeFileSync(configFile, JSON.stringify(config, null, 2));
|
|
95
|
+
console.log('配置文件已更新');
|
|
96
|
+
} catch (err) {
|
|
97
|
+
console.error('清理配置文件失败:', err.message);
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
console.log(`未找到配置文件: ${configFile}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return oldQqbotConfig;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 执行命令并继承 stdio
|
|
107
|
+
function runCommand(cmd, args = []) {
|
|
108
|
+
try {
|
|
109
|
+
execSync([cmd, ...args].join(' '), { stdio: 'inherit' });
|
|
110
|
+
return true;
|
|
111
|
+
} catch (err) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 升级命令
|
|
117
|
+
function upgrade() {
|
|
118
|
+
console.log('=== QQBot 插件升级脚本 ===');
|
|
119
|
+
|
|
120
|
+
let foundInstallation = null;
|
|
121
|
+
let savedConfig = null;
|
|
122
|
+
const home = homedir();
|
|
123
|
+
|
|
124
|
+
// 检查 openclaw
|
|
125
|
+
if (existsSync(join(home, '.openclaw'))) {
|
|
126
|
+
savedConfig = cleanupInstallation('openclaw');
|
|
127
|
+
foundInstallation = 'openclaw';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// 检查 clawdbot
|
|
131
|
+
if (existsSync(join(home, '.clawdbot'))) {
|
|
132
|
+
const clawdbotConfig = cleanupInstallation('clawdbot');
|
|
133
|
+
if (!savedConfig) savedConfig = clawdbotConfig;
|
|
134
|
+
foundInstallation = 'clawdbot';
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (!foundInstallation) {
|
|
138
|
+
console.log('\n未找到 clawdbot 或 openclaw 安装目录');
|
|
139
|
+
console.log('请确认已安装 clawdbot 或 openclaw');
|
|
140
|
+
process.exit(1);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
console.log('\n=== 清理完成 ===');
|
|
144
|
+
|
|
145
|
+
// 自动安装插件
|
|
146
|
+
console.log('\n[1/2] 安装新版本插件...');
|
|
147
|
+
runCommand(foundInstallation, ['plugins', 'install', '@sliverp/qqbot']);
|
|
148
|
+
|
|
149
|
+
// 自动配置通道(使用保存的 appId 和 clientSecret)
|
|
150
|
+
console.log('\n[2/2] 配置机器人通道...');
|
|
151
|
+
if (savedConfig?.appId && savedConfig?.clientSecret) {
|
|
152
|
+
const token = `${savedConfig.appId}:${savedConfig.clientSecret}`;
|
|
153
|
+
console.log(`使用已保存的配置: appId=${savedConfig.appId}`);
|
|
154
|
+
runCommand(foundInstallation, ['channels', 'add', '--channel', 'qqbot', '--token', `"${token}"`]);
|
|
155
|
+
|
|
156
|
+
// 恢复其他配置项(如 markdownSupport)
|
|
157
|
+
if (savedConfig.markdownSupport !== undefined) {
|
|
158
|
+
runCommand(foundInstallation, ['config', 'set', 'channels.qqbot.markdownSupport', String(savedConfig.markdownSupport)]);
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
console.log('未找到已保存的 qqbot 配置,请手动配置:');
|
|
162
|
+
console.log(` ${foundInstallation} channels add --channel qqbot --token "AppID:AppSecret"`);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
console.log('\n=== 升级完成 ===');
|
|
167
|
+
console.log(`\n可以运行以下命令启动机器人:`);
|
|
168
|
+
console.log(` ${foundInstallation} gateway --verbose`);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 安装命令
|
|
172
|
+
function install() {
|
|
173
|
+
console.log('=== QQBot 插件安装 ===');
|
|
174
|
+
|
|
175
|
+
const cmd = detectInstallation();
|
|
176
|
+
if (!cmd) {
|
|
177
|
+
console.log('未找到 clawdbot 或 openclaw 安装');
|
|
178
|
+
console.log('请先安装 openclaw 或 clawdbot');
|
|
179
|
+
process.exit(1);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
console.log(`\n使用 ${cmd} 安装插件...`);
|
|
183
|
+
runCommand(cmd, ['plugins', 'install', '@sliverp/qqbot']);
|
|
184
|
+
|
|
185
|
+
console.log('\n=== 安装完成 ===');
|
|
186
|
+
console.log('\n请配置机器人通道:');
|
|
187
|
+
console.log(` ${cmd} channels add --channel qqbot --token "AppID:AppSecret"`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// 显示帮助
|
|
191
|
+
function showHelp() {
|
|
192
|
+
console.log(`
|
|
193
|
+
QQBot CLI - QQ机器人插件管理工具
|
|
194
|
+
|
|
195
|
+
用法:
|
|
196
|
+
npx @sliverp/qqbot <命令>
|
|
197
|
+
|
|
198
|
+
命令:
|
|
199
|
+
upgrade 清理旧版本插件(升级前执行)
|
|
200
|
+
install 安装插件到 openclaw/clawdbot
|
|
201
|
+
|
|
202
|
+
示例:
|
|
203
|
+
npx @sliverp/qqbot upgrade
|
|
204
|
+
npx @sliverp/qqbot install
|
|
205
|
+
`);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// 主入口
|
|
209
|
+
switch (command) {
|
|
210
|
+
case 'upgrade':
|
|
211
|
+
upgrade();
|
|
212
|
+
break;
|
|
213
|
+
case 'install':
|
|
214
|
+
install();
|
|
215
|
+
break;
|
|
216
|
+
case '-h':
|
|
217
|
+
case '--help':
|
|
218
|
+
case 'help':
|
|
219
|
+
showHelp();
|
|
220
|
+
break;
|
|
221
|
+
default:
|
|
222
|
+
if (command) {
|
|
223
|
+
console.log(`未知命令: ${command}`);
|
|
224
|
+
}
|
|
225
|
+
showHelp();
|
|
226
|
+
process.exit(command ? 1 : 0);
|
|
227
|
+
}
|
package/package.json
CHANGED
package/qqbot-1.3.0.tgz
DELETED
|
Binary file
|