dsh-mc-server-ops 0.6.2 → 0.6.4

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.
Files changed (4) hide show
  1. package/README.md +18 -0
  2. package/client.js +28 -0
  3. package/index.js +135 -3
  4. 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
 
@@ -274,6 +280,18 @@ powershell -File <服务器目录>\ops\mcadmin.ps1 -Action owner-add -Arg <openi
274
280
 
275
281
  游戏内玩家和群友一样受权限守卫限制:只读查询可以,写文件、执行命令需要服主授权;mod 侧默认每玩家 5 秒冷却,避免刷屏。
276
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
+ - **QQ 群里也能改**:玩家先用 `bind-player` 绑定游戏名并进入游戏,然后在群里跟小铜说「把天气改成下雨」即可——小铜会用你的游戏名执行 `weather`,在线 ≤2 人直接生效、≥3 人时游戏内会广播投票(其他玩家 `/vote yes`)。服主在群里说则直接执行。
294
+
277
295
  ## 安全模型
278
296
 
279
297
  - 仓库和 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。"),
package/index.js CHANGED
@@ -83,6 +83,36 @@ function isOwner(actor) {
83
83
  return readOwners().includes(actor)
84
84
  }
85
85
 
86
+ /** 读 ops/data/bindings.json:QQ openid 与游戏名的绑定。 */
87
+ function readPlayerBindings() {
88
+ try {
89
+ const raw = readFileSync(join(paths.serverDir, 'ops', 'data', 'bindings.json'), 'utf8').replace(/^\uFEFF/, '')
90
+ const doc = JSON.parse(raw)
91
+ return Array.isArray(doc)
92
+ ? doc.map((x) => ({ qq: String((x && x.qq) || ''), name: String((x && x.name) || '') })).filter((x) => x.qq && x.name)
93
+ : []
94
+ } catch {
95
+ return []
96
+ }
97
+ }
98
+
99
+ /** 从 mcadmin players 的输出里解析在线玩家名。 */
100
+ function parseOnlinePlayers(text) {
101
+ const match = /online:\s*([^\n]*)/i.exec(String(text || ''))
102
+ if (!match) return []
103
+ return match[1].split(',').map((x) => x.trim()).filter(Boolean)
104
+ }
105
+
106
+ /** 天气类型规范化;不认识的返回空串。 */
107
+ const WEATHER_NAMES = { clear: '晴天', rain: '下雨', thunder: '雷雨' }
108
+ function normalizeWeatherType(arg) {
109
+ const value = String(arg || '').trim().toLowerCase()
110
+ if (value === 'clear' || value === 'sunny' || value === '晴天') return 'clear'
111
+ if (value === 'rain' || value === '下雨') return 'rain'
112
+ if (value === 'thunder' || value === '雷雨') return 'thunder'
113
+ return ''
114
+ }
115
+
86
116
  function grantTools(agentId) {
87
117
  if (agentId) grants.set(String(agentId), Date.now() + GRANT_MS)
88
118
  }
@@ -116,7 +146,7 @@ function readQqbotCredentials() {
116
146
 
117
147
  const ACTIONS = [
118
148
  'status', 'players', 'say', 'cmd', 'start', 'stop', 'restart',
119
- 'logs', 'crashinfo', 'whoami', 'help', 'gamemode',
149
+ 'logs', 'crashinfo', 'whoami', 'help', 'gamemode', 'weather',
120
150
  'bind-player', 'unbind-player', 'my-players', 'bindings', 'whitelist-sync',
121
151
  'kubejs-backup', 'kubejs-reload', 'vote-start', 'vote-approve', 'vote-reject', 'vote-status',
122
152
  'export-data', 'import-data', 'bridge-groups',
@@ -173,9 +203,10 @@ const DESCRIPTION = [
173
203
  '- stop:停服,仅服主',
174
204
  '- restart:服务器确认关闭、或健康=无响应(假死)时任何人可用(假死会自动强杀重启);健康=正常或启动中时仅服主',
175
205
  '- gamemode:切换游戏模式,arg 形如 spectator 玩家名。玩家只能切 survival / spectator,且目标必须是自己绑定的游戏名;creative / adventure 和切别人仅服主',
206
+ '- weather:改天气,arg 为 clear / rain / thunder(也可写 晴天 / 下雨 / 雷雨)。所有玩家都能用:自己的游戏名在线时,小铜会用他的游戏名发起——在线 ≤2 人直接生效,≥3 人时游戏内会广播投票(其他玩家 /vote yes);服主则直接执行。不要因为 cmd 仅服主就拒绝玩家的天气请求',
176
207
  '- cmd:执行任意后台指令(op/deop/ban/kick/whitelist 等),仅服主',
177
208
  '',
178
- '玩家还能做:用 read / glob / grep 看服务器文件、用只读的 pwsh 命令查信息、用 qqbot_send_file 把普通文件发到群里(口令/密钥类文件除外)。',
209
+ '玩家还能做:用 read / glob / grep 看服务器文件、用只读的 pwsh 命令查信息、用 qqbot_send_file 把普通文件发到群里(口令/密钥类文件除外)、用 weather 改天气。',
179
210
  '玩家不能:改文件、删文件、新建文件、执行危险命令、设置创造模式。',
180
211
  '- bind-player:把游戏名绑定到当前 QQ(同时加入服务器白名单),arg 为游戏名(1-16 位字母/数字/下划线),一个 QQ 最多 10 个,一个游戏名最多被 3 个 QQ 绑定',
181
212
  '- unbind-player:解绑自己的某个游戏名(若该名字已无其它 QQ 绑定,同时移出白名单),arg 为游戏名',
@@ -379,6 +410,21 @@ export const Config = Schema.object({
379
410
  .description('受保护路径:改动这些位置需要群友投票放行。默认 world, ops/data, whitelist'),
380
411
  }).description('投票'),
381
412
 
413
+ weather: Schema.object({
414
+ enabled: Schema.boolean().default(true)
415
+ .description('允许游戏内玩家用 /weather 改天气(需要 mcai mod 1.2.0+)。配置会写到服务器 config/mcai-weather.json,改完即时生效'),
416
+ directThreshold: Schema.number().min(0).max(20).default(2)
417
+ .description('在线玩家不超过这个数时直接改天气、不投票(默认 2:0-2 人直接生效)'),
418
+ approveNeed: Schema.number().min(1).max(20).default(2)
419
+ .description('投票通过需要几人同意(发起人自动算 1 票;默认 2)'),
420
+ rejectNeed: Schema.number().min(1).max(20).default(1)
421
+ .description('几人反对就作废(默认 1 = 一票否决)'),
422
+ timeoutSeconds: Schema.number().min(5).max(3600).default(60)
423
+ .description('投票有效期(秒,默认 60)'),
424
+ cooldownSeconds: Schema.number().min(0).max(86400).default(60)
425
+ .description('同一玩家两次发起的最小间隔(秒,默认 60,0 = 不限)'),
426
+ }).description('游戏内天气投票(配套 mcai mod)'),
427
+
382
428
  adapter: Schema.union(['local', 'panel']).default('local')
383
429
  .description('写操作走哪条路:local=本机直接跑脚本(默认);panel=通过 Pterodactyl 面板的 HTTP API 启停/发指令'),
384
430
  panel: Schema.object({
@@ -715,13 +761,20 @@ export function apply(ctx, config) {
715
761
  maxPerDay: 900,
716
762
  }, (config && config.bridge) || {}),
717
763
  ai: Object.assign({ enabled: true, port: 8765, timeoutSeconds: 60 }, (config && config.ai) || {}),
764
+ weather: Object.assign({
765
+ enabled: true, directThreshold: 2, approveNeed: 2, rejectNeed: 1,
766
+ timeoutSeconds: 60, cooldownSeconds: 60,
767
+ }, (config && config.weather) || {}),
718
768
  }
769
+ // 启动时先把天气投票配置写到服务器 config/mcai-weather.json;mod 每次 /weather 都读这个文件。
770
+ writeWeatherConfig(serverDir, liveConfig.weather)
719
771
  const memoryConfig = {
720
772
  get: () => liveConfig,
721
773
  set: (next) => {
722
774
  liveConfig = Object.assign({}, liveConfig, next && typeof next === 'object' ? next : {})
723
775
  writeVoteConfig(opsDir, liveConfig.vote)
724
776
  setProtectedPaths(liveConfig.vote && liveConfig.vote.protectedPaths)
777
+ writeWeatherConfig(paths.serverDir, liveConfig.weather)
725
778
  },
726
779
  }
727
780
  // 启动时先落一份,保证脚本读到的是当前设置
@@ -836,6 +889,7 @@ export function apply(ctx, config) {
836
889
  void bridge.start()
837
890
  const tick = setInterval(() => {
838
891
  resolveBridgeGroups((liveConfig && liveConfig.bridge) || {})
892
+ writeWeatherConfig(serverDir, liveConfig.weather)
839
893
  writeBridgeState(serverDir, Object.assign(
840
894
  { appId: appId ? appId.slice(0, 3) + '***' : '', hasSecret: !!appSecret },
841
895
  bridge.status(),
@@ -1096,7 +1150,7 @@ export function apply(ctx, config) {
1096
1150
  },
1097
1151
  arg: {
1098
1152
  type: 'string',
1099
- description: 'say 的内容、cmd 的指令(不带斜杠)、口令,其它操作为空',
1153
+ description: 'say 的内容、cmd 的指令(不带斜杠)、weather 的天气类型(clear/rain/thunder)、口令,其它操作为空',
1100
1154
  },
1101
1155
  channel: {
1102
1156
  type: 'string',
@@ -1177,6 +1231,49 @@ export function apply(ctx, config) {
1177
1231
  }
1178
1232
  }
1179
1233
 
1234
+ // 天气投票:玩家也能用(不需要服主权限)。
1235
+ // - 服主/控制台:直接执行 weather 命令(相当于控制台发起,不投票)
1236
+ // - 玩家:用他绑定且当前在线的游戏名执行 execute as <name> run weather <type>,
1237
+ // mod 会按在线人数决定直接生效还是发起游戏内投票(/vote yes)。
1238
+ if (action === 'weather') {
1239
+ const weatherCfg = (liveConfig && liveConfig.weather) || {}
1240
+ if (weatherCfg.enabled === false) return { ok: false, text: '天气功能已经在设置里关掉了(设置 → 游戏内天气投票)。' }
1241
+ const type = normalizeWeatherType(arg)
1242
+ if (!type) return { ok: false, text: '用法:weather <clear|rain|thunder>(晴天 / 下雨 / 雷雨)' }
1243
+ const typeName = WEATHER_NAMES[type]
1244
+ const threshold = Math.max(0, Number(weatherCfg.directThreshold) || 0)
1245
+ const approveNeed = Math.max(1, Number(weatherCfg.approveNeed) || 2)
1246
+ if (isOwner(actor)) {
1247
+ const done = await runMcAdmin(['-Action', 'cmd', '-Arg', 'weather ' + type, '-Actor', 'LOCAL-CONSOLE', '-Channel', 'console'], 60000)
1248
+ if (!done.ok) return { ok: false, text: '改天气失败:' + done.text }
1249
+ return { ok: true, text: '天气已改成「' + typeName + '」(服主直接执行,不投票)。' }
1250
+ }
1251
+ const mine = readPlayerBindings().filter((b) => b.qq === actor).map((b) => b.name)
1252
+ if (!mine.length) {
1253
+ return {
1254
+ ok: false,
1255
+ text: '你还没绑定游戏名,我没法用你的身份改天气。先发「绑定游戏名 你的ID」把 QQ 和游戏名绑上、进服后再试;'
1256
+ + '也可以直接在游戏里输入 /weather ' + type + '(在线 3 人以上会自动发起投票)。',
1257
+ }
1258
+ }
1259
+ const playersResult = await runMcAdmin(['-Action', 'players', '-Actor', actor, '-Channel', channel], 30000)
1260
+ const online = parseOnlinePlayers(playersResult.ok ? playersResult.text : '')
1261
+ const onlineName = mine.find((name) => online.some((p) => p.toLowerCase() === name.toLowerCase()))
1262
+ if (!onlineName) {
1263
+ return {
1264
+ ok: false,
1265
+ text: '你绑定的游戏名(' + mine.join(', ') + ')现在都不在线。先进服再让我改;'
1266
+ + '或者在游戏里输入 /weather ' + type + '(在线 3 人以上会自动发起投票)。',
1267
+ }
1268
+ }
1269
+ const started = await runMcAdmin(['-Action', 'cmd', '-Arg', 'execute as ' + onlineName + ' run weather ' + type, '-Actor', 'LOCAL-CONSOLE', '-Channel', 'console'], 60000)
1270
+ if (!started.ok) return { ok: false, text: '发起天气请求失败:' + started.text }
1271
+ const how = online.length <= threshold
1272
+ ? '当前在线 ' + online.length + ' 人(≤' + threshold + '),会直接生效'
1273
+ : '当前在线 ' + online.length + ' 人,游戏里已经发起投票,需要 ' + approveNeed + ' 票同意(其他玩家输入 /vote yes)'
1274
+ return { ok: true, text: '已用你的游戏名 ' + onlineName + ' 发起:把天气改成「' + typeName + '」。' + how + '。' }
1275
+ }
1276
+
1180
1277
  const isWindows = process.platform === 'win32'
1181
1278
  if (!isWindows && !NODE_ACTIONS.has(action)) {
1182
1279
  return { ok: false, text: `(${process.platform})${action} 还没跨平台,目前只在 Windows 上可用。跨平台可用的是:${[...NODE_ACTIONS].join(' / ')}` }
@@ -1473,6 +1570,7 @@ async function handleConfigRoute(req, res, services, fallbackConfig) {
1473
1570
  if (desc) {
1474
1571
  await settings.update(desc.ns, body, desc.revision)
1475
1572
  fallbackConfig.set(body)
1573
+ writeWeatherConfig(paths.serverDir, (body && body.weather) || (fallbackConfig.get() || {}).weather)
1476
1574
  return send(200, { ok: true, via: 'settings' })
1477
1575
  }
1478
1576
  if (editor && typeof editor.entries === 'function' && typeof editor.edit === 'function') {
@@ -1485,6 +1583,7 @@ async function handleConfigRoute(req, res, services, fallbackConfig) {
1485
1583
  if (entry) {
1486
1584
  await editor.edit(entry, (current) => Object.assign({}, current || {}, body))
1487
1585
  fallbackConfig.set(body)
1586
+ writeWeatherConfig(paths.serverDir, (body && body.weather) || (fallbackConfig.get() || {}).weather)
1488
1587
  return send(200, { ok: true, via: 'configEditor' })
1489
1588
  }
1490
1589
  }
@@ -1533,6 +1632,39 @@ function readBridgeState() {
1533
1632
 
1534
1633
 
1535
1634
 
1635
+ /** 把天气投票配置写到服务器 config/mcai-weather.json;mod 每次 /weather 时读取,改完即时生效。 */
1636
+ let lastWeatherConfigCore = ''
1637
+ function writeWeatherConfig(serverDir, weatherCfg) {
1638
+ try {
1639
+ const src = weatherCfg || {}
1640
+ const clampInt = (value, fallback, min, max) => {
1641
+ const n = Number(value)
1642
+ const v = Number.isFinite(n) ? Math.round(n) : fallback
1643
+ return Math.max(min, Math.min(max, v))
1644
+ }
1645
+ const core = {
1646
+ enabled: src.enabled !== false,
1647
+ directThreshold: clampInt(src.directThreshold, 2, 0, 20),
1648
+ approveNeed: clampInt(src.approveNeed, 2, 1, 20),
1649
+ rejectNeed: clampInt(src.rejectNeed, 1, 1, 20),
1650
+ timeoutSeconds: clampInt(src.timeoutSeconds, 60, 5, 3600),
1651
+ cooldownSeconds: clampInt(src.cooldownSeconds, 60, 0, 86400),
1652
+ }
1653
+ const coreText = JSON.stringify(core)
1654
+ if (coreText === lastWeatherConfigCore) return core
1655
+ const dir = join(serverDir, 'config')
1656
+ mkdirSync(dir, { recursive: true })
1657
+ writeFileSync(join(dir, 'mcai-weather.json'), JSON.stringify(Object.assign({}, core, {
1658
+ updatedAt: new Date().toISOString(),
1659
+ managedBy: 'dsh-mc-server-ops',
1660
+ }), null, 2) + '\n', 'utf8')
1661
+ lastWeatherConfigCore = coreText
1662
+ return core
1663
+ } catch (error) {
1664
+ return null
1665
+ }
1666
+ }
1667
+
1536
1668
  /** 把投票门槛写到 ops\data\vote-config.json,脚本每次调用时读它。 */
1537
1669
  function writeVoteConfig(opsPath, voteCfg, fallback) {
1538
1670
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mc-server-ops",
3
- "version": "0.6.2",
3
+ "version": "0.6.4",
4
4
  "description": "在 DeepSeek Harness 里托管 Minecraft 服务器的插件:状态/启停/日志/白名单/投票授权,静态命令拦截,本地/Pterodactyl 可插拔适配器,游戏聊天直通 QQ 群(不经过 AI、不耗 token),把人设注入 QQ 会话,并让游戏内玩家用 /ai 和「小铜」对话(配套 Forge mod)。需要先安装 QQ 机器人插件。",
5
5
  "type": "module",
6
6
  "main": "./index.js",