koishi-plugin-gl-bot 0.0.8 → 0.0.10
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/dist/index.js +1 -1
- package/lib/constants/env.d.ts +0 -0
- package/lib/constants/env.js +0 -0
- package/lib/constants/index.d.ts +1 -0
- package/lib/constants/index.js +4 -0
- package/lib/gl/index.d.ts +83 -0
- package/lib/gl/index.js +51 -0
- package/lib/gl/index.type.d.ts +0 -0
- package/lib/gl/index.type.js +0 -0
- package/lib/gl/type.d.ts +0 -0
- package/lib/gl/type.js +0 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +4 -20
- package/lib/mcsManager/api.d.ts +27 -0
- package/lib/mcsManager/api.js +111 -0
- package/lib/mcsManager/bot.d.ts +14 -0
- package/lib/mcsManager/bot.js +42 -0
- package/lib/mcsManager/commands/index.d.ts +10 -0
- package/lib/mcsManager/commands/index.js +19 -0
- package/lib/mcsManager/commands/list.d.ts +11 -0
- package/lib/mcsManager/commands/list.js +26 -0
- package/lib/mcsManager/commands/restart.d.ts +12 -0
- package/lib/mcsManager/commands/restart.js +61 -0
- package/lib/mcsManager/commands/start.d.ts +12 -0
- package/lib/mcsManager/commands/start.js +53 -0
- package/lib/mcsManager/config.d.ts +18 -0
- package/lib/mcsManager/config.js +24 -0
- package/lib/mcsManager/constants.d.ts +12 -0
- package/lib/mcsManager/constants.js +16 -0
- package/lib/mcsManager/index.d.ts +26 -0
- package/lib/mcsManager/index.js +21 -0
- package/lib/mcsManager/instance.d.ts +18 -0
- package/lib/mcsManager/instance.js +24 -0
- package/lib/mcsManager/panel.d.ts +24 -0
- package/lib/mcsManager/panel.js +123 -0
- package/lib/mcsManager/type.d.ts +115 -0
- package/lib/mcsManager/type.js +2 -0
- package/lib/mcsManager/ws.d.ts +24 -0
- package/lib/mcsManager/ws.js +116 -0
- package/lib/queQiao/index.d.ts +108 -0
- package/lib/queQiao/index.js +437 -0
- package/lib/queQiao/locale/en-US.json +1 -0
- package/lib/queQiao/locale/en-US.yml +9 -0
- package/lib/queQiao/locale/zh-CN.json +1 -0
- package/lib/queQiao/locale/zh-CN.yml +9 -0
- package/lib/queQiao/mcwss.d.ts +61 -0
- package/lib/queQiao/mcwss.js +260 -0
- package/lib/queQiao/values.d.ts +72 -0
- package/lib/queQiao/values.js +155 -0
- package/lib/utils/game.mc.d.ts +4 -0
- package/lib/utils/game.mc.js +29 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +17 -0
- package/package.json +11 -2
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.name = void 0;
|
|
7
|
+
const koishi_1 = require("koishi");
|
|
8
|
+
const lodash_1 = require("lodash");
|
|
9
|
+
const rcon_client_1 = require("rcon-client");
|
|
10
|
+
const ws_1 = require("ws");
|
|
11
|
+
const constants_1 = require("../constants");
|
|
12
|
+
const utils_1 = require("../utils");
|
|
13
|
+
const mcwss_1 = __importDefault(require("./mcwss"));
|
|
14
|
+
const values_1 = require("./values");
|
|
15
|
+
// import zhCN from "./locale/zh-CN.json";
|
|
16
|
+
// import enUS from "./locale/en-US.json";
|
|
17
|
+
const zhCN = require('./locale/zh-CN.json');
|
|
18
|
+
const enUS = require('./locale/en-US.json');
|
|
19
|
+
exports.name = 'minecraft-sync-msg';
|
|
20
|
+
const logger = new koishi_1.Logger('minecraft-sync-msg');
|
|
21
|
+
class MinecraftSyncMsg {
|
|
22
|
+
constructor(ctx, config) {
|
|
23
|
+
this.ctx = ctx;
|
|
24
|
+
this.config = config;
|
|
25
|
+
this.isDisposing = false;
|
|
26
|
+
this.reconnectAttempts = 0;
|
|
27
|
+
this.reconnectIntervalId = null;
|
|
28
|
+
this.initialize();
|
|
29
|
+
}
|
|
30
|
+
initialize() {
|
|
31
|
+
this.setupRcon();
|
|
32
|
+
this.setupWebSocket();
|
|
33
|
+
this.setupWatchChannel();
|
|
34
|
+
this.setupMessageHandler();
|
|
35
|
+
this.setupDisposeHandler();
|
|
36
|
+
this.ctx.i18n.define('zh-CN', zhCN);
|
|
37
|
+
this.ctx.i18n.define('en-US', enUS);
|
|
38
|
+
}
|
|
39
|
+
setupRcon() {
|
|
40
|
+
if (!this.config.rconEnable) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this.rcon = new rcon_client_1.Rcon({
|
|
44
|
+
host: this.config.rconServerHost,
|
|
45
|
+
port: this.config.rconServerPort,
|
|
46
|
+
password: this.config.rconPassword,
|
|
47
|
+
});
|
|
48
|
+
this.connectToRcon().catch(err => {
|
|
49
|
+
logger.error('RCON服务器连接失败:', err);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async connectToRcon() {
|
|
53
|
+
try {
|
|
54
|
+
await this.rcon.connect();
|
|
55
|
+
logger.info('已连接到RCON服务器');
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
logger.error('连接到RCON服务器时发生错误:', err);
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
setupWebSocket() {
|
|
63
|
+
if (this.config.wsServer === '服务端') {
|
|
64
|
+
this.plFork = this.ctx.plugin(mcwss_1.default, this.config);
|
|
65
|
+
// this.plFork = new McWss(this.ctx, this.config);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
this.connectWebSocket();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
connectWebSocket() {
|
|
73
|
+
const headers = {
|
|
74
|
+
'x-self-name': encodeURIComponent(this.config.serverName),
|
|
75
|
+
Authorization: `Bearer ${this.config.Token}`,
|
|
76
|
+
'x-client-origin': 'NOTkoishi',
|
|
77
|
+
};
|
|
78
|
+
this.ws = new ws_1.WebSocket(`ws://${this.config.wsHost}:${this.config.wsPort}/minecraft/ws`, {
|
|
79
|
+
headers,
|
|
80
|
+
});
|
|
81
|
+
this.bindWebSocketEvents();
|
|
82
|
+
}
|
|
83
|
+
bindWebSocketEvents() {
|
|
84
|
+
if (!this.ws) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
this.ws.on('open', () => this.handleWsOpen());
|
|
88
|
+
this.ws.on('message', buffer => this.handleWsMessage(buffer));
|
|
89
|
+
this.ws.on('close', () => this.handleWsClose());
|
|
90
|
+
this.ws.on('error', err => this.handleWsError(err));
|
|
91
|
+
}
|
|
92
|
+
handleWsOpen() {
|
|
93
|
+
logger.info('成功连上websocket服务器');
|
|
94
|
+
if (!this.config.hideConnect) {
|
|
95
|
+
this.broadcastToChannels('Websocket服务器连接成功!');
|
|
96
|
+
}
|
|
97
|
+
if (!constants_1.IS_DEV) {
|
|
98
|
+
const msgData = {
|
|
99
|
+
api: 'broadcast',
|
|
100
|
+
data: {
|
|
101
|
+
message: [
|
|
102
|
+
{
|
|
103
|
+
text: this.extractAndRemoveColor(this.config.joinMsg).output,
|
|
104
|
+
color: this.extractAndRemoveColor(this.config.joinMsg).color || 'gold',
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
this.ws?.send(JSON.stringify(msgData));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
handleWsMessage(buffer) {
|
|
113
|
+
// Convert RawData to string
|
|
114
|
+
const dataStr = buffer.toString();
|
|
115
|
+
let data;
|
|
116
|
+
try {
|
|
117
|
+
data = JSON.parse(dataStr);
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
logger.error('Failed to parse WebSocket message:', err);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const eventName = data.event_name ? (0, values_1.getListeningEvent)(data.event_name) : '';
|
|
124
|
+
if (eventName === 'PlayerCommandPreprocessEvent') {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
// console.log(data);
|
|
128
|
+
// if (!getSubscribedEvents(this.config.event).includes(eventName)) return;
|
|
129
|
+
// let sendMsg = `[${data.server_name}](${eventTrans[eventName].name}) ${
|
|
130
|
+
// eventTrans[eventName].action ? data.player?.nickname + ' ' : ''
|
|
131
|
+
// }${
|
|
132
|
+
// eventTrans[eventName].action ? eventTrans[eventName].action + ' ' : ''
|
|
133
|
+
// }${
|
|
134
|
+
// data.message ? data.message : ''
|
|
135
|
+
// }`
|
|
136
|
+
let sendMsg = koishi_1.h
|
|
137
|
+
.unescape(data.message ? data.message : '')
|
|
138
|
+
.replaceAll('&', '&')
|
|
139
|
+
.replaceAll(/<\/?template>/gi, '')
|
|
140
|
+
.replaceAll(/§./g, '');
|
|
141
|
+
sendMsg = sendMsg.replaceAll(/<json.*\/>/gi, '<json消息>');
|
|
142
|
+
const imageMatch = sendMsg.match(/(https?|file):\/\/.*\.(jpg|jpeg|webp|ico|gif|jfif|bmp|png)/gi);
|
|
143
|
+
const sendImage = imageMatch?.[0];
|
|
144
|
+
if (sendImage) {
|
|
145
|
+
sendMsg = sendMsg.replace(sendImage, `<img src="${sendImage}" />`);
|
|
146
|
+
}
|
|
147
|
+
sendMsg = this.ctx.i18n.render([this.config.locale ? this.config.locale : 'zh-CN'], [`minecraft-sync-msg.action.${eventName}`], [data.player?.nickname, sendMsg]);
|
|
148
|
+
if (data.server_name && sendMsg) {
|
|
149
|
+
this.broadcastToChannels(sendMsg);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
handleWsClose() {
|
|
153
|
+
if (this.isDisposing) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (!this.config.hideConnect) {
|
|
157
|
+
this.broadcastToChannels('与Websocket服务器断开连接!');
|
|
158
|
+
}
|
|
159
|
+
logger.error('非正常与Websocket服务器断开连接!');
|
|
160
|
+
this.ws = undefined;
|
|
161
|
+
this.reconnectWebSocket();
|
|
162
|
+
}
|
|
163
|
+
handleWsError(err) {
|
|
164
|
+
if (this.isDisposing) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (!this.config.hideConnect) {
|
|
168
|
+
this.broadcastToChannels('与Websocket服务器断通信时发生错误!');
|
|
169
|
+
}
|
|
170
|
+
logger.error('与Websocket服务器断通信时发生错误:', err);
|
|
171
|
+
}
|
|
172
|
+
setupWatchChannel() {
|
|
173
|
+
this.ctx.on('message', async (session) => {
|
|
174
|
+
this.config.watchChannel.forEach(channel => {
|
|
175
|
+
const [platform, channelId] = channel.split(':');
|
|
176
|
+
if (platform === session.platform && channelId === session.channelId) {
|
|
177
|
+
const msgData = {
|
|
178
|
+
api: 'broadcast',
|
|
179
|
+
data: {
|
|
180
|
+
message: [
|
|
181
|
+
{
|
|
182
|
+
text: `[${session.event._data.group_name}] <${session.username}> ${(0, utils_1.clearSessionContentToMcMessage)(session.content)}`,
|
|
183
|
+
color: this.extractAndRemoveColor(this.config.joinMsg).color ||
|
|
184
|
+
'white',
|
|
185
|
+
},
|
|
186
|
+
],
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
this.ws?.send(JSON.stringify(msgData));
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
async reconnectWebSocket() {
|
|
195
|
+
this.clearReconnectInterval();
|
|
196
|
+
this.reconnectIntervalId = setInterval(async () => {
|
|
197
|
+
if (this.reconnectAttempts >= this.config.maxReconnectCount) {
|
|
198
|
+
logger.error(`已达到最大重连次数 (${this.config.maxReconnectCount} 次),停止重连。`);
|
|
199
|
+
this.clearReconnectInterval();
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
this.reconnectAttempts++;
|
|
203
|
+
logger.info(`尝试第 ${this.reconnectAttempts} 次重连...`);
|
|
204
|
+
try {
|
|
205
|
+
const headers = {
|
|
206
|
+
'x-self-name': encodeURIComponent(this.config.serverName),
|
|
207
|
+
Authorization: `Bearer ${this.config.Token}`,
|
|
208
|
+
'x-client-origin': 'koishi',
|
|
209
|
+
};
|
|
210
|
+
const ws = new ws_1.WebSocket(`ws://${this.config.wsHost}:${this.config.wsPort}/minecraft/ws`, {
|
|
211
|
+
headers,
|
|
212
|
+
});
|
|
213
|
+
ws.on('open', () => {
|
|
214
|
+
logger.info('WebSocket 重连成功');
|
|
215
|
+
this.clearReconnectInterval();
|
|
216
|
+
this.ws = ws;
|
|
217
|
+
this.bindWebSocketEvents();
|
|
218
|
+
});
|
|
219
|
+
ws.on('error', err => {
|
|
220
|
+
logger.error('重连时发生错误:', err);
|
|
221
|
+
ws.close();
|
|
222
|
+
});
|
|
223
|
+
ws.on('close', () => {
|
|
224
|
+
if (!this.isDisposing) {
|
|
225
|
+
logger.info('WebSocket 再次断开,将继续尝试重连...');
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
logger.error('创建WebSocket时发生错误:', err);
|
|
231
|
+
if (this.reconnectAttempts >= this.config.maxReconnectCount) {
|
|
232
|
+
this.clearReconnectInterval();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}, this.config.maxReconnectInterval);
|
|
236
|
+
}
|
|
237
|
+
clearReconnectInterval() {
|
|
238
|
+
if (this.reconnectIntervalId) {
|
|
239
|
+
clearInterval(this.reconnectIntervalId);
|
|
240
|
+
this.reconnectIntervalId = null;
|
|
241
|
+
}
|
|
242
|
+
this.reconnectAttempts = 0;
|
|
243
|
+
}
|
|
244
|
+
setupMessageHandler() {
|
|
245
|
+
this.ctx.on('message', async (session) => {
|
|
246
|
+
if (!this.isValidChannel(session)) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (this.isMessageCommand(session)) {
|
|
250
|
+
await this.handleMessageCommand(session);
|
|
251
|
+
}
|
|
252
|
+
if (this.isRconCommand(session)) {
|
|
253
|
+
await this.handleRconCommand(session);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
isValidChannel(session) {
|
|
258
|
+
return (this.config.sendToChannel.includes(`${session.platform}:${session.channelId}`) || session.platform === 'sandbox');
|
|
259
|
+
}
|
|
260
|
+
isMessageCommand(session) {
|
|
261
|
+
return (session.content.startsWith(this.config.sendprefix) &&
|
|
262
|
+
session.content !== this.config.sendprefix);
|
|
263
|
+
}
|
|
264
|
+
isRconCommand(session) {
|
|
265
|
+
return (this.config.rconEnable &&
|
|
266
|
+
this.config.cmdprefix &&
|
|
267
|
+
session.content.startsWith(this.config.cmdprefix) &&
|
|
268
|
+
session.content !== this.config.cmdprefix);
|
|
269
|
+
}
|
|
270
|
+
async handleMessageCommand(session) {
|
|
271
|
+
let imgurl = '<unknown image url>';
|
|
272
|
+
if (session.content.includes('<img') &&
|
|
273
|
+
koishi_1.h.select(session.content, 'img')[0]?.type === 'img' &&
|
|
274
|
+
koishi_1.h.select(session.content, 'img')[0]?.attrs?.src) {
|
|
275
|
+
imgurl = koishi_1.h.select(session.content, 'img')[0].attrs.src;
|
|
276
|
+
}
|
|
277
|
+
const msg = session.content
|
|
278
|
+
.replaceAll('&', '&')
|
|
279
|
+
.replaceAll(/<\/?template>/gi, '')
|
|
280
|
+
.replace(this.config.sendprefix, '')
|
|
281
|
+
.replaceAll(/<json.*\/>/gi, '<json消息>')
|
|
282
|
+
.replaceAll(/<video.*\/>/gi, '<视频消息>')
|
|
283
|
+
.replaceAll(/<audio.*\/>/gi, '<音频消息>')
|
|
284
|
+
.replaceAll(/<img.*\/>/gi, `[[CICode,url=${imgurl}]]`)
|
|
285
|
+
.replaceAll(/<at.*\/>/gi, `@[${koishi_1.h.select(session.content, 'at')[0]?.attrs?.name
|
|
286
|
+
? koishi_1.h.select(session.content, 'at')[0]?.attrs?.name
|
|
287
|
+
: koishi_1.h.select(session.content, 'at')[0]?.attrs?.id}]`);
|
|
288
|
+
try {
|
|
289
|
+
const { output, color } = this.extractAndRemoveColor(msg);
|
|
290
|
+
const msgData = {
|
|
291
|
+
api: 'broadcast',
|
|
292
|
+
data: {
|
|
293
|
+
message: [
|
|
294
|
+
{
|
|
295
|
+
// text: `(${session.platform})[${session.event.user.name}] ` + output,
|
|
296
|
+
text: this.ctx.i18n
|
|
297
|
+
.render([this.config.locale ? this.config.locale : 'zh-CN'], ['minecraft-sync-msg.message.MCReceivePrefix'], [session.platform, session.userId])
|
|
298
|
+
.map(element => element.attrs.content)
|
|
299
|
+
.join('') + output,
|
|
300
|
+
color: color || 'white',
|
|
301
|
+
},
|
|
302
|
+
],
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
this.ws?.send(JSON.stringify(msgData));
|
|
306
|
+
this.ctx.logger.info(JSON.stringify(msgData));
|
|
307
|
+
}
|
|
308
|
+
catch (err) {
|
|
309
|
+
logger.error('[minecraft-sync-msg] 消息发送到WebSocket服务端失败', err);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
async handleRconCommand(session) {
|
|
313
|
+
const cmd = session.content
|
|
314
|
+
.replaceAll('&', '§')
|
|
315
|
+
.replaceAll('&', '§')
|
|
316
|
+
.replaceAll(this.config.cmdprefix, '');
|
|
317
|
+
let response;
|
|
318
|
+
if (this.config.alluser) {
|
|
319
|
+
response = await this.sendRconCommand(cmd);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
if (this.config.superuser.includes(session.userId)) {
|
|
323
|
+
response = cmd.includes(this.config.cannotCmd)
|
|
324
|
+
? '危险命令,禁止使用'
|
|
325
|
+
: await this.sendRconCommand(cmd);
|
|
326
|
+
response = response || '该命令无反馈';
|
|
327
|
+
}
|
|
328
|
+
else if (cmd.includes(this.config.commonCmd)) {
|
|
329
|
+
response = this.config.cannotCmd.includes(cmd)
|
|
330
|
+
? '危险命令,禁止使用'
|
|
331
|
+
: await this.sendRconCommand(cmd);
|
|
332
|
+
response = response || '该命令无反馈';
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
response = '无权使用该命令';
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
session.send(response?.replaceAll(/§./g, '') || '');
|
|
339
|
+
}
|
|
340
|
+
async sendRconCommand(command) {
|
|
341
|
+
try {
|
|
342
|
+
const response = await this.rcon.send(command);
|
|
343
|
+
return response;
|
|
344
|
+
}
|
|
345
|
+
catch (err) {
|
|
346
|
+
logger.error('发送RCON命令时发生错误:', err);
|
|
347
|
+
throw err;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
extractAndRemoveColor(input) {
|
|
351
|
+
const regex = /&(\w+)&/;
|
|
352
|
+
const match = input.match(regex);
|
|
353
|
+
if (match) {
|
|
354
|
+
const color = match[1];
|
|
355
|
+
const output = input.replace(regex, '');
|
|
356
|
+
return { output, color };
|
|
357
|
+
}
|
|
358
|
+
return { output: input, color: '' };
|
|
359
|
+
}
|
|
360
|
+
broadcastToChannels(message) {
|
|
361
|
+
this.ctx.bots.forEach((bot) => {
|
|
362
|
+
const channels = this.config.sendToChannel
|
|
363
|
+
.filter(str => str.includes(`${bot.platform}`))
|
|
364
|
+
.map(str => str.replace(`${bot.platform}:`, ''));
|
|
365
|
+
console.log(this.config.sendToChannel);
|
|
366
|
+
if (process.env.NODE_ENV === 'development') {
|
|
367
|
+
logger.info((0, lodash_1.isString)(message)
|
|
368
|
+
? message
|
|
369
|
+
: message.map(el => el.attrs.content).join(''));
|
|
370
|
+
}
|
|
371
|
+
bot.broadcast(channels, message, 0);
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
setupDisposeHandler() {
|
|
375
|
+
this.ctx.on('dispose', async () => {
|
|
376
|
+
this.isDisposing = true;
|
|
377
|
+
await this.dispose();
|
|
378
|
+
this.isDisposing = false;
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
async dispose() {
|
|
382
|
+
if (this.plFork) {
|
|
383
|
+
await this.plFork.dispose();
|
|
384
|
+
}
|
|
385
|
+
this.ctx.registry.delete(mcwss_1.default);
|
|
386
|
+
if (this.ws) {
|
|
387
|
+
this.ws.removeAllListeners();
|
|
388
|
+
if (this.ws.readyState === ws_1.WebSocket.OPEN) {
|
|
389
|
+
this.ws.close();
|
|
390
|
+
}
|
|
391
|
+
this.ws = undefined;
|
|
392
|
+
}
|
|
393
|
+
this.clearReconnectInterval();
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
(function (MinecraftSyncMsg) {
|
|
397
|
+
// export interface Config extends WsConf, RconConf {
|
|
398
|
+
// sendToChannel: string[];
|
|
399
|
+
// sendprefix: string;
|
|
400
|
+
// cmdprefix: string;
|
|
401
|
+
// hideConnect: boolean;
|
|
402
|
+
// locale: string;
|
|
403
|
+
// watchChannel: string[];
|
|
404
|
+
// }
|
|
405
|
+
MinecraftSyncMsg.Config = koishi_1.Schema.intersect([
|
|
406
|
+
values_1.WsConf,
|
|
407
|
+
values_1.RconConf,
|
|
408
|
+
koishi_1.Schema.object({
|
|
409
|
+
sendToChannel: koishi_1.Schema.array(String).description('消息发送到目标群组格式{platform}:{groupId}'),
|
|
410
|
+
watchChannel: koishi_1.Schema.array(String).description('消息观察频道目标群组格式{platform}:{groupId}'),
|
|
411
|
+
sendprefix: koishi_1.Schema.string()
|
|
412
|
+
.default('.#')
|
|
413
|
+
.description('消息发送前缀(不可与命令发送前缀相同,可以为空)'),
|
|
414
|
+
cmdprefix: koishi_1.Schema.string()
|
|
415
|
+
.default('./')
|
|
416
|
+
.description('命令发送前缀(不可与消息发送前缀相同)'),
|
|
417
|
+
hideConnect: koishi_1.Schema.boolean()
|
|
418
|
+
.default(true)
|
|
419
|
+
.description('关闭连接成功/失败提示'),
|
|
420
|
+
locale: koishi_1.Schema.union(['zh-CN', 'en-US'])
|
|
421
|
+
.default('zh-CN')
|
|
422
|
+
.description('本地化语言选择,zh_CN为中文,en-US为英文'),
|
|
423
|
+
}).description('基础配置'),
|
|
424
|
+
]);
|
|
425
|
+
MinecraftSyncMsg.usage = `
|
|
426
|
+
插件使用详情请看 [v2.x](https://blog.iin0.cn/views/myblog/mc/wskoishitomc.html)
|
|
427
|
+
*** 注意 ***
|
|
428
|
+
* 命令发送前缀(不能为空)和消息发送前缀(可以为空)不能相同
|
|
429
|
+
* forge端不支持PlayerCommandPreprocessEvent事件
|
|
430
|
+
* * 原版端仅支持聊天、加入、离开事件
|
|
431
|
+
* sendToChannel的格式为{platform}:{groupId},如:\`discord:123456\`
|
|
432
|
+
* v2.1.0-beta可以通过\`本地化\`自定义对应事件发送格式
|
|
433
|
+
- action节点的{0}是玩家名称{1}是消息
|
|
434
|
+
- message节点中的{0}是平台{1}是用户名
|
|
435
|
+
`;
|
|
436
|
+
})(MinecraftSyncMsg || (MinecraftSyncMsg = {}));
|
|
437
|
+
exports.default = MinecraftSyncMsg;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"minecraft-sync-msg":{"action":{"PlayerJoinEvent":"[join] Player {0} joined the game","PlayerCommandPreprocessEvent":"[command] Player {0} executed command","PlayerDeathEvent":"[death] Player {0} meet the god","AsyncPlayerChatEvent":"[chat] Player {0} said: {1}","PlayerQuitEvent":"[quit] Player {0} left the game"},"message":{"MCReceivePrefix":"({0})[{1}]"}}}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
minecraft-sync-msg:
|
|
2
|
+
action:
|
|
3
|
+
PlayerJoinEvent: '[join] Player {0} joined the game'
|
|
4
|
+
PlayerCommandPreprocessEvent: '[command] Player {0} executed command'
|
|
5
|
+
PlayerDeathEvent: '[death] Player {0} meet the god'
|
|
6
|
+
AsyncPlayerChatEvent: '[chat] Player {0} said: {1}'
|
|
7
|
+
PlayerQuitEvent: '[quit] Player {0} left the game'
|
|
8
|
+
message:
|
|
9
|
+
MCReceivePrefix: '({0})[{1}]'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"minecraft-sync-msg":{"action":{"PlayerJoinEvent":"[加入]{0} 加入了服务器!","PlayerCommandPreprocessEvent":"[指令]{0} 执行了指令","PlayerDeathEvent":"[死亡]{0} 与山长眠!","AsyncPlayerChatEvent":"[聊天]{0} 说:{1}","PlayerQuitEvent":"[离开]{0} 离开了服务器!"},"message":{"MCReceivePrefix":"({0})[{1}]"}}}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
minecraft-sync-msg:
|
|
2
|
+
action:
|
|
3
|
+
PlayerJoinEvent: '[加入]{0} 加入了服务器!'
|
|
4
|
+
PlayerCommandPreprocessEvent: '[指令]{0} 执行了指令'
|
|
5
|
+
PlayerDeathEvent: '[死亡]{0} 与山长眠!'
|
|
6
|
+
AsyncPlayerChatEvent: '[聊天]{0} 说:{1}'
|
|
7
|
+
PlayerQuitEvent: '[离开]{0} 离开了服务器!'
|
|
8
|
+
message:
|
|
9
|
+
MCReceivePrefix: '({0})[{1}]'
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { IncomingMessage } from 'http';
|
|
2
|
+
import { Context, Schema } from 'koishi';
|
|
3
|
+
import { WsConf } from './values';
|
|
4
|
+
declare class McWss {
|
|
5
|
+
private conf;
|
|
6
|
+
private logger;
|
|
7
|
+
private wss;
|
|
8
|
+
private ctx;
|
|
9
|
+
private connectedClients;
|
|
10
|
+
constructor(ctx: Context, cfg: McWss.Config);
|
|
11
|
+
private setupWebSocketHandlers;
|
|
12
|
+
private setupMessageHandler;
|
|
13
|
+
verifyHeaders(headers: IncomingMessage['headers']): {
|
|
14
|
+
valid: boolean;
|
|
15
|
+
clientOrigin?: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export declare function extractAndRemoveColor(input: string): {
|
|
19
|
+
output: string;
|
|
20
|
+
color: string;
|
|
21
|
+
};
|
|
22
|
+
declare namespace McWss {
|
|
23
|
+
interface Config extends WsConf {
|
|
24
|
+
sendToChannel: string[];
|
|
25
|
+
sendprefix: string;
|
|
26
|
+
hideConnect: boolean;
|
|
27
|
+
locale: string | any;
|
|
28
|
+
}
|
|
29
|
+
const Config: Schema<Schemastery.ObjectS<{
|
|
30
|
+
wsServer: Schema<"客户端" | "服务端", "客户端" | "服务端">;
|
|
31
|
+
wsHost: Schema<string, string>;
|
|
32
|
+
wsPort: Schema<number, number>;
|
|
33
|
+
Token: Schema<string, string>;
|
|
34
|
+
serverName: Schema<string, string>;
|
|
35
|
+
joinMsg: Schema<string, string>;
|
|
36
|
+
event: Schema<number | readonly ("AsyncPlayerChatEvent" | "PlayerCommandPreprocessEvent" | "PlayerDeathEvent" | "PlayerJoinEvent" | "PlayerQuitEvent")[], number>;
|
|
37
|
+
maxReconnectCount: Schema<number, number>;
|
|
38
|
+
maxReconnectInterval: Schema<number, number>;
|
|
39
|
+
}> | Schemastery.ObjectS<{
|
|
40
|
+
sendToChannel: Schema<string[], string[]>;
|
|
41
|
+
sendprefix: Schema<string, string>;
|
|
42
|
+
hideConnect: Schema<boolean, boolean>;
|
|
43
|
+
locale: Schema<"zh-CN" | "en-US", "zh-CN" | "en-US">;
|
|
44
|
+
}>, {
|
|
45
|
+
wsServer: "客户端" | "服务端";
|
|
46
|
+
wsHost: string;
|
|
47
|
+
wsPort: number;
|
|
48
|
+
Token: string;
|
|
49
|
+
serverName: string;
|
|
50
|
+
joinMsg: string;
|
|
51
|
+
event: number;
|
|
52
|
+
maxReconnectCount: number;
|
|
53
|
+
maxReconnectInterval: number;
|
|
54
|
+
} & import("cosmokit").Dict & {
|
|
55
|
+
sendToChannel: string[];
|
|
56
|
+
sendprefix: string;
|
|
57
|
+
hideConnect: boolean;
|
|
58
|
+
locale: "zh-CN" | "en-US";
|
|
59
|
+
}>;
|
|
60
|
+
}
|
|
61
|
+
export default McWss;
|