dsh-mc-server-ops 0.6.1 → 0.6.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/README.md +20 -1
- package/client.js +29 -1
- package/index.js +78 -1
- package/ops/qq-bridge.mjs +1 -1
- package/ops/qqbot-hook.mjs +113 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
- **授权**:动受保护区域(world、ops\data、白名单)要 2 位群友投票;**凑满 2 票之前任何人反对就作废**
|
|
38
38
|
- **人设**:默认「小铜」,名字和语气都能在设置里改
|
|
39
39
|
- **游戏内 AI**:装上配套 Forge mod 后,玩家在游戏里 `/ai <内容>` 就能和「小铜」对话(只监听 `127.0.0.1`,按玩家 UUID 复用独立会话);mod 1.1.0 起还能带截图/文件附件
|
|
40
|
+
- **天气投票**:配套 mod 1.2.0 起,游戏内玩家可用 `/weather` 改天气;在线 ≤2 人直接生效,3 人及以上要 `/vote yes` 投票(2 票同意通过),门槛都能在设置面板改
|
|
40
41
|
|
|
41
42
|
## 安装
|
|
42
43
|
|
|
@@ -110,6 +111,11 @@ dsh plugin add /path/to/dsh-mc-server-ops
|
|
|
110
111
|
| `ai.enabled` | `true` | 是否开游戏内 `/ai` 对话(需要配套 Forge mod;只监听 `127.0.0.1`) |
|
|
111
112
|
| `ai.port` | `8765` | mod 连接的本地端口 |
|
|
112
113
|
| `ai.timeoutSeconds` | `60` | 等 AI 回复的超时秒数 |
|
|
114
|
+
| `weather.enabled` | `true` | 允许游戏内玩家用 `/weather` 改天气(需要 mcai mod 1.2.0+) |
|
|
115
|
+
| `weather.directThreshold` | `2` | 在线 ≤ 这个数时直接改天气;更多人才发起投票 |
|
|
116
|
+
| `weather.approveNeed` / `weather.rejectNeed` | `2` / `1` | 通过需要几票同意 / 几票反对作废(发起人自动算 1 票同意) |
|
|
117
|
+
| `weather.timeoutSeconds` | `60` | 投票有效期(秒) |
|
|
118
|
+
| `weather.cooldownSeconds` | `60` | 同一玩家两次发起的最小间隔(秒) |
|
|
113
119
|
|
|
114
120
|
## 目录结构
|
|
115
121
|
|
|
@@ -240,7 +246,9 @@ powershell -File <服务器目录>\ops\mcadmin.ps1 -Action owner-add -Arg <openi
|
|
|
240
246
|
|
|
241
247
|
配置在设置面板的「群服互通」里,**改完立刻生效**(不用重启),状态行会显示「运行中 / 在线几人 / 今日已发几条 / 错误原因」。
|
|
242
248
|
|
|
243
|
-
群 openid
|
|
249
|
+
群 openid 不知道怎么办:**第一次不用填**。插件加载时会自动给本机 qqbot 的 `inbound.js` 打一个幂等抓取钩子(带标记、先备份、失败不影响 qqbot),机器人收到群消息时会把群 openid 写进 `ops/data/qq-keys.txt`;群友在群里 **@ 一次机器人**,群服互通就会自动用上这个群,不需要手填。详细说明见 [docs/qq-bridge.md](https://github.com/mogutofu/dsh-mc-server-ops/blob/main/docs/qq-bridge.md)。
|
|
250
|
+
|
|
251
|
+
如果 qqbot 升级把钩子冲掉了:插件下次加载会自动重打;实在不行也可以手动填 `bridge.groups`(`mc_server bridge-groups` 会给出本地记录过的群 openid)。
|
|
244
252
|
|
|
245
253
|
插件会自动记住用过的群:正常转发过一次后,群 openid 会存到 `ops/data/bridge-groups.json`;`bridge.groups` 留空或因为重装插件被重置时,自动用上次记住的群,再退回最近捕获的群(`ops/data/qq-keys.txt`),所以**重装/升级插件不会再让群服互通断掉**。状态和日志里会标出来源(`config` / `remembered` / `captured`)。
|
|
246
254
|
|
|
@@ -272,6 +280,17 @@ powershell -File <服务器目录>\ops\mcadmin.ps1 -Action owner-add -Arg <openi
|
|
|
272
280
|
|
|
273
281
|
游戏内玩家和群友一样受权限守卫限制:只读查询可以,写文件、执行命令需要服主授权;mod 侧默认每玩家 5 秒冷却,避免刷屏。
|
|
274
282
|
|
|
283
|
+
## 游戏内天气投票(/weather)
|
|
284
|
+
|
|
285
|
+
配套 Forge mod **mcai 1.2.0+**:游戏内所有玩家都能用 `/weather clear|rain|thunder [持续秒数]` 改天气(mod 会把原版需要 OP 的 `/weather` 换成对所有人开放)。
|
|
286
|
+
|
|
287
|
+
- 在线玩家 **≤ `weather.directThreshold`(默认 2)**:直接生效,全服提示「XX 把天气改成了下雨」
|
|
288
|
+
- **3 人及以上**:发起投票,发起人自动算 1 票同意;其他人在游戏里用 `/vote yes` 同意、`/vote no` 反对、`/vote status` 查看
|
|
289
|
+
- 同意达到 `weather.approveNeed`(默认 2)通过;反对达到 `weather.rejectNeed`(默认 1)作废;`weather.timeoutSeconds`(默认 60 秒)没结果也会作废
|
|
290
|
+
- 同一玩家两次发起间隔 `weather.cooldownSeconds`(默认 60 秒)
|
|
291
|
+
- 配置写在 `<服务器目录>/config/mcai-weather.json`,mod 每次 `/weather` 都读它,所以**设置面板改完即时生效**,不用重启服务器
|
|
292
|
+
- 服务器装的是旧版 mod(< 1.2.0)时,这个配置不会生效,`/weather` 也还是原版需要 OP 的版本
|
|
293
|
+
|
|
275
294
|
## 安全模型
|
|
276
295
|
|
|
277
296
|
- 仓库和 npm 包里**不含任何口令、QQ 号、记忆数据或整合包**。
|
package/client.js
CHANGED
|
@@ -182,6 +182,14 @@ window.__ModuleLoader__.load({
|
|
|
182
182
|
? (v.vote || {}).protectedPaths.join(", ")
|
|
183
183
|
: "world, ops/data, whitelist",
|
|
184
184
|
},
|
|
185
|
+
weather: {
|
|
186
|
+
enabled: (v.weather || {}).enabled !== false,
|
|
187
|
+
directThreshold: (v.weather || {}).directThreshold ?? 2,
|
|
188
|
+
approveNeed: (v.weather || {}).approveNeed ?? 2,
|
|
189
|
+
rejectNeed: (v.weather || {}).rejectNeed ?? 1,
|
|
190
|
+
timeoutSeconds: (v.weather || {}).timeoutSeconds ?? 60,
|
|
191
|
+
cooldownSeconds: (v.weather || {}).cooldownSeconds ?? 60,
|
|
192
|
+
},
|
|
185
193
|
permissions: {
|
|
186
194
|
voteEscalation: (v.permissions || {}).voteEscalation !== false,
|
|
187
195
|
kubejsEdit: (v.permissions || {}).kubejsEdit !== false,
|
|
@@ -225,6 +233,9 @@ window.__ModuleLoader__.load({
|
|
|
225
233
|
bridge: Object.assign({}, prev.bridge, { coalesce: Object.assign({}, prev.bridge.coalesce, { [key]: value }) }),
|
|
226
234
|
}) : prev))
|
|
227
235
|
}
|
|
236
|
+
function patchWeather(key, value) {
|
|
237
|
+
setForm((prev) => (prev ? Object.assign({}, prev, { weather: Object.assign({}, prev.weather, { [key]: value }) }) : prev))
|
|
238
|
+
}
|
|
228
239
|
|
|
229
240
|
async function save() {
|
|
230
241
|
if (!form) return
|
|
@@ -395,6 +406,23 @@ window.__ModuleLoader__.load({
|
|
|
395
406
|
h(Field, { label: "受保护路径", hint: "逗号分隔,改动这些位置要走投票。默认 world, ops/data, whitelist" },
|
|
396
407
|
h(TextInput, { value: form.vote.protectedPaths, placeholder: "world, ops/data, whitelist", onChange: (x) => patch("vote", "protectedPaths", x) })),
|
|
397
408
|
),
|
|
409
|
+
h("div", null,
|
|
410
|
+
h("div", { style: titleStyle }, "游戏内天气投票"),
|
|
411
|
+
h("div", { style: hintStyle }, "玩家在游戏里用 /weather 改天气,在线人少直接生效、人多时 /vote 投票。配置写到服务器 config/mcai-weather.json,改完即时生效(需要 mcai mod 1.2.0+)。"),
|
|
412
|
+
),
|
|
413
|
+
h("div", { style: groupStyle },
|
|
414
|
+
h(Toggle, { label: "允许玩家改天气(/weather)", value: form.weather.enabled, onChange: (x) => patchWeather("enabled", x) }),
|
|
415
|
+
h(Field, { label: "在线不超过几人时直接生效", hint: "默认 2:0-2 人直接改;3 人及以上发起投票" },
|
|
416
|
+
h(NumberInput, { value: form.weather.directThreshold, onChange: (x) => patchWeather("directThreshold", x) })),
|
|
417
|
+
h(Field, { label: "投票需要几人同意", hint: "默认 2;发起人自动算 1 票同意" },
|
|
418
|
+
h(NumberInput, { value: form.weather.approveNeed, onChange: (x) => patchWeather("approveNeed", x) })),
|
|
419
|
+
h(Field, { label: "几人反对就作废", hint: "默认 1 = 一票否决" },
|
|
420
|
+
h(NumberInput, { value: form.weather.rejectNeed, onChange: (x) => patchWeather("rejectNeed", x) })),
|
|
421
|
+
h(Field, { label: "投票有效期(秒)", hint: "默认 60 秒;到点没结果自动作废" },
|
|
422
|
+
h(NumberInput, { value: form.weather.timeoutSeconds, onChange: (x) => patchWeather("timeoutSeconds", x) })),
|
|
423
|
+
h(Field, { label: "发起冷却(秒)", hint: "同一玩家两次 /weather 的最小间隔,默认 60 秒,0 = 不限" },
|
|
424
|
+
h(NumberInput, { value: form.weather.cooldownSeconds, onChange: (x) => patchWeather("cooldownSeconds", x) })),
|
|
425
|
+
),
|
|
398
426
|
h("div", null,
|
|
399
427
|
h("div", { style: titleStyle }, "群服互通"),
|
|
400
428
|
h("div", { style: hintStyle }, "把游戏内聊天直接转发到 QQ 群。走机器人自己的接口,不经过 AI、不消耗 token。"),
|
|
@@ -415,7 +443,7 @@ window.__ModuleLoader__.load({
|
|
|
415
443
|
h("option", { value: "auto" }, "有玩家时开启(默认)"),
|
|
416
444
|
h("option", { value: "on" }, "常开启"),
|
|
417
445
|
h("option", { value: "off" }, "常关闭"))),
|
|
418
|
-
h(Field, { label: "转发到哪些群", hint: "
|
|
446
|
+
h(Field, { label: "转发到哪些群", hint: "首次不用填:在群里 @ 一次小铜,插件会自动发现;留空 = 自动(上次记住 → 最近捕获),重装插件不会丢" },
|
|
419
447
|
h(TextInput, { value: form.bridge.groups, placeholder: "留空 = 自动(上次记住的群)", onChange: (x) => patchBridge("groups", x) })),
|
|
420
448
|
h(Field, { label: "除聊天外还转发", hint: "聊天始终转发。进服/退服默认关(会刷屏),死亡消息默认开" },
|
|
421
449
|
h("div", { style: { display: "flex", gap: 18, flexWrap: "wrap" } },
|
package/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import Schema from '@deepseek-ai/schemastery'
|
|
|
8
8
|
import { guardCommand } from './ops/command-guard.mjs'
|
|
9
9
|
import { createAdapter } from './ops/adapters/index.mjs'
|
|
10
10
|
import { createBridge, writeBridgeState, QqApi } from './ops/qq-bridge.mjs'
|
|
11
|
+
import { ensureQqbotInboundHook } from './ops/qqbot-hook.mjs'
|
|
11
12
|
|
|
12
13
|
// 运行期路径:apply() 里按配置初始化。服务器目录、脚本位置都可以在设置里改。
|
|
13
14
|
const paths = { serverDir: '', script: '', ownersFile: '', flipLog: '' }
|
|
@@ -378,6 +379,21 @@ export const Config = Schema.object({
|
|
|
378
379
|
.description('受保护路径:改动这些位置需要群友投票放行。默认 world, ops/data, whitelist'),
|
|
379
380
|
}).description('投票'),
|
|
380
381
|
|
|
382
|
+
weather: Schema.object({
|
|
383
|
+
enabled: Schema.boolean().default(true)
|
|
384
|
+
.description('允许游戏内玩家用 /weather 改天气(需要 mcai mod 1.2.0+)。配置会写到服务器 config/mcai-weather.json,改完即时生效'),
|
|
385
|
+
directThreshold: Schema.number().min(0).max(20).default(2)
|
|
386
|
+
.description('在线玩家不超过这个数时直接改天气、不投票(默认 2:0-2 人直接生效)'),
|
|
387
|
+
approveNeed: Schema.number().min(1).max(20).default(2)
|
|
388
|
+
.description('投票通过需要几人同意(发起人自动算 1 票;默认 2)'),
|
|
389
|
+
rejectNeed: Schema.number().min(1).max(20).default(1)
|
|
390
|
+
.description('几人反对就作废(默认 1 = 一票否决)'),
|
|
391
|
+
timeoutSeconds: Schema.number().min(5).max(3600).default(60)
|
|
392
|
+
.description('投票有效期(秒,默认 60)'),
|
|
393
|
+
cooldownSeconds: Schema.number().min(0).max(86400).default(60)
|
|
394
|
+
.description('同一玩家两次发起的最小间隔(秒,默认 60,0 = 不限)'),
|
|
395
|
+
}).description('游戏内天气投票(配套 mcai mod)'),
|
|
396
|
+
|
|
381
397
|
adapter: Schema.union(['local', 'panel']).default('local')
|
|
382
398
|
.description('写操作走哪条路:local=本机直接跑脚本(默认);panel=通过 Pterodactyl 面板的 HTTP API 启停/发指令'),
|
|
383
399
|
panel: Schema.object({
|
|
@@ -654,6 +670,23 @@ export function apply(ctx, config) {
|
|
|
654
670
|
paths.ownersFile = join(opsDir, 'data', 'owners.json')
|
|
655
671
|
paths.flipLog = join(opsDir, 'data', 'preset-flip.log')
|
|
656
672
|
|
|
673
|
+
// ── QQ 群 openid 自动发现 ──
|
|
674
|
+
// 给 qqbot 的 inbound.js 打幂等钩子:它收到群消息时把群 openid 追加到下面的文件,
|
|
675
|
+
// bridge 发现配置里没有群时会自动采用(来源:captured)。首次装插件不需要手填 openid,
|
|
676
|
+
// 群友在群里 @ 一次机器人即可。钩子写失败不影响任何功能,退回手动配置。
|
|
677
|
+
process.env.MC_OPS_QQ_KEYS_FILE = join(opsDir, 'data', 'qq-keys.txt')
|
|
678
|
+
try {
|
|
679
|
+
mkdirSync(join(opsDir, 'data'), { recursive: true })
|
|
680
|
+
ensureQqbotInboundHook({
|
|
681
|
+
log: (m) => {
|
|
682
|
+
try {
|
|
683
|
+
appendFileSync(join(opsDir, 'data', 'bridge.log'), new Date().toISOString() + ' [bridge] ' + m + '\n')
|
|
684
|
+
} catch (error) { }
|
|
685
|
+
},
|
|
686
|
+
})
|
|
687
|
+
} catch (error) { }
|
|
688
|
+
|
|
689
|
+
|
|
657
690
|
// 设置页:插件详情里渲染自己的表单(client.js),表单通过下面这两个 HTTP 路由读写配置。
|
|
658
691
|
const settingsService = ctx.get('settings')
|
|
659
692
|
if (settingsService && typeof settingsService.configure === 'function') {
|
|
@@ -697,13 +730,20 @@ export function apply(ctx, config) {
|
|
|
697
730
|
maxPerDay: 900,
|
|
698
731
|
}, (config && config.bridge) || {}),
|
|
699
732
|
ai: Object.assign({ enabled: true, port: 8765, timeoutSeconds: 60 }, (config && config.ai) || {}),
|
|
733
|
+
weather: Object.assign({
|
|
734
|
+
enabled: true, directThreshold: 2, approveNeed: 2, rejectNeed: 1,
|
|
735
|
+
timeoutSeconds: 60, cooldownSeconds: 60,
|
|
736
|
+
}, (config && config.weather) || {}),
|
|
700
737
|
}
|
|
738
|
+
// 启动时先把天气投票配置写到服务器 config/mcai-weather.json;mod 每次 /weather 都读这个文件。
|
|
739
|
+
writeWeatherConfig(serverDir, liveConfig.weather)
|
|
701
740
|
const memoryConfig = {
|
|
702
741
|
get: () => liveConfig,
|
|
703
742
|
set: (next) => {
|
|
704
743
|
liveConfig = Object.assign({}, liveConfig, next && typeof next === 'object' ? next : {})
|
|
705
744
|
writeVoteConfig(opsDir, liveConfig.vote)
|
|
706
745
|
setProtectedPaths(liveConfig.vote && liveConfig.vote.protectedPaths)
|
|
746
|
+
writeWeatherConfig(paths.serverDir, liveConfig.weather)
|
|
707
747
|
},
|
|
708
748
|
}
|
|
709
749
|
// 启动时先落一份,保证脚本读到的是当前设置
|
|
@@ -785,7 +825,8 @@ export function apply(ctx, config) {
|
|
|
785
825
|
if (key !== lastBridgeGroupsLog) {
|
|
786
826
|
lastBridgeGroupsLog = key
|
|
787
827
|
try {
|
|
788
|
-
|
|
828
|
+
const hint = bridgeGroupsState.source === 'none' ? ';未发现目标群:在 QQ 群里 @ 一次机器人即可自动发现,也可在设置里手填' : ''
|
|
829
|
+
appendFileSync(join(opsDir, 'data', 'bridge.log'), new Date().toISOString() + ' [bridge] 群目标:' + (bridgeGroupsState.groups.join(', ') || '(空)') + '(来源:' + bridgeGroupsState.source + ')' + hint + '\n')
|
|
789
830
|
} catch (error) { }
|
|
790
831
|
}
|
|
791
832
|
return bridgeGroupsState
|
|
@@ -817,6 +858,7 @@ export function apply(ctx, config) {
|
|
|
817
858
|
void bridge.start()
|
|
818
859
|
const tick = setInterval(() => {
|
|
819
860
|
resolveBridgeGroups((liveConfig && liveConfig.bridge) || {})
|
|
861
|
+
writeWeatherConfig(serverDir, liveConfig.weather)
|
|
820
862
|
writeBridgeState(serverDir, Object.assign(
|
|
821
863
|
{ appId: appId ? appId.slice(0, 3) + '***' : '', hasSecret: !!appSecret },
|
|
822
864
|
bridge.status(),
|
|
@@ -1454,6 +1496,7 @@ async function handleConfigRoute(req, res, services, fallbackConfig) {
|
|
|
1454
1496
|
if (desc) {
|
|
1455
1497
|
await settings.update(desc.ns, body, desc.revision)
|
|
1456
1498
|
fallbackConfig.set(body)
|
|
1499
|
+
writeWeatherConfig(paths.serverDir, (body && body.weather) || (fallbackConfig.get() || {}).weather)
|
|
1457
1500
|
return send(200, { ok: true, via: 'settings' })
|
|
1458
1501
|
}
|
|
1459
1502
|
if (editor && typeof editor.entries === 'function' && typeof editor.edit === 'function') {
|
|
@@ -1466,6 +1509,7 @@ async function handleConfigRoute(req, res, services, fallbackConfig) {
|
|
|
1466
1509
|
if (entry) {
|
|
1467
1510
|
await editor.edit(entry, (current) => Object.assign({}, current || {}, body))
|
|
1468
1511
|
fallbackConfig.set(body)
|
|
1512
|
+
writeWeatherConfig(paths.serverDir, (body && body.weather) || (fallbackConfig.get() || {}).weather)
|
|
1469
1513
|
return send(200, { ok: true, via: 'configEditor' })
|
|
1470
1514
|
}
|
|
1471
1515
|
}
|
|
@@ -1514,6 +1558,39 @@ function readBridgeState() {
|
|
|
1514
1558
|
|
|
1515
1559
|
|
|
1516
1560
|
|
|
1561
|
+
/** 把天气投票配置写到服务器 config/mcai-weather.json;mod 每次 /weather 时读取,改完即时生效。 */
|
|
1562
|
+
let lastWeatherConfigCore = ''
|
|
1563
|
+
function writeWeatherConfig(serverDir, weatherCfg) {
|
|
1564
|
+
try {
|
|
1565
|
+
const src = weatherCfg || {}
|
|
1566
|
+
const clampInt = (value, fallback, min, max) => {
|
|
1567
|
+
const n = Number(value)
|
|
1568
|
+
const v = Number.isFinite(n) ? Math.round(n) : fallback
|
|
1569
|
+
return Math.max(min, Math.min(max, v))
|
|
1570
|
+
}
|
|
1571
|
+
const core = {
|
|
1572
|
+
enabled: src.enabled !== false,
|
|
1573
|
+
directThreshold: clampInt(src.directThreshold, 2, 0, 20),
|
|
1574
|
+
approveNeed: clampInt(src.approveNeed, 2, 1, 20),
|
|
1575
|
+
rejectNeed: clampInt(src.rejectNeed, 1, 1, 20),
|
|
1576
|
+
timeoutSeconds: clampInt(src.timeoutSeconds, 60, 5, 3600),
|
|
1577
|
+
cooldownSeconds: clampInt(src.cooldownSeconds, 60, 0, 86400),
|
|
1578
|
+
}
|
|
1579
|
+
const coreText = JSON.stringify(core)
|
|
1580
|
+
if (coreText === lastWeatherConfigCore) return core
|
|
1581
|
+
const dir = join(serverDir, 'config')
|
|
1582
|
+
mkdirSync(dir, { recursive: true })
|
|
1583
|
+
writeFileSync(join(dir, 'mcai-weather.json'), JSON.stringify(Object.assign({}, core, {
|
|
1584
|
+
updatedAt: new Date().toISOString(),
|
|
1585
|
+
managedBy: 'dsh-mc-server-ops',
|
|
1586
|
+
}), null, 2) + '\n', 'utf8')
|
|
1587
|
+
lastWeatherConfigCore = coreText
|
|
1588
|
+
return core
|
|
1589
|
+
} catch (error) {
|
|
1590
|
+
return null
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1517
1594
|
/** 把投票门槛写到 ops\data\vote-config.json,脚本每次调用时读它。 */
|
|
1518
1595
|
function writeVoteConfig(opsPath, voteCfg, fallback) {
|
|
1519
1596
|
try {
|
package/ops/qq-bridge.mjs
CHANGED
|
@@ -339,7 +339,7 @@ export function createBridge(deps) {
|
|
|
339
339
|
const batch = queue
|
|
340
340
|
queue = []
|
|
341
341
|
if (!shouldForward(c)) { stats.suppressed += batch.length; return }
|
|
342
|
-
if (!c.groups.length) { stats.dropped += batch.length; lastError = '
|
|
342
|
+
if (!c.groups.length) { stats.dropped += batch.length; lastError = '还没发现目标群:在 QQ 群里 @ 一次机器人即可自动发现(或设置里手填群 openid)'; return }
|
|
343
343
|
const text = formatBatch(batch, { gamePrefix: c.gamePrefix, stripUrls: c.stripUrls })
|
|
344
344
|
if (!text) return
|
|
345
345
|
rollDay()
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 给 @tencent-connect/dsh-qqbot 的 inbound.js 打一个幂等抓取钩子:
|
|
3
|
+
* 机器人收到 QQ 群/私聊消息时,把 scope + peerId(群 openid / 用户 openid)
|
|
4
|
+
* 追加到 process.env.MC_OPS_QQ_KEYS_FILE 指向的文件里。
|
|
5
|
+
*
|
|
6
|
+
* 这样群服互通不需要用户手填群 openid:装好插件后在群里 @ 一次机器人,
|
|
7
|
+
* 群 openid 就会被抓到,bridge 会自动采用(来源:captured)。
|
|
8
|
+
*
|
|
9
|
+
* 设计要点:
|
|
10
|
+
* - 幂等:文件里已有 MARKER 就不再写;插件每次加载都会检查,qqbot 升级冲掉了会自动重打。
|
|
11
|
+
* - 失败安全:任何异常都只返回结果,不抛错、不影响 qqbot 运行。
|
|
12
|
+
* - 留备份:首次写前保存 inbound.js.mc-ops.bak。
|
|
13
|
+
*/
|
|
14
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync, copyFileSync } from 'node:fs'
|
|
15
|
+
import { homedir } from 'node:os'
|
|
16
|
+
import { join } from 'node:path'
|
|
17
|
+
|
|
18
|
+
const MARKER = '[mc-ops capture hook]'
|
|
19
|
+
|
|
20
|
+
function listProfileDirs() {
|
|
21
|
+
const dirs = []
|
|
22
|
+
const home = process.env.DSH_HOME || join(homedir(), '.dsh')
|
|
23
|
+
const profiles = join(home, 'profiles')
|
|
24
|
+
try {
|
|
25
|
+
for (const name of readdirSync(profiles)) dirs.push(join(profiles, name))
|
|
26
|
+
} catch (error) {
|
|
27
|
+
// ignore
|
|
28
|
+
}
|
|
29
|
+
const fromEnv = process.env.DSH_PROFILE_DIR
|
|
30
|
+
if (fromEnv) dirs.push(fromEnv)
|
|
31
|
+
return dirs
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function findQqbotInboundFiles() {
|
|
35
|
+
const files = []
|
|
36
|
+
for (const dir of listProfileDirs()) {
|
|
37
|
+
const candidate = join(dir, 'node_modules', '@tencent-connect', 'dsh-qqbot', 'dist', 'transport', 'inbound.js')
|
|
38
|
+
if (existsSync(candidate)) files.push(candidate)
|
|
39
|
+
}
|
|
40
|
+
return files
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function ensureAppendFileSyncImport(source) {
|
|
44
|
+
const m = /import\s*\{([^}]*)\}\s*from\s*'node:fs'/m.exec(source)
|
|
45
|
+
if (m) {
|
|
46
|
+
if (/\bappendFileSync\b/.test(m[1])) return source
|
|
47
|
+
const names = m[1].trimEnd()
|
|
48
|
+
return source.replace(m[0], 'import {' + names + ', appendFileSync } from \'node:fs\'')
|
|
49
|
+
}
|
|
50
|
+
// 没有 node:fs 具名导入:在第一条 import 后补一行
|
|
51
|
+
const first = /^import[^\n]*\n/m.exec(source)
|
|
52
|
+
if (!first) return source
|
|
53
|
+
return source.slice(0, first.index + first[0].length) + 'import { appendFileSync } from \'node:fs\'\n' + source.slice(first.index + first[0].length)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function patchSource(source) {
|
|
57
|
+
if (source.includes(MARKER)) return { changed: false, source, reason: 'already-patched' }
|
|
58
|
+
const anchor = source.indexOf('const replyTarget = {')
|
|
59
|
+
if (anchor === -1) return { changed: false, source, reason: 'anchor-not-found' }
|
|
60
|
+
const end = source.indexOf('};', anchor)
|
|
61
|
+
if (end === -1) return { changed: false, source, reason: 'anchor-end-not-found' }
|
|
62
|
+
let next = ensureAppendFileSyncImport(source)
|
|
63
|
+
const insertAt = next.indexOf('};', next.indexOf('const replyTarget = {')) + 2
|
|
64
|
+
const block = [
|
|
65
|
+
'',
|
|
66
|
+
' // ' + MARKER + ' 记录群/用户 openid,供群服互通自动发现目标群(dsh-mc-server-ops 写入,可删)',
|
|
67
|
+
' try {',
|
|
68
|
+
' const hookFile = process.env.MC_OPS_QQ_KEYS_FILE;',
|
|
69
|
+
' if (hookFile) appendFileSync(hookFile, new Date().toISOString() + \' \' + scope + \' \' + peerId + \'\\n\');',
|
|
70
|
+
' } catch { }',
|
|
71
|
+
'',
|
|
72
|
+
].join('\n')
|
|
73
|
+
next = next.slice(0, insertAt) + block + next.slice(insertAt)
|
|
74
|
+
return { changed: true, source: next, reason: 'patched' }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {{ log?: (message: string) => void }} [options]
|
|
79
|
+
* @returns {{ ok: boolean, patched: number, failed: number, files: string[], reason?: string }}
|
|
80
|
+
*/
|
|
81
|
+
export function ensureQqbotInboundHook(options = {}) {
|
|
82
|
+
const log = typeof options.log === 'function' ? options.log : () => {}
|
|
83
|
+
const files = findQqbotInboundFiles()
|
|
84
|
+
const result = { ok: files.length > 0, patched: 0, failed: 0, files, reason: '' }
|
|
85
|
+
if (!files.length) {
|
|
86
|
+
result.reason = '没有找到 @tencent-connect/dsh-qqbot(未安装 QQ 机器人插件?)'
|
|
87
|
+
return result
|
|
88
|
+
}
|
|
89
|
+
for (const file of files) {
|
|
90
|
+
try {
|
|
91
|
+
const source = readFileSync(file, 'utf8')
|
|
92
|
+
const patched = patchSource(source)
|
|
93
|
+
if (!patched.changed) {
|
|
94
|
+
log('qqbot 抓取钩子已存在:' + file)
|
|
95
|
+
continue
|
|
96
|
+
}
|
|
97
|
+
if (patched.reason !== 'patched') {
|
|
98
|
+
result.failed += 1
|
|
99
|
+
log('qqbot 抓取钩子打不上(' + patched.reason + '):' + file)
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
const backup = file + '.mc-ops.bak'
|
|
103
|
+
if (!existsSync(backup)) copyFileSync(file, backup)
|
|
104
|
+
writeFileSync(file, patched.source, 'utf8')
|
|
105
|
+
result.patched += 1
|
|
106
|
+
log('qqbot 抓取钩子已写入:' + file + '(重启 DSH / 重载 qqbot 后生效)')
|
|
107
|
+
} catch (error) {
|
|
108
|
+
result.failed += 1
|
|
109
|
+
log('qqbot 抓取钩子写入失败:' + file + ':' + (error && error.message ? error.message : String(error)))
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return result
|
|
113
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mc-server-ops",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.3",
|
|
4
4
|
"description": "在 DeepSeek Harness 里托管 Minecraft 服务器的插件:状态/启停/日志/白名单/投票授权,静态命令拦截,本地/Pterodactyl 可插拔适配器,游戏聊天直通 QQ 群(不经过 AI、不耗 token),把人设注入 QQ 会话,并让游戏内玩家用 /ai 和「小铜」对话(配套 Forge mod)。需要先安装 QQ 机器人插件。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|