This repository was archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoCQSync.lxl.js
More file actions
629 lines (576 loc) · 14.7 KB
/
Copy pathGoCQSync.lxl.js
File metadata and controls
629 lines (576 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/* global JsonConfigFile WSClient mc ll logger Format */
// LiteLoaderScript Dev Helper
/// <reference path="../HelperLib/src/index.d.ts"/>
// 控制台颜色控制符
const conGreen = '\u001b[0;32m';
// const conWhite = '\u001b[0;37m';
const conYellow = '\u001b[0;33m';
const conRed = '\u001b[0;31m';
// const conBlue = '\u001b[0;34m'; // 太暗了
const conMagenta = '\u001b[0;35m';
const conCyan = '\u001b[0;36m';
const conReset = '\u001b[0m';
const config = new JsonConfigFile('plugins/GoCQSync/config.json');
config.init('ws_url', 'ws://127.0.0.1:6700');
config.init('superusers', ['']);
config.init('enable_groups', ['']);
config.init('log_level', 4);
config.init('cmd_prefix', '/');
config.init('cmd_status', '查询');
config.init('allow_cmd', ['^list$']);
const ws = new WSClient();
const reqCache = new Map();
// let detectTaskId;
logger.setTitle(`GoCQSync`);
logger.setLogLevel(config.get('log_level'));
/**
* 生成指定区间随机整数
* @param {number} min
* @param {number} max
* @returns {number}
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* 向主线程抛出error
* @param {Error} e error
*/
function throwError(e) {
setTimeout(() => {
throw e;
}, 0);
}
/**
* 向GoCQ发送数据
* @param {object} data
*/
function sendData(data) {
if (!ws.send(JSON.stringify(data))) {
logger.error('向GoCQ发送数据失败!');
throw new Error('发送数据失败');
}
}
/**
* 调用GoCQ API
* @param {string} name
* @param {object} data
* @param {(data:object)void} callback
*/
function callAPI(name, data, callback = () => {}) {
const echo = new Date().getTime().toString();
const echoColor = `\u001b[0;${getRandomInt(31, 36)}m`; // 随机前景色
const logStart = `调用API"${conGreen}${name}${conReset}",echo=${echoColor}${echo}${conReset},`;
logger.debug(`${logStart}等待返回数据`);
try {
sendData({ action: name, params: data, echo });
} catch {
callback(undefined);
logger.debug(`${logStart}${conRed}失败`);
}
const checkFunc = () => {
const ret = reqCache.get(echo);
if (ret !== undefined) {
logger.debug(`${logStart}已返回数据:${conGreen}${JSON.stringify(ret)}`);
reqCache.delete(echo);
callback(ret);
} else {
setTimeout(checkFunc, 0); // async
}
};
checkFunc();
}
/**
* 发送群消息
* @param {number} groupId
* @param {string|Array<object>} message
* @param {boolean} autoEscape 是否自动转CQ码
* @param {Function} callback
*/
function sendGroupMsg(
groupId,
message,
autoEscape = false,
callback = () => {}
) {
return callAPI(
'send_group_msg',
{
group_id: groupId,
message,
auto_escape: autoEscape,
},
callback
);
}
/**
* 获取bot运行状态
* @param {Function} callback
*/
/*
function getStatus(callback = () => {}) {
return callAPI('get_status', {}, callback);
}
*/
/**
* 向所有启用群发消息
* @param {string|Array<object>} msg
* @param {boolean} autoEscape 是否自动转CQ码
*/
function sendToAllEnableGroups(msg, autoEscape = false) {
config.get('enable_groups').forEach((g) => {
sendGroupMsg(Number(g), msg, autoEscape);
});
}
/**
* 消息text转array
* @param {string} msg
*/
function text2Array(msg) {
/**
* CQCode Object
* @param {string} type
* @param {object} data
*/
function getCodeObj(type, data) {
return { type, data };
}
// https://github.com/nonebot/adapter-onebot/blob/master/nonebot/adapters/onebot/v11/message.py#L301
const cqRegex = /\[CQ:([a-zA-Z0-9-_.]+)((?:,[a-zA-Z0-9-_.]+=[^,\]]*)*),?\]/gu;
const tmp = [];
let lastRightBracket = 0;
for (;;) {
const { lastIndex } = cqRegex;
const result = cqRegex.exec(msg);
// let msg='123[CQ:test],456[CQ:tteesstt,a=1,b=a]'
// ['[CQ:tteesstt,a=1,b=a]', 'tteesstt', ',a=1,b=a', index: 16, input: '123[CQ:test],456[CQ:tteesstt,a=1,b=a]', groups: undefined]
if (result) {
const { index, 0: raw, 1: type, 2: dataRaw } = result;
if (index > 0 && lastIndex !== index)
tmp.push(getCodeObj('text', { text: msg.substring(lastIndex, index) }));
const data = {};
const dataArr = dataRaw.split(',');
dataArr.shift(); // ['a=1', 'b=a']
dataArr.forEach((it) => {
const [x, y] = it.split('=');
data[x] = y;
});
tmp.push(getCodeObj(type, data));
lastRightBracket = index + raw.length;
} else {
break;
}
}
const tail = msg.substring(lastRightBracket);
if (tail) tmp.push(getCodeObj('text', { text: tail }));
return tmp;
}
/**
* 消息object转文本
* @param {Array<object>} msg
* @param {string} head 特殊消息格式化时左侧文本
* @param {string} tail 特殊消息格式化时右侧文本
*/
function messageObjectToString(
msg,
head = `${Format.Aqua}[`,
tail = `]${Format.Clear}`
) {
/**
* 给文字两头加上文本(处理特殊消息格式化)
* @param {string} t text
*/
function addHeadAndTail(t) {
return `${head}${t}${tail}`;
}
return msg
.map((i) => {
let txt;
switch (i.type) {
case 'text':
return i.data.text;
case 'face':
txt = '表情';
break;
case 'record':
txt = `${i.data.magic === '1' ? '变声' : ''}语音`;
break;
case 'video':
txt = '视频';
break;
case 'at':
txt = `@${i.data.qq}`;
break;
case 'rps':
txt = '猜拳';
break;
case 'dice':
txt = '骰子';
break;
case 'share': {
const { title, url } = i.data;
txt = `分享:${title}(${url})`;
break;
}
case 'contact': {
const { type, id } = i.data;
txt = `推荐${type === 'qq' ? '联系人' : '群聊'}:${id}`;
break;
}
case 'location': {
const {
lat, // 纬度
lon, // 经度
} = i.data;
txt = `位置:${lon}, ${lat}`;
break;
}
case 'image':
txt = i.data.subType === '1' ? '动画表情' : '图片';
break;
case 'reply':
txt = '回复';
break;
case 'redbag':
txt = `红包:${i.data.title}`;
break;
case 'forward':
txt = '合并转发';
break;
case 'xml': // fallthrough
case 'json':
txt = '卡片消息';
break;
default:
txt = '特殊消息';
break;
}
return addHeadAndTail(txt);
})
.join('');
}
/**
* 消息object提取纯文本
* @param {Array<object>} msg
*/
function messageObjectExtractString(msg) {
return msg
.map((it) => {
if (it.type === 'text') {
return it.data.text;
}
return '';
})
.join('');
}
/**
* 获取服务器状态
* @returns {string}
*/
function getServerStat() {
const playerLi = mc.getOnlinePlayers();
return `
游戏版本:${mc.getBDSVersion()}
协议版本:${mc.getServerProtocolVersion()}
LL版本:${ll.versionString()}
插件数量:${ll.listPlugins().length}
玩家列表(共 ${playerLi.length} 人):
${playerLi
.map((p) => {
const dv = p.getDevice();
return `${p.realName} | ${dv.os} | ${dv.avgPing}ms(${dv.avgPacketLoss}% loss)`;
})
.join('\n')}
`.trim();
}
/**
* @param {string} cmd
* @returns {boolean}
*/
function isAllowedCmd(cmd) {
const allowed = config.get('allow_cmd');
for (const reg of allowed) {
if (RegExp(reg).test(cmd)) return true;
}
return false;
}
/**
* 群消息处理
* @param {object} ev
*/
function processGroupMsg(ev) {
const {
self_id: selfId,
user_id: userId,
// raw_message: rawMessage,
group_id: groupId,
sender: { nickname, card },
} = ev;
let { message } = ev;
/**
* 往来源群聊发送消息
* @param {string|Array<object>} msg
* @param {boolean} autoEscape 是否自动转CQ码
*/
function fastReply(msg, autoEscape = false) {
sendGroupMsg(groupId, msg, autoEscape);
}
if (!(message instanceof Array)) message = text2Array(message);
const txtMsg = messageObjectExtractString(message);
// log输出
const nick = card === '' ? nickname : card;
const userColor = userId === selfId ? conGreen : conYellow;
logger.info(
`${conMagenta}Group ${groupId}${conReset} -> ` +
`${userColor}${nick}(${userId})${conReset}:` +
`${messageObjectToString(message, `${conCyan}[`, `]${conReset}`)}`
);
if (userId !== selfId) {
// 群聊信息转服务器
const { Gold, Green, Gray, LightPurple, Bold, Clear } = Format;
mc.broadcast(
`${LightPurple}${Bold}[群聊]${Clear} ` +
`${Green}${nick}${Gold}(${userId})${Gray}: ` +
`${Clear}${messageObjectToString(message)}`
);
// 执行指令
if (txtMsg.startsWith(config.get('cmd_prefix'))) {
const cmd = txtMsg.slice(config.get('cmd_prefix').length);
if (
config
.get('superusers')
.map((i) => i.toString())
.includes(userId.toString()) ||
isAllowedCmd(cmd)
) {
const { success, output } = mc.runcmdEx(cmd);
const stateTxt = success ? '成功' : '失败';
fastReply(`执行${stateTxt}\n${output}`);
logger.info(
`执行指令 ${conGreen}${cmd} ${conYellow}${stateTxt}\n${conCyan}${output}`
);
} else {
fastReply('权限不足');
}
} else if (txtMsg === config.get('cmd_status')) {
sendGroupMsg(groupId, getServerStat());
}
}
}
/**
* 群聊戳一戳处理
* @param {object} ev
*/
function processGroupPoke(ev) {
// 戳一戳获取服务器状态
const {
self_id: selfId,
group_id: groupId,
target_id: targetId,
user_id: userId,
} = ev;
if (targetId === selfId) {
logger.info(
`${conMagenta}Group ${groupId}${conReset} -> ` +
`${conYellow}${userId}${conCyan}戳了戳${conReset}我`
);
sendGroupMsg(groupId, getServerStat());
}
}
/**
* GoCQ事件处理
*/
function processEvent(ev) {
const {
echo,
post_type: postType,
message_type: messageType,
notice_type: noticeType,
meta_event_type: metaEventType,
sub_type: subType,
self_id: selfId,
group_id: groupId,
} = ev;
// API调用返回处理
if (echo !== undefined) {
reqCache.set(echo, ev);
return;
}
// 其他事件处理
if (
postType === 'meta_event' &&
metaEventType === 'lifecycle' &&
subType === 'connect'
) {
// 连接成功
logger.info(`Bot ${conCyan}${selfId} ${conGreen}已连接`);
}
const enableGroups = config.get('enable_groups').map((i) => i.toString());
if (groupId !== undefined && enableGroups.includes(groupId.toString())) {
if (
(postType === 'message' || postType === 'message_sent') &&
messageType === 'group' &&
subType === 'normal'
) {
// 群聊普通(回声)消息
processGroupMsg(ev);
} else if (
postType === 'notice' &&
noticeType === 'notify' &&
subType === 'poke'
) {
// 群聊戳一戳
processGroupPoke(ev);
}
}
}
/**
* 连接GoCQ
* @returns {boolean} 是否成功
*/
function connectGoCQ() {
ws.close();
const success = ws.connect(config.get('ws_url'));
if (success) {
logger.info(`${conGreen}成功与GoCQ建立连接`);
} else {
logger.error('与GoCQ建立连接失败!');
}
return success;
}
/**
* 重连GoCQ (async)
*/
function reconnectGoCQ() {
const reconnect = () => {
logger.info(`${conGreen}正在尝试与GoCQ建立连接……`);
const ok = connectGoCQ();
if (!ok) {
logger.info(`${conYellow}3s后尝试再次重连……`);
setTimeout(reconnect, 3000);
} else {
// eslint-disable-next-line no-use-before-define
// startDetectConnection();
}
return ok;
};
reconnect();
}
/**
* 停止检测ws连接状态进程
*/
/*
function stopDetectConnection() {
clearInterval(detectTaskId);
logger.info(`${conYellow}连接状态监控进程已停止`);
}
*/
/**
* 启动检测ws连接状态进程
*/
/*
function startDetectConnection() {
// 别问我为什么要调用接口校验连接
// 因为 ws.state===ws.Open 的值即使连接上了也是false!!
// 又是llse的锅……
detectTaskId = setInterval(() => {
getStatus((ret) => {
if (ret === undefined) {
logger.error(
`调用接口失败!可能与GoCQ失去连接!${conReset}正在尝试重连……`
);
stopDetectConnection();
reconnectGoCQ();
}
});
}, 5000);
logger.info(`${conGreen}连接状态监控进程已启动`);
}
*/
/**
* 解析GoCQ下发数据
*/
ws.listen('onTextReceived', (text) => {
logger.debug(`收到数据:${conCyan}${text}`);
let ev;
try {
ev = JSON.parse(text);
} catch (e) {
logger.error(`解析GoCQ下发数据失败\n${e.stack}`);
return;
}
processEvent(ev);
});
/**
* ws发生错误
*/
ws.listen('onError', (msg) => {
logger.error(`与GoCQ的连接出现错误!错误信息:${conCyan}${msg}`);
});
/**
* 丢失与GoCQ连接处理
*/
ws.listen('onLostConnection', (code) => {
// stopDetectConnection();
logger.error(
`与GoCQ失去连接!错误码:${conYellow}${code}${conReset},正在尝试重连……`
);
reconnectGoCQ();
});
/**
* 服务器启动成功时启动ws连接与监控进程
*/
mc.listen('onServerStarted', () => {
reconnectGoCQ();
});
/**
* 游戏内聊天转发到群
*/
mc.listen('onChat', (player, msg) => {
(async () => {
sendToAllEnableGroups(`[服务器] ${player.realName}:${msg}`);
})().catch(throwError);
});
/**
* 尝试进服提示
*/
mc.listen('onPreJoin', (player) => {
if (!player.isSimulatedPlayer()) {
(async () => {
const { realName, xuid } = player;
sendToAllEnableGroups(
`[服务器] ${realName} 正在尝试进入服务器,XUID:${xuid}`
);
})().catch(throwError);
}
});
/**
* 进服提示
*/
mc.listen('onJoin', (player) => {
if (!player.isSimulatedPlayer()) {
(async () => {
sendToAllEnableGroups(`[服务器] 欢迎 ${player.realName} 进入服务器`);
})().catch(throwError);
}
});
/**
* 退服提示
*/
mc.listen('onLeft', (player) => {
if (!player.isSimulatedPlayer()) {
const { realName } = player;
(async () => {
sendToAllEnableGroups(`[服务器] ${realName} 退出了服务器`);
})().catch(throwError);
}
});
mc.regConsoleCmd('cqreconnect', '手动重连GoCQHTTP', () => {
logger.info(`${conYellow}正在尝试重连……`);
return reconnectGoCQ();
});
ll.registerPlugin('GoCQSync', '依赖GoCQHTTP的群服互通', [0, 4, 3], {
Author: 'student_2333',
License: 'Apache-2.0',
});
logger.info(`${conGreen}插件加载成功,欢迎使用~`);