mioku-plugin-mc 1.0.0
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/LICENSE +21 -0
- package/README.md +68 -0
- package/config.md +139 -0
- package/core/rcon.ts +85 -0
- package/core/ws-factory.ts +223 -0
- package/handlers/reconnect.ts +9 -0
- package/handlers/status.ts +39 -0
- package/handlers/sync.ts +38 -0
- package/index.ts +203 -0
- package/package.json +45 -0
- package/types.ts +79 -0
- package/utils/command-router.ts +51 -0
- package/utils/config-handler.ts +49 -0
- package/utils/message-formatter.ts +117 -0
- package/utils/server-manager.ts +126 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mioku Lab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Mioku Minecraft Plugin
|
|
2
|
+
|
|
3
|
+
Minecraft 服务器与 QQ 群消息互通插件,支持服务器状态监控与 RCON 命令执行,服务器需安装鹊桥模组或插件并配置。
|
|
4
|
+
|
|
5
|
+
使用到的外部包:`@cikeyqi/queqiao-node-sdk`
|
|
6
|
+
|
|
7
|
+
## 功能特性
|
|
8
|
+
|
|
9
|
+
- **消息同步**:群聊消息同步到 MC 服务器,MC 服务器消息同步到群聊
|
|
10
|
+
- **服务器状态推送**:玩家加入/退出/死亡/达成进度时自动推送到群聊
|
|
11
|
+
- **RCON 命令执行**:在群聊中发送命令前缀开头的消息即可执行 RCON 命令
|
|
12
|
+
- **多服务器支持**:可配置多个 MC 服务器,每个服务器独立设置连接方式
|
|
13
|
+
- **正向/反向 WebSocket**:支持机器人主动连接或 MC 服务器反向连接
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
|
+
|
|
17
|
+
将插件放置在 `plugins/mc/` 目录下,然后执行:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
bun install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 配置
|
|
24
|
+
|
|
25
|
+
插件需要通过 WebUI 配置页面来管理配置项。
|
|
26
|
+
|
|
27
|
+
### 全局配置
|
|
28
|
+
|
|
29
|
+
| 配置项 | 类型 | 说明 |
|
|
30
|
+
|------------------|--------|----------------------------|
|
|
31
|
+
| `聊天前缀` | text | QQ 消息转发到 MC 时显示的前缀,默认 "说:" |
|
|
32
|
+
| `显示服务器名称` | switch | 是否在推送消息中显示服务器名称 |
|
|
33
|
+
| `启用反向 WebSocket` | switch | 启用反向连接(MC 服务器连接到此机器人) |
|
|
34
|
+
| `反向连接端口` | number | 反向 WebSocket 监听端口 |
|
|
35
|
+
| `反向连接路径` | text | 反向 WebSocket 路径 |
|
|
36
|
+
| `反向连接密码` | secret | 反向 WebSocket 连接密码 |
|
|
37
|
+
|
|
38
|
+
### 服务器配置
|
|
39
|
+
|
|
40
|
+
每个服务器可独立配置以下选项:
|
|
41
|
+
|
|
42
|
+
| 配置项 | 类型 | 说明 |
|
|
43
|
+
|------------------|--------|-----------------------------|
|
|
44
|
+
| `服务器名称` | text | 服务器唯一标识,不能与其他服务器重复 |
|
|
45
|
+
| `WebSocket 地址` | text | 服务器 WebSocket 连接地址(正向连接时使用) |
|
|
46
|
+
| `连接密码` | text | WebSocket 连接密码(可选) |
|
|
47
|
+
| `关联群聊` | text | 关联的 QQ 群号 |
|
|
48
|
+
| `机器人账号` | text | 使用哪个机器人 QQ 号发送消息 |
|
|
49
|
+
| `命令前缀` | text | RCON 命令前缀,默认 "$" |
|
|
50
|
+
| `命令白名单` | text | 允许执行 RCON 命令的 QQ 号列表 |
|
|
51
|
+
| `RCON 命令白名单` | text | 允许执行的 RCON 命令列表 |
|
|
52
|
+
| `启用同步` | switch | 是否启用消息同步 |
|
|
53
|
+
| `最大重连次数` | number | WebSocket 最大重连次数 |
|
|
54
|
+
| `启用正向 WebSocket` | switch | 启用正向连接(连接到 MC 服务器) |
|
|
55
|
+
| `启用反向连接` | switch | 该服务器是否启用反向连接 |
|
|
56
|
+
| `启用 RCON` | switch | 启用 RCON 命令执行 |
|
|
57
|
+
| `RCON 地址` | text | RCON 服务器地址 |
|
|
58
|
+
| `RCON 端口` | number | RCON 端口 |
|
|
59
|
+
| `RCON 密码` | secret | RCON 密码 |
|
|
60
|
+
|
|
61
|
+
## 命令
|
|
62
|
+
|
|
63
|
+
| 命令 | 描述 | 权限 |
|
|
64
|
+
|--------------------|----------------------------------------|-----|
|
|
65
|
+
| `/mc 状态` | 查看所有已配置服务器的 WebSocket 连接状态,包括连接中、已断开等 | 主人 |
|
|
66
|
+
| `/mc 开启同步 <服务器名称>` | 开启指定服务器的群聊消息同步功能,开启后该服务器可接收和发送群聊消息 | 主人 |
|
|
67
|
+
| `/mc 关闭同步 <服务器名称>` | 关闭指定服务器的群聊消息同步功能,关闭后该服务器不再接收和发送群聊消息 | 主人 |
|
|
68
|
+
| `/mc 重连` | 断开并重新建立所有服务器的 WebSocket 连接,用于连接异常时手动恢复 | 主人 |
|
package/config.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Minecraft 插件配置
|
|
3
|
+
description: 配置 Minecraft 服务器连接、消息同步与 RCON 命令白名单
|
|
4
|
+
fields:
|
|
5
|
+
- key: base.say_way
|
|
6
|
+
label: 聊天前缀
|
|
7
|
+
type: text
|
|
8
|
+
description: QQ 消息转发到 MC 时显示的前缀,默认 "说:"
|
|
9
|
+
placeholder: 说:
|
|
10
|
+
|
|
11
|
+
- key: base.display_server_name
|
|
12
|
+
label: 显示服务器名称
|
|
13
|
+
type: switch
|
|
14
|
+
description: 消息中是否显示服务器名称
|
|
15
|
+
|
|
16
|
+
- key: base.reverse_ws_enabled
|
|
17
|
+
label: 启用反向 WebSocket
|
|
18
|
+
type: switch
|
|
19
|
+
description: 启用反向连接(MC 服务器连接到此机器人)
|
|
20
|
+
|
|
21
|
+
- key: base.reverse_ws_port
|
|
22
|
+
label: 反向连接端口
|
|
23
|
+
type: number
|
|
24
|
+
description: 反向 WebSocket 监听端口
|
|
25
|
+
placeholder: 8080
|
|
26
|
+
|
|
27
|
+
- key: base.reverse_ws_path
|
|
28
|
+
label: 反向连接路径
|
|
29
|
+
type: text
|
|
30
|
+
description: 反向 WebSocket 路径
|
|
31
|
+
placeholder: /minecraft/ws
|
|
32
|
+
|
|
33
|
+
- key: base.reverse_ws_password
|
|
34
|
+
label: 反向连接密码
|
|
35
|
+
type: secret
|
|
36
|
+
description: 反向 WebSocket 连接密码
|
|
37
|
+
|
|
38
|
+
- key: base.servers
|
|
39
|
+
label: 服务器列表
|
|
40
|
+
type: array
|
|
41
|
+
description: 配置多个 Minecraft 服务器连接
|
|
42
|
+
itemFields:
|
|
43
|
+
- key: server_name
|
|
44
|
+
label: 服务器名称
|
|
45
|
+
type: text
|
|
46
|
+
description: 服务器唯一标识,不能与其他服务器重复
|
|
47
|
+
placeholder: main
|
|
48
|
+
|
|
49
|
+
- key: ws_url
|
|
50
|
+
label: WebSocket 地址
|
|
51
|
+
type: text
|
|
52
|
+
description: 服务器 WebSocket 连接地址(正向连接时使用)
|
|
53
|
+
placeholder: ws://127.0.0.1:8081
|
|
54
|
+
|
|
55
|
+
- key: ws_password
|
|
56
|
+
label: 连接密码
|
|
57
|
+
type: text
|
|
58
|
+
description: WebSocket 连接密码(可选)
|
|
59
|
+
|
|
60
|
+
- key: group_list
|
|
61
|
+
label: 关联群聊
|
|
62
|
+
type: text
|
|
63
|
+
description: 关联的 QQ 群号
|
|
64
|
+
|
|
65
|
+
- key: bot_self_id
|
|
66
|
+
label: 机器人账号
|
|
67
|
+
type: text
|
|
68
|
+
description: 机器人 QQ 号
|
|
69
|
+
|
|
70
|
+
- key: command_header
|
|
71
|
+
label: 命令前缀
|
|
72
|
+
type: text
|
|
73
|
+
description: RCON 命令前缀,默认 "$"
|
|
74
|
+
placeholder: $
|
|
75
|
+
|
|
76
|
+
- key: command_user
|
|
77
|
+
label: 命令白名单
|
|
78
|
+
type: text
|
|
79
|
+
description: 允许执行 RCON 命令的 QQ 号列表,每行一个
|
|
80
|
+
|
|
81
|
+
- key: rcon_command_whitelist
|
|
82
|
+
label: RCON 命令白名单
|
|
83
|
+
type: text
|
|
84
|
+
description: 允许执行的 RCON 命令列表,每行一个
|
|
85
|
+
|
|
86
|
+
- key: sync_enabled
|
|
87
|
+
label: 启用同步
|
|
88
|
+
type: switch
|
|
89
|
+
description: 是否启用消息同步
|
|
90
|
+
|
|
91
|
+
- key: ws_max_attempts
|
|
92
|
+
label: 最大重连次数
|
|
93
|
+
type: number
|
|
94
|
+
description: WebSocket 最大重连次数
|
|
95
|
+
placeholder: 10
|
|
96
|
+
|
|
97
|
+
- key: forward_ws_enabled
|
|
98
|
+
label: 启用正向 WebSocket
|
|
99
|
+
type: switch
|
|
100
|
+
description: 启用正向连接(连接到 MC 服务器)
|
|
101
|
+
|
|
102
|
+
- key: reverse_ws_enabled
|
|
103
|
+
label: 启用反向连接
|
|
104
|
+
type: switch
|
|
105
|
+
description: 该服务器是否启用反向连接
|
|
106
|
+
|
|
107
|
+
- key: rcon_enabled
|
|
108
|
+
label: 启用 RCON
|
|
109
|
+
type: switch
|
|
110
|
+
description: 启用 RCON 命令执行
|
|
111
|
+
|
|
112
|
+
- key: rcon_host
|
|
113
|
+
label: RCON 地址
|
|
114
|
+
type: text
|
|
115
|
+
description: RCON 服务器地址
|
|
116
|
+
placeholder: 127.0.0.1
|
|
117
|
+
|
|
118
|
+
- key: rcon_port
|
|
119
|
+
label: RCON 端口
|
|
120
|
+
type: number
|
|
121
|
+
description: RCON 端口
|
|
122
|
+
placeholder: 25575
|
|
123
|
+
|
|
124
|
+
- key: rcon_password
|
|
125
|
+
label: RCON 密码
|
|
126
|
+
type: secret
|
|
127
|
+
description: RCON 密码
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
```mioku-fields
|
|
131
|
+
keys:
|
|
132
|
+
- base.say_way
|
|
133
|
+
- base.display_server_name
|
|
134
|
+
- base.reverse_ws_enabled
|
|
135
|
+
- base.reverse_ws_port
|
|
136
|
+
- base.reverse_ws_path
|
|
137
|
+
- base.reverse_ws_password
|
|
138
|
+
- base.servers
|
|
139
|
+
```
|
package/core/rcon.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { ServerConfig } from "../types";
|
|
2
|
+
|
|
3
|
+
export interface CommandResult {
|
|
4
|
+
status?: string;
|
|
5
|
+
message?: string;
|
|
6
|
+
data?: unknown;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function parseCommand(
|
|
10
|
+
text: string,
|
|
11
|
+
serverItem: ServerConfig,
|
|
12
|
+
): string | null {
|
|
13
|
+
const header = serverItem.command_header || "$";
|
|
14
|
+
const trimmed = text.trim();
|
|
15
|
+
|
|
16
|
+
if (!header || !trimmed.startsWith(header)) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return trimmed.slice(header.length).trim();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function isCommandAllowed(
|
|
24
|
+
text: string,
|
|
25
|
+
serverItem: ServerConfig,
|
|
26
|
+
isMaster: boolean,
|
|
27
|
+
userId: number | string,
|
|
28
|
+
): boolean {
|
|
29
|
+
if (isMaster) return true;
|
|
30
|
+
|
|
31
|
+
// check whitelist
|
|
32
|
+
const cmdName = getCommandName(text);
|
|
33
|
+
const whitelist = serverItem.rcon_command_whitelist || [];
|
|
34
|
+
if (whitelist.some((w) => String(w).replace(/^\/+/, "") === cmdName)) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// check user list
|
|
39
|
+
const userList = serverItem.command_user || [];
|
|
40
|
+
return userList.some((u) => String(u) === String(userId));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function getCommandName(text: string): string {
|
|
44
|
+
const trimmed = String(text).trim();
|
|
45
|
+
const parts = trimmed.split(/\s+/, 1);
|
|
46
|
+
return parts[0] || "";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function formatCommandResult(raw: unknown): string {
|
|
50
|
+
if (raw === null || raw === undefined || raw === "") {
|
|
51
|
+
return "命令执行成功";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (typeof raw === "string") {
|
|
55
|
+
try {
|
|
56
|
+
const parsed = JSON.parse(raw);
|
|
57
|
+
return formatCommandResult(parsed);
|
|
58
|
+
} catch {
|
|
59
|
+
return raw;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (typeof raw !== "object") {
|
|
64
|
+
return String(raw);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const obj = raw as Record<string, unknown>;
|
|
68
|
+
|
|
69
|
+
if (obj.data !== null && obj.data !== undefined && obj.data !== "") {
|
|
70
|
+
return typeof obj.data === "string"
|
|
71
|
+
? String(obj.data)
|
|
72
|
+
: JSON.stringify(obj.data);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const status = String(obj.status || "")
|
|
76
|
+
.trim()
|
|
77
|
+
.toUpperCase();
|
|
78
|
+
const message = String(obj.message || "").trim();
|
|
79
|
+
|
|
80
|
+
if (status && status !== "SUCCESS") {
|
|
81
|
+
return message || `命令执行失败 (${status})`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return message || "命令执行成功";
|
|
85
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createClient,
|
|
3
|
+
createReverseClient,
|
|
4
|
+
type QueQiaoClient,
|
|
5
|
+
type QueQiaoEvent,
|
|
6
|
+
type KnownApi,
|
|
7
|
+
} from "@cikeyqi/queqiao-node-sdk";
|
|
8
|
+
import type { McConfig, ServerConfig } from "../types";
|
|
9
|
+
|
|
10
|
+
export interface WsClient {
|
|
11
|
+
serverName: string;
|
|
12
|
+
config: ServerConfig;
|
|
13
|
+
status: "connected" | "disconnected" | "connecting";
|
|
14
|
+
send(data: Record<string, unknown>): Promise<void>;
|
|
15
|
+
close(): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface WsServer {
|
|
19
|
+
status: "listening" | "stopped";
|
|
20
|
+
close(): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type EventHandler = (event: QueQiaoEvent) => void;
|
|
24
|
+
type StatusHandler = (serverName: string, status: string) => void;
|
|
25
|
+
type ErrorHandler = (serverName: string, error: Error) => void;
|
|
26
|
+
|
|
27
|
+
// Safe string normalizer - empty string becomes undefined, otherwise trimmed
|
|
28
|
+
function safeString(value: string | undefined | null): string | undefined {
|
|
29
|
+
if (!value) return undefined;
|
|
30
|
+
const trimmed = value.trim();
|
|
31
|
+
return trimmed || undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function createForwardClient(
|
|
35
|
+
serverItem: ServerConfig,
|
|
36
|
+
onEvent: EventHandler,
|
|
37
|
+
onStatusChange: StatusHandler,
|
|
38
|
+
onError?: ErrorHandler,
|
|
39
|
+
): WsClient {
|
|
40
|
+
let client: QueQiaoClient | null = null;
|
|
41
|
+
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
42
|
+
let reconnectAttempt = 0;
|
|
43
|
+
let currentStatus: "connected" | "disconnected" | "connecting" = "disconnected";
|
|
44
|
+
|
|
45
|
+
const updateStatus = (status: "connected" | "disconnected" | "connecting") => {
|
|
46
|
+
currentStatus = status;
|
|
47
|
+
onStatusChange(serverItem.server_name, status);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const connect = () => {
|
|
51
|
+
if (client) {
|
|
52
|
+
client.close().catch(() => {});
|
|
53
|
+
client = null;
|
|
54
|
+
}
|
|
55
|
+
updateStatus("connecting");
|
|
56
|
+
|
|
57
|
+
const accessToken = safeString(serverItem.ws_password);
|
|
58
|
+
|
|
59
|
+
client = createClient(
|
|
60
|
+
[
|
|
61
|
+
{
|
|
62
|
+
url: serverItem.ws_url,
|
|
63
|
+
selfName: serverItem.server_name,
|
|
64
|
+
accessToken: accessToken,
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
{
|
|
68
|
+
reconnect: false,
|
|
69
|
+
},
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
client.on("connection_open", (_selfName: string) => {
|
|
73
|
+
reconnectAttempt = 0;
|
|
74
|
+
updateStatus("connected");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
client.on("connection_close", (_selfName: string, _code: number, _reason: string) => {
|
|
78
|
+
updateStatus("disconnected");
|
|
79
|
+
scheduleReconnect();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
client.on("connection_error", (_selfName: string, error: Error) => {
|
|
83
|
+
onError?.(serverItem.server_name, error);
|
|
84
|
+
updateStatus("disconnected");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
client.on("event", (eventData: QueQiaoEvent) => {
|
|
88
|
+
onEvent(eventData);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
client.on("error", () => {});
|
|
92
|
+
|
|
93
|
+
client.connect().catch((err: Error) => {
|
|
94
|
+
onError?.(serverItem.server_name, err);
|
|
95
|
+
updateStatus("disconnected");
|
|
96
|
+
scheduleReconnect();
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const scheduleReconnect = () => {
|
|
101
|
+
if (reconnectTimer) return;
|
|
102
|
+
const maxAttempts = serverItem.ws_max_attempts || 0;
|
|
103
|
+
|
|
104
|
+
reconnectAttempt++;
|
|
105
|
+
if (maxAttempts > 0 && reconnectAttempt > maxAttempts) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Exponential backoff: 10s, 20s, 30s, ... up to 60s
|
|
110
|
+
const baseDelay = 10000;
|
|
111
|
+
const delay = Math.min(baseDelay * reconnectAttempt, 60000);
|
|
112
|
+
|
|
113
|
+
reconnectTimer = setTimeout(() => {
|
|
114
|
+
reconnectTimer = null;
|
|
115
|
+
connect();
|
|
116
|
+
}, delay);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const send = async (data: Record<string, unknown>): Promise<void> => {
|
|
120
|
+
const api = data.type as string;
|
|
121
|
+
const apiData = { ...data };
|
|
122
|
+
delete apiData.type;
|
|
123
|
+
|
|
124
|
+
if (!client || !client.isOpen({ selfName: serverItem.server_name })) {
|
|
125
|
+
throw new Error(`服务器 ${serverItem.server_name} 未连接`);
|
|
126
|
+
}
|
|
127
|
+
await client.request(
|
|
128
|
+
api as KnownApi,
|
|
129
|
+
apiData as Record<string, unknown>,
|
|
130
|
+
{ selfName: serverItem.server_name },
|
|
131
|
+
);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const close = () => {
|
|
135
|
+
if (reconnectTimer) {
|
|
136
|
+
clearTimeout(reconnectTimer);
|
|
137
|
+
reconnectTimer = null;
|
|
138
|
+
}
|
|
139
|
+
client?.close().catch(() => {});
|
|
140
|
+
client = null;
|
|
141
|
+
updateStatus("disconnected");
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
connect();
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
serverName: serverItem.server_name,
|
|
148
|
+
config: serverItem,
|
|
149
|
+
get status() {
|
|
150
|
+
return currentStatus;
|
|
151
|
+
},
|
|
152
|
+
send,
|
|
153
|
+
close,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function createReverseServer(
|
|
158
|
+
config: McConfig,
|
|
159
|
+
onEvent: EventHandler,
|
|
160
|
+
onStatusChange: StatusHandler,
|
|
161
|
+
onError?: ErrorHandler,
|
|
162
|
+
): WsServer {
|
|
163
|
+
let client: QueQiaoClient | null = null;
|
|
164
|
+
|
|
165
|
+
const updateStatus = (status: "listening" | "stopped") => {
|
|
166
|
+
onStatusChange("reverse", status);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const start = () => {
|
|
170
|
+
if (!config.reverse_ws_port) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const accessToken = safeString(config.reverse_ws_password);
|
|
175
|
+
|
|
176
|
+
client = createReverseClient(
|
|
177
|
+
{
|
|
178
|
+
port: config.reverse_ws_port,
|
|
179
|
+
path: config.reverse_ws_path || "/minecraft/ws",
|
|
180
|
+
host: "0.0.0.0",
|
|
181
|
+
},
|
|
182
|
+
accessToken ? { accessToken } : {},
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
client.on("connection_open", (_selfName: string) => {
|
|
186
|
+
updateStatus("listening");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
client.on("connection_close", (_selfName: string, _code: number, _reason: string) => {
|
|
190
|
+
updateStatus("stopped");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
client.on("connection_error", (_selfName: string, error: Error) => {
|
|
194
|
+
onError?.("reverse", error);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
client.on("event", (eventData: QueQiaoEvent) => {
|
|
198
|
+
onEvent(eventData);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
client.on("error", () => {});
|
|
202
|
+
|
|
203
|
+
client.connect().catch((err: Error) => {
|
|
204
|
+
onError?.("reverse", err);
|
|
205
|
+
updateStatus("stopped");
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const close = () => {
|
|
210
|
+
client?.close().catch(() => {});
|
|
211
|
+
client = null;
|
|
212
|
+
updateStatus("stopped");
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
start();
|
|
216
|
+
|
|
217
|
+
return {
|
|
218
|
+
get status() {
|
|
219
|
+
return client ? "listening" : "stopped";
|
|
220
|
+
},
|
|
221
|
+
close,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ServerManager } from "../utils/server-manager";
|
|
2
|
+
import type { McConfig } from "../types";
|
|
3
|
+
|
|
4
|
+
export function handleStatus(
|
|
5
|
+
serverManager: ServerManager,
|
|
6
|
+
config: McConfig,
|
|
7
|
+
reply: (text: string) => Promise<void>,
|
|
8
|
+
) {
|
|
9
|
+
if (config.servers.length === 0) {
|
|
10
|
+
return reply("暂无已配置的服务器");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const statusList = serverManager.getStatusList();
|
|
14
|
+
|
|
15
|
+
const lines: string[] = [];
|
|
16
|
+
|
|
17
|
+
config.servers.forEach((server, index) => {
|
|
18
|
+
if (index > 0) {
|
|
19
|
+
lines.push("");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const statusObj = statusList.find((s) => s.name === server.server_name);
|
|
23
|
+
const wsStatus = !server.forward_ws_enabled
|
|
24
|
+
? "未启用"
|
|
25
|
+
: statusObj?.status === "connected"
|
|
26
|
+
? "已连接"
|
|
27
|
+
: statusObj?.status === "connecting"
|
|
28
|
+
? "连接中"
|
|
29
|
+
: "未连接";
|
|
30
|
+
|
|
31
|
+
const rconStatus = !server.rcon_enabled ? "未启用" : "已连接";
|
|
32
|
+
|
|
33
|
+
lines.push(`服务器:${server.server_name}`);
|
|
34
|
+
lines.push(`WebSocket 连接状态:${wsStatus}`);
|
|
35
|
+
lines.push(`RCON 连接状态:${rconStatus}`);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return reply(lines.join("\n"));
|
|
39
|
+
}
|
package/handlers/sync.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ServerManager } from "../utils/server-manager";
|
|
2
|
+
import type { ConfigHandler } from "../utils/config-handler";
|
|
3
|
+
import type { McConfig } from "../types";
|
|
4
|
+
|
|
5
|
+
export async function handleSync(
|
|
6
|
+
serverName: string | undefined,
|
|
7
|
+
enabled: boolean,
|
|
8
|
+
serverManager: ServerManager,
|
|
9
|
+
configHandler: ConfigHandler,
|
|
10
|
+
config: McConfig,
|
|
11
|
+
reply: (text: string) => Promise<void>,
|
|
12
|
+
): Promise<boolean> {
|
|
13
|
+
if (!serverName) {
|
|
14
|
+
// show all servers and their sync status
|
|
15
|
+
if (config.servers.length === 0) {
|
|
16
|
+
await reply("暂无已配置的服务器");
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const lines = config.servers.map((s) => {
|
|
21
|
+
const syncStatus = s.sync_enabled ? "已开启" : "已关闭";
|
|
22
|
+
return `${s.server_name}: ${syncStatus}`;
|
|
23
|
+
});
|
|
24
|
+
await reply(lines.join("\n"));
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const serverItem = configHandler.findServerByName(serverName);
|
|
29
|
+
if (!serverItem) {
|
|
30
|
+
await reply(`未找到服务器「${serverName}」`);
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
await configHandler.updateServerSync(serverName, enabled);
|
|
35
|
+
const action = enabled ? "开启" : "关闭";
|
|
36
|
+
await reply(`${action}了服务器 ${serverName} 的同步`);
|
|
37
|
+
return true;
|
|
38
|
+
}
|
package/index.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { definePlugin, type MiokiContext } from "mioki";
|
|
2
|
+
import type { ConfigService } from "../../src/services/config/tpyes";
|
|
3
|
+
import { createConfigHandler } from "./utils/config-handler";
|
|
4
|
+
import { createServerManager } from "./utils/server-manager";
|
|
5
|
+
import { parseMcCommand } from "./utils/command-router";
|
|
6
|
+
import { handleStatus } from "./handlers/status";
|
|
7
|
+
import { handleSync } from "./handlers/sync";
|
|
8
|
+
import { handleReconnect } from "./handlers/reconnect";
|
|
9
|
+
import { formatMcEvent, formatQqToMc } from "./utils/message-formatter";
|
|
10
|
+
import {
|
|
11
|
+
parseCommand,
|
|
12
|
+
isCommandAllowed,
|
|
13
|
+
formatCommandResult,
|
|
14
|
+
} from "./core/rcon";
|
|
15
|
+
import type { McEvent } from "./types";
|
|
16
|
+
import type { McConfig } from "./types";
|
|
17
|
+
|
|
18
|
+
export default definePlugin({
|
|
19
|
+
name: "mc",
|
|
20
|
+
version: "1.0.0",
|
|
21
|
+
description: "Minecraft服务器与QQ群消息互通插件",
|
|
22
|
+
|
|
23
|
+
async setup(ctx: MiokiContext) {
|
|
24
|
+
const configService = ctx.services?.config as ConfigService | undefined;
|
|
25
|
+
|
|
26
|
+
const configHandler = createConfigHandler(configService);
|
|
27
|
+
await configHandler.register();
|
|
28
|
+
|
|
29
|
+
const config = configHandler.getConfig();
|
|
30
|
+
|
|
31
|
+
const serverManager = createServerManager(
|
|
32
|
+
(serverName, status) => {
|
|
33
|
+
ctx.logger.info(`[MC] 服务器 ${serverName} 状态: ${status}`);
|
|
34
|
+
},
|
|
35
|
+
(event: McEvent) => {
|
|
36
|
+
handleMcEvent(ctx, event, config, configHandler);
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
error: (msg: string) => ctx.logger.error(msg),
|
|
40
|
+
},
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
serverManager.startServers(config);
|
|
44
|
+
|
|
45
|
+
ctx.handle("message", async (event: any) => {
|
|
46
|
+
const text = ctx.text(event).trim();
|
|
47
|
+
const parsed = parseMcCommand(text);
|
|
48
|
+
|
|
49
|
+
if (!parsed || parsed.action === "") {
|
|
50
|
+
if (event.group_id) {
|
|
51
|
+
await forwardToMc(ctx, event, config, configHandler, serverManager);
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
switch (parsed.action) {
|
|
57
|
+
case "状态": {
|
|
58
|
+
await handleStatus(serverManager, config, (msg) => event.reply(msg));
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
case "开启同步": {
|
|
62
|
+
const serverName = parsed.args[0];
|
|
63
|
+
await handleSync(
|
|
64
|
+
serverName,
|
|
65
|
+
true,
|
|
66
|
+
serverManager,
|
|
67
|
+
configHandler,
|
|
68
|
+
config,
|
|
69
|
+
(msg) => event.reply(msg),
|
|
70
|
+
);
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case "关闭同步": {
|
|
74
|
+
const serverName = parsed.args[0];
|
|
75
|
+
await handleSync(
|
|
76
|
+
serverName,
|
|
77
|
+
false,
|
|
78
|
+
serverManager,
|
|
79
|
+
configHandler,
|
|
80
|
+
config,
|
|
81
|
+
(msg) => event.reply(msg),
|
|
82
|
+
);
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
case "重连": {
|
|
86
|
+
if (ctx.isOwner?.(event)) {
|
|
87
|
+
await handleReconnect(serverManager, (msg) => event.reply(msg));
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
ctx.logger.info("Minecraft插件加载成功");
|
|
95
|
+
|
|
96
|
+
return () => {
|
|
97
|
+
serverManager.stopServers();
|
|
98
|
+
ctx.logger.info("Minecraft插件已卸载");
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
async function handleMcEvent(
|
|
104
|
+
ctx: MiokiContext,
|
|
105
|
+
event: McEvent,
|
|
106
|
+
config: McConfig,
|
|
107
|
+
configHandler: ReturnType<typeof createConfigHandler>,
|
|
108
|
+
) {
|
|
109
|
+
const messageText = formatMcEvent(event, config);
|
|
110
|
+
if (!messageText) return;
|
|
111
|
+
|
|
112
|
+
const serverName = event.server_name || "";
|
|
113
|
+
const serverItem = configHandler.findServerByName(serverName);
|
|
114
|
+
if (!serverItem) return;
|
|
115
|
+
|
|
116
|
+
if (serverItem.sync_enabled === false) return;
|
|
117
|
+
|
|
118
|
+
const botList = serverItem.bot_self_id || "";
|
|
119
|
+
const groupList = serverItem.group_list || "";
|
|
120
|
+
|
|
121
|
+
const bots = botList ? [botList] : [];
|
|
122
|
+
const groups = groupList ? [groupList] : [];
|
|
123
|
+
|
|
124
|
+
for (const botId of bots) {
|
|
125
|
+
const bot = ctx.pickBot(Number(botId));
|
|
126
|
+
if (!bot) continue;
|
|
127
|
+
|
|
128
|
+
for (const groupId of groups) {
|
|
129
|
+
try {
|
|
130
|
+
await bot.sendGroupMsg(Number(groupId), messageText);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
ctx.logger.error(`[MC] 发送消息到群 ${groupId} 失败: ${err}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function forwardToMc(
|
|
139
|
+
ctx: MiokiContext,
|
|
140
|
+
event: any,
|
|
141
|
+
config: McConfig,
|
|
142
|
+
configHandler: ReturnType<typeof createConfigHandler>,
|
|
143
|
+
serverManager: ReturnType<typeof createServerManager>,
|
|
144
|
+
) {
|
|
145
|
+
const text = ctx.text(event);
|
|
146
|
+
ctx.logger.debug(`[MC] 收到群消息 group=${event.group_id} text=${text}`);
|
|
147
|
+
|
|
148
|
+
// 检查是否为群消息
|
|
149
|
+
if (!event.group_id) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const servers = configHandler.getServersForGroup(event.group_id);
|
|
154
|
+
if (servers.length === 0) return;
|
|
155
|
+
|
|
156
|
+
const msgList = Array.isArray(event.message)
|
|
157
|
+
? event.message
|
|
158
|
+
: [{ type: "text", text: text }];
|
|
159
|
+
|
|
160
|
+
for (const server of servers) {
|
|
161
|
+
if (server.sync_enabled === false) continue;
|
|
162
|
+
|
|
163
|
+
const commandText = parseCommand(text, server);
|
|
164
|
+
if (commandText !== null) {
|
|
165
|
+
if (server.rcon_enabled === false) continue;
|
|
166
|
+
|
|
167
|
+
const isAllowed = isCommandAllowed(
|
|
168
|
+
commandText,
|
|
169
|
+
server,
|
|
170
|
+
ctx.isOwner?.(event) ?? false,
|
|
171
|
+
event.user_id ?? "",
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
if (isAllowed) {
|
|
175
|
+
try {
|
|
176
|
+
const result = await serverManager.sendToServer(
|
|
177
|
+
server.server_name,
|
|
178
|
+
"send_rcon_command",
|
|
179
|
+
{ command: commandText },
|
|
180
|
+
);
|
|
181
|
+
await event.reply(formatCommandResult(result));
|
|
182
|
+
} catch (err) {
|
|
183
|
+
await event.reply(`执行命令失败: ${err}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
const mcMessage = formatQqToMc(
|
|
191
|
+
event.sender || {},
|
|
192
|
+
msgList,
|
|
193
|
+
config,
|
|
194
|
+
server,
|
|
195
|
+
);
|
|
196
|
+
await serverManager.sendToServer(server.server_name, "broadcast", {
|
|
197
|
+
message: mcMessage,
|
|
198
|
+
});
|
|
199
|
+
} catch (err) {
|
|
200
|
+
ctx.logger.error(`[MC] 发送到服务器 ${server.server_name} 失败: ${err}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mioku-plugin-mc",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Minecraft与QQ群消息互通插件,支持服务器状态监控与RCON命令",
|
|
5
|
+
"main": "index.ts",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"mioku"
|
|
8
|
+
],
|
|
9
|
+
"mioku": {
|
|
10
|
+
"services": [
|
|
11
|
+
"config"
|
|
12
|
+
],
|
|
13
|
+
"help": {
|
|
14
|
+
"title": "Minecraft",
|
|
15
|
+
"description": "Minecraft服务器与QQ群消息互通插件,支持服务器状态监控与RCON命令",
|
|
16
|
+
"commands": [
|
|
17
|
+
{
|
|
18
|
+
"cmd": "/mc 状态",
|
|
19
|
+
"desc": "查看所有已配置服务器的WebSocket连接状态,包括连接中、已断开等",
|
|
20
|
+
"role": "master"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"cmd": "/mc 开启同步",
|
|
24
|
+
"desc": "开启指定服务器的群聊消息同步功能,开启后该服务器可接收和发送群聊消息",
|
|
25
|
+
"usage": "/mc 开启同步 <服务器名称>",
|
|
26
|
+
"role": "master"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"cmd": "/mc 关闭同步",
|
|
30
|
+
"desc": "关闭指定服务器的群聊消息同步功能,关闭后该服务器不再接收和发送群聊消息",
|
|
31
|
+
"usage": "/mc 关闭同步 <服务器名称>",
|
|
32
|
+
"role": "master"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"cmd": "/mc 重连",
|
|
36
|
+
"desc": "断开并重新建立所有服务器的WebSocket连接,用于连接异常时手动恢复",
|
|
37
|
+
"role": "master"
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@cikeyqi/queqiao-node-sdk": "^0.1.3"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/types.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export interface McEvent {
|
|
2
|
+
post_type: string;
|
|
3
|
+
sub_type: string;
|
|
4
|
+
server_name?: string;
|
|
5
|
+
player?: { nickname?: string };
|
|
6
|
+
message?: string;
|
|
7
|
+
raw_message?: unknown;
|
|
8
|
+
death?: { text?: string; translate?: { text?: string } };
|
|
9
|
+
command?: string;
|
|
10
|
+
achievement?: {
|
|
11
|
+
text?: string;
|
|
12
|
+
translate?: { text?: string };
|
|
13
|
+
display?: {
|
|
14
|
+
title?: { text?: string; translate?: { text?: string } };
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ServerConfig {
|
|
20
|
+
server_name: string;
|
|
21
|
+
ws_url: string;
|
|
22
|
+
ws_password?: string;
|
|
23
|
+
ws_max_attempts?: number;
|
|
24
|
+
group_list: string;
|
|
25
|
+
bot_self_id: string;
|
|
26
|
+
command_header?: string;
|
|
27
|
+
command_user?: string[];
|
|
28
|
+
rcon_command_whitelist?: string[];
|
|
29
|
+
mc_qq_chat_image_enable?: boolean;
|
|
30
|
+
mask_word?: string;
|
|
31
|
+
sync_enabled?: boolean;
|
|
32
|
+
// RCON
|
|
33
|
+
rcon_enabled?: boolean;
|
|
34
|
+
rcon_host?: string;
|
|
35
|
+
rcon_port?: number;
|
|
36
|
+
rcon_password?: string;
|
|
37
|
+
// WebSocket direction control (per server)
|
|
38
|
+
forward_ws_enabled?: boolean;
|
|
39
|
+
reverse_ws_enabled?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface McConfig {
|
|
43
|
+
servers: ServerConfig[];
|
|
44
|
+
say_way: string;
|
|
45
|
+
display_server_name: boolean;
|
|
46
|
+
// Reverse WS server (global, not per-server)
|
|
47
|
+
reverse_ws_enabled?: boolean;
|
|
48
|
+
reverse_ws_port?: number;
|
|
49
|
+
reverse_ws_path?: string;
|
|
50
|
+
reverse_ws_password?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const DEFAULT_CONFIG: McConfig = {
|
|
54
|
+
servers: [],
|
|
55
|
+
say_way: "说:",
|
|
56
|
+
display_server_name: true,
|
|
57
|
+
reverse_ws_enabled: false,
|
|
58
|
+
reverse_ws_port: 8080,
|
|
59
|
+
reverse_ws_path: "/minecraft/ws",
|
|
60
|
+
reverse_ws_password: "",
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export function normalizeConfig(raw: Partial<McConfig>): McConfig {
|
|
64
|
+
const servers = Array.isArray(raw.servers) ? raw.servers : [];
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
servers: servers.map((s) => ({
|
|
68
|
+
...s,
|
|
69
|
+
group_list: typeof s.group_list === "string" ? s.group_list.trim() : "",
|
|
70
|
+
bot_self_id: typeof s.bot_self_id === "string" ? s.bot_self_id.trim() : "",
|
|
71
|
+
})),
|
|
72
|
+
say_way: raw.say_way || DEFAULT_CONFIG.say_way,
|
|
73
|
+
display_server_name: raw.display_server_name ?? DEFAULT_CONFIG.display_server_name,
|
|
74
|
+
reverse_ws_enabled: raw.reverse_ws_enabled ?? DEFAULT_CONFIG.reverse_ws_enabled,
|
|
75
|
+
reverse_ws_port: raw.reverse_ws_port ?? DEFAULT_CONFIG.reverse_ws_port,
|
|
76
|
+
reverse_ws_path: raw.reverse_ws_path ?? DEFAULT_CONFIG.reverse_ws_path,
|
|
77
|
+
reverse_ws_password: raw.reverse_ws_password ?? DEFAULT_CONFIG.reverse_ws_password,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export interface ParsedCommand {
|
|
2
|
+
action: string;
|
|
3
|
+
args: string[];
|
|
4
|
+
raw: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function parseMcCommand(text: string): ParsedCommand | null {
|
|
8
|
+
const trimmed = text.trim();
|
|
9
|
+
if (!trimmed.startsWith("/mc")) return null;
|
|
10
|
+
|
|
11
|
+
const remainder = trimmed.slice(3).trim();
|
|
12
|
+
if (!remainder) {
|
|
13
|
+
return { action: "", args: [], raw: "" };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// split into action and args
|
|
17
|
+
const parts = remainder.split(/\s+/);
|
|
18
|
+
const action = parts[0] || "";
|
|
19
|
+
const args = parts.slice(1);
|
|
20
|
+
|
|
21
|
+
return { action, args, raw: remainder };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function matchMcCommand(
|
|
25
|
+
text: string,
|
|
26
|
+
pattern: RegExp
|
|
27
|
+
): RegExpMatchArray | null {
|
|
28
|
+
const parsed = parseMcCommand(text);
|
|
29
|
+
if (!parsed) return null;
|
|
30
|
+
return parsed.raw.match(pattern);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function buildStatusReply(
|
|
34
|
+
serverName: string,
|
|
35
|
+
status: string,
|
|
36
|
+
displayName?: string
|
|
37
|
+
): string {
|
|
38
|
+
const name = displayName || serverName;
|
|
39
|
+
const icon = status === "connected" ? "在线" : status === "connecting" ? "连接中" : "离线";
|
|
40
|
+
return `[${name}] ${icon}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function buildSyncReply(
|
|
44
|
+
serverName: string,
|
|
45
|
+
enabled: boolean,
|
|
46
|
+
displayName?: string
|
|
47
|
+
): string {
|
|
48
|
+
const name = displayName || serverName;
|
|
49
|
+
const action = enabled ? "开启" : "关闭";
|
|
50
|
+
return `${action}了服务器 ${name} 的同步`;
|
|
51
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ConfigService } from "../../../src/services/config/tpyes";
|
|
2
|
+
import { DEFAULT_CONFIG, normalizeConfig, type McConfig } from "../types";
|
|
3
|
+
|
|
4
|
+
export function createConfigHandler(configService: ConfigService | undefined) {
|
|
5
|
+
let currentConfig: McConfig = { ...DEFAULT_CONFIG };
|
|
6
|
+
|
|
7
|
+
const register = async () => {
|
|
8
|
+
if (!configService) return;
|
|
9
|
+
await configService.registerConfig("mc", "base", DEFAULT_CONFIG);
|
|
10
|
+
const raw = await configService.getConfig("mc", "base");
|
|
11
|
+
currentConfig = normalizeConfig(raw);
|
|
12
|
+
configService.onConfigChange("mc", "base", (next) => {
|
|
13
|
+
currentConfig = normalizeConfig(next);
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const getConfig = () => currentConfig;
|
|
18
|
+
|
|
19
|
+
const updateServerSync = async (serverName: string, enabled: boolean) => {
|
|
20
|
+
if (!configService) return;
|
|
21
|
+
const servers = currentConfig.servers.map((s) =>
|
|
22
|
+
s.server_name === serverName ? { ...s, sync_enabled: enabled } : s,
|
|
23
|
+
);
|
|
24
|
+
await configService.updateConfig("mc", "base", { servers });
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const findServerByName = (serverName: string) => {
|
|
28
|
+
return (
|
|
29
|
+
currentConfig.servers.find((s) => s.server_name === serverName) || null
|
|
30
|
+
);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const getServersForGroup = (groupId: string | number): typeof currentConfig.servers => {
|
|
34
|
+
const gid = String(groupId);
|
|
35
|
+
return currentConfig.servers.filter((s) => {
|
|
36
|
+
return s.group_list == gid;
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
register,
|
|
42
|
+
getConfig,
|
|
43
|
+
updateServerSync,
|
|
44
|
+
findServerByName,
|
|
45
|
+
getServersForGroup,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type ConfigHandler = ReturnType<typeof createConfigHandler>;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { McConfig, McEvent } from "../types";
|
|
2
|
+
|
|
3
|
+
export function formatMcEvent(event: McEvent, config: McConfig): string | null {
|
|
4
|
+
const { sub_type, player, message, server_name } = event;
|
|
5
|
+
|
|
6
|
+
const sayWord = config.say_way || "说:";
|
|
7
|
+
const playerName = player?.nickname || "未知玩家";
|
|
8
|
+
|
|
9
|
+
let body: string | null = null;
|
|
10
|
+
|
|
11
|
+
switch (sub_type) {
|
|
12
|
+
case "player_join":
|
|
13
|
+
body = `${playerName} 加入了游戏`;
|
|
14
|
+
break;
|
|
15
|
+
case "player_quit":
|
|
16
|
+
body = `${playerName} 退出了游戏`;
|
|
17
|
+
break;
|
|
18
|
+
case "player_death":
|
|
19
|
+
body =
|
|
20
|
+
event.death?.text ||
|
|
21
|
+
event.death?.translate?.text ||
|
|
22
|
+
`${playerName} 死亡了`;
|
|
23
|
+
break;
|
|
24
|
+
case "player_command":
|
|
25
|
+
body = `${playerName} 使用命令 ${event.command || ""}`.trim();
|
|
26
|
+
break;
|
|
27
|
+
case "player_achievement": {
|
|
28
|
+
const titleRaw =
|
|
29
|
+
event.achievement?.translate?.text ||
|
|
30
|
+
event.achievement?.text ||
|
|
31
|
+
event.achievement?.display?.title?.text ||
|
|
32
|
+
event.achievement?.display?.title?.translate?.text ||
|
|
33
|
+
event.achievement?.display?.title;
|
|
34
|
+
const title = typeof titleRaw === "string" ? titleRaw : null;
|
|
35
|
+
if (title) {
|
|
36
|
+
body =
|
|
37
|
+
event.achievement?.translate?.text || event.achievement?.text
|
|
38
|
+
? title
|
|
39
|
+
: `${playerName} 达成了进度 ${title}`;
|
|
40
|
+
}
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
case "player_chat": {
|
|
44
|
+
const content = message || extractText(event.raw_message);
|
|
45
|
+
if (content) {
|
|
46
|
+
body = `${playerName} ${sayWord} ${content}`.trim();
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
default:
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!body) return null;
|
|
55
|
+
|
|
56
|
+
if (!config.display_server_name) return body;
|
|
57
|
+
const name = server_name || "未知服务器";
|
|
58
|
+
return `[${name}] ${body}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function extractText(raw: unknown): string {
|
|
62
|
+
if (!raw) return "";
|
|
63
|
+
if (typeof raw === "string") return raw.trim();
|
|
64
|
+
if (typeof raw !== "object") return String(raw);
|
|
65
|
+
|
|
66
|
+
const obj = raw as Record<string, unknown>;
|
|
67
|
+
if (typeof obj.text === "string") return obj.text;
|
|
68
|
+
if (typeof obj.translate === "string") return obj.translate;
|
|
69
|
+
|
|
70
|
+
const withArr = Array.isArray(obj.with) ? obj.with : [];
|
|
71
|
+
if (withArr.length > 0) {
|
|
72
|
+
return withArr.map((w) => (typeof w === "string" ? w : "")).join("");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return "";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function formatQqToMc(
|
|
79
|
+
sender: { nickname?: string; card?: string; user_id?: number | string },
|
|
80
|
+
messageArr: Array<{ type: string; text?: string; url?: string }>,
|
|
81
|
+
config: McConfig,
|
|
82
|
+
serverItem: { mc_qq_chat_image_enable?: boolean },
|
|
83
|
+
): Array<{ text: string; color?: string }> {
|
|
84
|
+
const result: Array<{ text: string; color?: string }> = [];
|
|
85
|
+
const sayWord = config.say_way || "说:";
|
|
86
|
+
|
|
87
|
+
const nick =
|
|
88
|
+
sender.nickname || sender.card || String(sender.user_id ?? "未知用户");
|
|
89
|
+
|
|
90
|
+
result.push({ text: nick, color: "green" });
|
|
91
|
+
result.push({ text: ` ${sayWord} `, color: "white" });
|
|
92
|
+
|
|
93
|
+
for (const msg of messageArr) {
|
|
94
|
+
if (msg.type === "text") {
|
|
95
|
+
result.push({
|
|
96
|
+
text: String(msg.text ?? "")
|
|
97
|
+
.replace(/\r/g, "")
|
|
98
|
+
.replace(/\n/g, "\n * "),
|
|
99
|
+
color: "white",
|
|
100
|
+
});
|
|
101
|
+
} else if (msg.type === "image") {
|
|
102
|
+
const imageUrl = String(msg.url || "");
|
|
103
|
+
if (serverItem.mc_qq_chat_image_enable) {
|
|
104
|
+
result.push({ text: `[[CICode,url=${imageUrl},name=图片]]` });
|
|
105
|
+
} else {
|
|
106
|
+
result.push({ text: "[图片]", color: "light_purple" });
|
|
107
|
+
}
|
|
108
|
+
} else {
|
|
109
|
+
result.push({
|
|
110
|
+
text: `[${msg.type}] ${msg.text || ""}`.trim(),
|
|
111
|
+
color: "white",
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createForwardClient,
|
|
3
|
+
createReverseServer,
|
|
4
|
+
type WsClient,
|
|
5
|
+
type WsServer,
|
|
6
|
+
} from "../core/ws-factory";
|
|
7
|
+
import type { McConfig, McEvent } from "../types";
|
|
8
|
+
import type { QueQiaoEvent } from "@cikeyqi/queqiao-node-sdk";
|
|
9
|
+
|
|
10
|
+
export type StatusCallback = (serverName: string, status: string) => void;
|
|
11
|
+
export type EventCallback = (event: McEvent) => void;
|
|
12
|
+
|
|
13
|
+
export function createServerManager(
|
|
14
|
+
onStatusChange: StatusCallback,
|
|
15
|
+
onMcEvent: EventCallback,
|
|
16
|
+
logger?: { error: (msg: string) => void },
|
|
17
|
+
) {
|
|
18
|
+
const forwardClients = new Map<string, WsClient>();
|
|
19
|
+
let reverseServer: WsServer | null = null;
|
|
20
|
+
|
|
21
|
+
const handleEvent = (event: QueQiaoEvent) => {
|
|
22
|
+
onMcEvent(event as McEvent);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const handleError = (serverName: string, error: Error) => {
|
|
26
|
+
logger?.error(`[MC] 服务器 ${serverName} 连接错误: ${error.message}`);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const handleClose = (serverName: string, code: number, reason: string) => {
|
|
30
|
+
logger?.error(`[MC] 服务器 ${serverName} 连接断开: code=${code} reason=${reason || '-'}`);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const startServers = (config: McConfig) => {
|
|
34
|
+
// close existing
|
|
35
|
+
stopServers();
|
|
36
|
+
|
|
37
|
+
// start reverse server if enabled (global, one instance)
|
|
38
|
+
if (config.reverse_ws_enabled && config.reverse_ws_port) {
|
|
39
|
+
reverseServer = createReverseServer(config, handleEvent, onStatusChange, handleError);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// start new forward connections
|
|
43
|
+
for (const server of config.servers) {
|
|
44
|
+
if (!server.server_name) continue;
|
|
45
|
+
|
|
46
|
+
// skip if sync is disabled
|
|
47
|
+
if (server.sync_enabled === false) continue;
|
|
48
|
+
|
|
49
|
+
// Forward WS: bot connects to MC server
|
|
50
|
+
if (server.forward_ws_enabled !== false && server.ws_url) {
|
|
51
|
+
const client = createForwardClient(server, handleEvent, onStatusChange, handleError);
|
|
52
|
+
forwardClients.set(server.server_name, client);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const stopServers = () => {
|
|
58
|
+
forwardClients.forEach((client) => client.close());
|
|
59
|
+
forwardClients.clear();
|
|
60
|
+
reverseServer?.close();
|
|
61
|
+
reverseServer = null;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const reconnectAll = () => {
|
|
65
|
+
forwardClients.forEach((client) => client.close());
|
|
66
|
+
forwardClients.clear();
|
|
67
|
+
reverseServer?.close();
|
|
68
|
+
reverseServer = null;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const getClient = (serverName: string) => forwardClients.get(serverName) || null;
|
|
72
|
+
|
|
73
|
+
const getConnectedNames = () => {
|
|
74
|
+
const names: string[] = [];
|
|
75
|
+
forwardClients.forEach((client, name) => {
|
|
76
|
+
if (client.status === "connected") {
|
|
77
|
+
names.push(name);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
return names;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const isServerConnected = (serverName: string) => {
|
|
84
|
+
const client = forwardClients.get(serverName);
|
|
85
|
+
return client?.status === "connected";
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const sendToServer = async (
|
|
89
|
+
serverName: string,
|
|
90
|
+
api: string,
|
|
91
|
+
data: Record<string, unknown>,
|
|
92
|
+
) => {
|
|
93
|
+
const client = forwardClients.get(serverName);
|
|
94
|
+
if (!client) {
|
|
95
|
+
throw new Error(`服务器 ${serverName} 未配置`);
|
|
96
|
+
}
|
|
97
|
+
if (client.status !== "connected") {
|
|
98
|
+
throw new Error(`服务器 ${serverName} 未连接`);
|
|
99
|
+
}
|
|
100
|
+
return client.send({ type: api, ...data });
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const getStatusList = () => {
|
|
104
|
+
const list: Array<{ name: string; status: string }> = [];
|
|
105
|
+
forwardClients.forEach((client, name) => {
|
|
106
|
+
list.push({ name, status: client.status });
|
|
107
|
+
});
|
|
108
|
+
if (reverseServer) {
|
|
109
|
+
list.push({ name: "reverse", status: reverseServer.status });
|
|
110
|
+
}
|
|
111
|
+
return list;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
startServers,
|
|
116
|
+
stopServers,
|
|
117
|
+
reconnectAll,
|
|
118
|
+
getClient,
|
|
119
|
+
getConnectedNames,
|
|
120
|
+
isServerConnected,
|
|
121
|
+
sendToServer,
|
|
122
|
+
getStatusList,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export type ServerManager = ReturnType<typeof createServerManager>;
|