koishi-plugin-node-async-bot-all 2.18.1 → 2.20.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/lib/commands.d.ts +1 -0
- package/lib/fun.d.ts +1 -6
- package/lib/index.js +144 -145
- package/package.json +1 -1
- package/res/userCard.html +1 -1
- package/res/userMsg.html +81 -0
package/lib/commands.d.ts
CHANGED
|
@@ -33,3 +33,4 @@ export declare function serverTest(ctx: Context, session: Session): Promise<Obje
|
|
|
33
33
|
export declare function getMeme(ctx: Context, session: Session, count: number): Promise<Number>;
|
|
34
34
|
export declare function getCat(ctx: Context, session: Session): Promise<Number>;
|
|
35
35
|
export declare function getQQInfo(ctx: Context, session: Session, qq: string): Promise<number>;
|
|
36
|
+
export declare function getMsg(ctx: Context, session: Session): Promise<number>;
|
package/lib/fun.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { Context } from "koishi";
|
|
|
3
3
|
import { APIUserInfo } from "./commands";
|
|
4
4
|
export declare function getSystemUsage(): Promise<Object>;
|
|
5
5
|
export declare function getHongKongTime(): string;
|
|
6
|
-
export declare function fetchWithTimeout(url: string, options: {} | undefined, timeout: number | undefined, log: any): Promise<Response>;
|
|
7
6
|
export declare function readInfoFile(ctx: Context): Promise<string>;
|
|
8
7
|
export declare function formatTimestampDiff(start: number, end: number): string;
|
|
9
8
|
export declare function getMsgCount(ctx: Context): Promise<Object>;
|
|
@@ -15,10 +14,6 @@ export declare function hostPing(host: string): Promise<{
|
|
|
15
14
|
packetLoss?: string;
|
|
16
15
|
}>;
|
|
17
16
|
export declare function random(type: number | undefined, data: any, data2?: any): number;
|
|
18
|
-
export declare function getHttp(log: any, url: string, timeout: number): Promise<{
|
|
19
|
-
success: boolean;
|
|
20
|
-
data: object;
|
|
21
|
-
error?: boolean;
|
|
22
|
-
}>;
|
|
23
17
|
export declare function request<T = any>(url: string, options?: RequestInit, timeout?: number, log?: any): Promise<HttpResponse<T>>;
|
|
24
18
|
export declare function readUserCardFile(userInfo: APIUserInfo): Promise<string>;
|
|
19
|
+
export declare function readUserMsgFile(userName: string, userAvatar: string, msg: string): Promise<string>;
|
package/lib/index.js
CHANGED
|
@@ -34,7 +34,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
34
34
|
// src/locales/zh-CN.yml
|
|
35
35
|
var require_zh_CN = __commonJS({
|
|
36
36
|
"src/locales/zh-CN.yml"(exports2, module2) {
|
|
37
|
-
module2.exports = { commands: { cxgame: { description: "查询服务器当前人数。", messages: { msg: "{time}{list}\n进服指南请在群公告中查看。", list: "【MC 服务器 {count}】\n➣ {version}:{players}\n➣ 玩家列表:{list}\n➣ 备注:{note}", listNoPlayer: "【MC 服务器 {count}】\n➣ {version}:{players}\n➣ 备注:{note}", listFailed: "【MC 服务器 {count}】\n➣ 查询失败:{data}\n➣ 请稍后重试。", forbidden: "{time}\n此指令不允许在本群使用。", failed: "{time}\n查询失败:{data}", timeout: "请求超时。", timeout2: "响应超时。", fewData: "服务端返回的数据过少。", close: "服务器已关闭。", error: "执行错误。", host: "没有到主机的路由。" } }, status: { description: "查询机器人状态。", messages: { msg: "{time}\n--- 系统状态 ---\n系统名称:{name}\nCPU使用率:{cpu}\n内存使用率:{memory}\n--- 机器人状态 ---\n昨日收/发消息数量:{msgCount}\n机器人版本:{version}\n运行时间:{online}", failed: "{time}\n状态获取失败。" } }, random: { description: "随机数生成器。", usage: "缺少参数时默认生成 0-10000 的随机数。\n使用示例:", examples: "random 1 128 生成1到128范围的随机数", messages: { msg: "{time}\n生成的随机数:{data}" } }, info: { description: "查询机器人信息。", messages: { msg: "{data}", failed: "{time}\n读取信息失败。" } }, rw: { description: "随机名言名句。", messages: { msg: "{time}\n{data}", error: "执行错误。",
|
|
37
|
+
module2.exports = { commands: { cxgame: { description: "查询服务器当前人数。", messages: { msg: "{time}{list}\n进服指南请在群公告中查看。", list: "【MC 服务器 {count}】\n➣ {version}:{players}\n➣ 玩家列表:{list}\n➣ 备注:{note}", listNoPlayer: "【MC 服务器 {count}】\n➣ {version}:{players}\n➣ 备注:{note}", listFailed: "【MC 服务器 {count}】\n➣ 查询失败:{data}\n➣ 请稍后重试。", forbidden: "{time}\n此指令不允许在本群使用。", failed: "{time}\n查询失败:{data}", timeout: "请求超时。", timeout2: "响应超时。", fewData: "服务端返回的数据过少。", close: "服务器已关闭。", error: "执行错误。", host: "没有到主机的路由。" } }, status: { description: "查询机器人状态。", messages: { msg: "{time}\n--- 系统状态 ---\n系统名称:{name}\nCPU使用率:{cpu}\n内存使用率:{memory}\n--- 机器人状态 ---\n昨日收/发消息数量:{msgCount}\n机器人版本:{version}\n运行时间:{online}", failed: "{time}\n状态获取失败。" } }, random: { description: "随机数生成器。", usage: "缺少参数时默认生成 0-10000 的随机数。\n使用示例:", examples: "random 1 128 生成1到128范围的随机数", messages: { msg: "{time}\n生成的随机数:{data}" } }, info: { description: "查询机器人信息。", messages: { msg: "{data}", failed: "{time}\n读取信息失败。" } }, rw: { description: "随机名言名句。", messages: { msg: "{time}\n{data}", error: "执行错误。", failed: "{time}\n获取失败({data})。" } }, randomba: { description: "随机BA图。", messages: { msg: "{quote}{image}", wait: "{quote}{time}\n请等待图片上传(可能较慢)。" } }, servertest: { description: "Ping服务器。", messages: { msg: "{time}\n== Ping {host} ==\n状态:{alive}\n丢包率:{packetLoss}\n返回IP:{ip}", forbidden: "{time}\n此指令不允许在本群使用。", failed: "{time}\nPing 失败:{data}" } }, meme: { description: "群友的怪话!", messages: { msg: "{quote}{time}\n{image}\n{title}", failed: "{quote}{time}\n获取失败:{data}", forbidden: "{quote}{time}\n此指令不允许在本群使用。", error: "执行错误。" } }, randomcat: { description: "随机猫猫图。", messages: { msg: "{quote}{image}", wait: "{quote}{time}\n请等待图片上传(可能较慢)。", failed: "{quote}{time}\n获取失败:{data}" } }, getqqinfo: { description: "获取 QQ 号的信息。", messages: { msg: "{quote}{image}", failed: "{quote}{time}\n获取失败:{data}", command: "QQ 号不正确。" } }, msg2img: { description: "引用一个消息并使用此指令,即可将消息转图。", messages: { msg: "{quote}{image}", failed: "{quote}{time}\n获取失败:{data}", null: "未引用任何消息。" } } } };
|
|
38
38
|
}
|
|
39
39
|
});
|
|
40
40
|
|
|
@@ -132,33 +132,6 @@ function getHongKongTime() {
|
|
|
132
132
|
return `${dateObj.year}-${dateObj.month}-${dateObj.day} ${dateObj.hour}:${dateObj.minute}:${dateObj.second}`;
|
|
133
133
|
}
|
|
134
134
|
__name(getHongKongTime, "getHongKongTime");
|
|
135
|
-
async function fetchWithTimeout(url, options = {}, timeout = 5e3, log) {
|
|
136
|
-
const controller = new AbortController();
|
|
137
|
-
const { signal } = controller;
|
|
138
|
-
const fetchOptions = {
|
|
139
|
-
...options,
|
|
140
|
-
signal
|
|
141
|
-
};
|
|
142
|
-
const timeoutId = setTimeout(() => {
|
|
143
|
-
controller.abort();
|
|
144
|
-
}, timeout);
|
|
145
|
-
try {
|
|
146
|
-
const response = await fetch(url, fetchOptions);
|
|
147
|
-
clearTimeout(timeoutId);
|
|
148
|
-
log.info(`Fetch code: ${response.status}`);
|
|
149
|
-
return response;
|
|
150
|
-
} catch (error) {
|
|
151
|
-
clearTimeout(timeoutId);
|
|
152
|
-
log.error(error);
|
|
153
|
-
log.error(`${error.name}: ${error.message}`);
|
|
154
|
-
if (error.name === "AbortError") {
|
|
155
|
-
throw new Error(`请求超时。(${timeout}ms)`);
|
|
156
|
-
} else {
|
|
157
|
-
throw error;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
__name(fetchWithTimeout, "fetchWithTimeout");
|
|
162
135
|
async function readInfoFile(ctx) {
|
|
163
136
|
let info;
|
|
164
137
|
try {
|
|
@@ -231,36 +204,6 @@ function random(type = 0, data, data2) {
|
|
|
231
204
|
}
|
|
232
205
|
}
|
|
233
206
|
__name(random, "random");
|
|
234
|
-
async function getHttp(log, url, timeout) {
|
|
235
|
-
let data;
|
|
236
|
-
try {
|
|
237
|
-
const response = await fetchWithTimeout(url, {}, timeout, log);
|
|
238
|
-
if (response.ok) {
|
|
239
|
-
data = await response.text();
|
|
240
|
-
log.info("Server data: " + data);
|
|
241
|
-
return {
|
|
242
|
-
"data": JSON.parse(data),
|
|
243
|
-
"success": true
|
|
244
|
-
};
|
|
245
|
-
} else {
|
|
246
|
-
data = await response.text();
|
|
247
|
-
log.error(`Error fetching data: ${data}`);
|
|
248
|
-
return {
|
|
249
|
-
"data": JSON.parse(data),
|
|
250
|
-
"error": false,
|
|
251
|
-
"success": false
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
} catch (err) {
|
|
255
|
-
log.error(`Request error: ${err.message}`);
|
|
256
|
-
return {
|
|
257
|
-
"data": { "name": err.name, "message": err.message },
|
|
258
|
-
"error": true,
|
|
259
|
-
"success": false
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
__name(getHttp, "getHttp");
|
|
264
207
|
async function request(url, options = {}, timeout = 8e3, log) {
|
|
265
208
|
const signal = AbortSignal.timeout(timeout);
|
|
266
209
|
try {
|
|
@@ -326,14 +269,24 @@ async function readUserCardFile(userInfo) {
|
|
|
326
269
|
return card;
|
|
327
270
|
}
|
|
328
271
|
__name(readUserCardFile, "readUserCardFile");
|
|
272
|
+
async function readUserMsgFile(userName, userAvatar, msg) {
|
|
273
|
+
let html;
|
|
274
|
+
try {
|
|
275
|
+
const aPath = import_path.default.resolve(__dirname, "..") + import_path.default.sep + "res" + import_path.default.sep + "userMsg.html";
|
|
276
|
+
html = await import_fs.default.promises.readFile(aPath, "utf8");
|
|
277
|
+
html = html.toString().replace("{userData.avatarUrl}", userAvatar).replace("{userData.username}", userName).replace("{userData.message}", msg);
|
|
278
|
+
} catch (error) {
|
|
279
|
+
html = error.message;
|
|
280
|
+
}
|
|
281
|
+
return html;
|
|
282
|
+
}
|
|
283
|
+
__name(readUserMsgFile, "readUserMsgFile");
|
|
329
284
|
|
|
330
285
|
// src/commands.ts
|
|
331
286
|
async function getServer(ctx, session) {
|
|
332
287
|
const log = ctx.logger("cx");
|
|
333
288
|
log.debug(`Got: {"form":"${session.event.guild?.id}","user":"${session.event.user?.id}","timestamp":${session.event.timestamp},"messageId":"${session.event.message?.id}"}`);
|
|
334
289
|
let msg;
|
|
335
|
-
let data;
|
|
336
|
-
let error;
|
|
337
290
|
const time = getHongKongTime();
|
|
338
291
|
const index = ctx.config.cxV2.findIndex((item) => item.id === session.event.guild?.id);
|
|
339
292
|
if (index !== -1) {
|
|
@@ -344,8 +297,8 @@ async function getServer(ctx, session) {
|
|
|
344
297
|
"data": "未指定查询 API",
|
|
345
298
|
"success": 2
|
|
346
299
|
};
|
|
347
|
-
log.
|
|
348
|
-
log.
|
|
300
|
+
log.warn("Sent:");
|
|
301
|
+
log.warn(msg);
|
|
349
302
|
return msg;
|
|
350
303
|
}
|
|
351
304
|
let count = 0;
|
|
@@ -353,14 +306,13 @@ async function getServer(ctx, session) {
|
|
|
353
306
|
for (const item of api) {
|
|
354
307
|
const note = ctx.config.cxV2[index]["note"][count];
|
|
355
308
|
count++;
|
|
356
|
-
const response = await
|
|
309
|
+
const response = await request(item, {}, ctx.config.timeout, log);
|
|
357
310
|
if (response.success) {
|
|
358
|
-
|
|
359
|
-
if (data["list"] == null) {
|
|
311
|
+
if (response.data.list == null) {
|
|
360
312
|
const temp = {
|
|
361
313
|
"count": count,
|
|
362
|
-
"players": data
|
|
363
|
-
"version": data
|
|
314
|
+
"players": response.data.players,
|
|
315
|
+
"version": response.data.version,
|
|
364
316
|
"note": note ?? "无"
|
|
365
317
|
};
|
|
366
318
|
log.info(`Server ${count}:`);
|
|
@@ -369,9 +321,9 @@ async function getServer(ctx, session) {
|
|
|
369
321
|
} else {
|
|
370
322
|
const temp = {
|
|
371
323
|
"count": count,
|
|
372
|
-
"players": data
|
|
373
|
-
"version": data
|
|
374
|
-
"list": data
|
|
324
|
+
"players": response.data.players,
|
|
325
|
+
"version": response.data.version,
|
|
326
|
+
"list": response.data.list.join(", "),
|
|
375
327
|
"note": note ?? "无"
|
|
376
328
|
};
|
|
377
329
|
log.info(`Server ${count}:`);
|
|
@@ -379,37 +331,30 @@ async function getServer(ctx, session) {
|
|
|
379
331
|
list = list + "\n" + session.text(".list", temp);
|
|
380
332
|
}
|
|
381
333
|
} else {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
error = session.text(".close");
|
|
396
|
-
} else if (error.includes("No route to host")) {
|
|
397
|
-
error = session.text(".host");
|
|
398
|
-
} else if (error.includes("Connection timed out")) {
|
|
399
|
-
error = session.text(".timeout");
|
|
400
|
-
} else if (error.includes("Server returned too few data")) {
|
|
401
|
-
error = session.text(".fewData");
|
|
402
|
-
} else if (error.includes("Server read timed out")) {
|
|
403
|
-
error = session.text(".timeout2");
|
|
334
|
+
let err;
|
|
335
|
+
if (response.code) {
|
|
336
|
+
err = response.isJson ? response.error["data"] : response.error;
|
|
337
|
+
if (err.includes("Connection refused")) {
|
|
338
|
+
err = session.text(".close");
|
|
339
|
+
} else if (err.includes("No route to host")) {
|
|
340
|
+
err = session.text(".host");
|
|
341
|
+
} else if (err.includes("Connection timed out")) {
|
|
342
|
+
err = session.text(".timeout");
|
|
343
|
+
} else if (err.includes("Server returned too few data")) {
|
|
344
|
+
err = session.text(".fewData");
|
|
345
|
+
} else if (err.includes("Server read timed out")) {
|
|
346
|
+
err = session.text(".timeout2");
|
|
404
347
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
"data": error
|
|
408
|
-
};
|
|
409
|
-
log.info(`Server ${count}:`);
|
|
410
|
-
log.info(temp);
|
|
411
|
-
list = list + "\n" + session.text(".listFailed", temp);
|
|
348
|
+
} else {
|
|
349
|
+
err = response.error.message;
|
|
412
350
|
}
|
|
351
|
+
const temp = {
|
|
352
|
+
"count": count,
|
|
353
|
+
"data": err
|
|
354
|
+
};
|
|
355
|
+
log.error(`Server ${count}:`);
|
|
356
|
+
log.error(temp);
|
|
357
|
+
list = list + "\n" + session.text(".listFailed", temp);
|
|
413
358
|
}
|
|
414
359
|
}
|
|
415
360
|
msg = {
|
|
@@ -457,8 +402,8 @@ async function getStatus(ctx, session) {
|
|
|
457
402
|
"success": 0
|
|
458
403
|
};
|
|
459
404
|
}
|
|
460
|
-
log.
|
|
461
|
-
log.
|
|
405
|
+
log.debug("Sent:");
|
|
406
|
+
log.debug(msg);
|
|
462
407
|
return msg;
|
|
463
408
|
}
|
|
464
409
|
__name(getStatus, "getStatus");
|
|
@@ -480,8 +425,8 @@ async function getRandom(ctx, session, min, max) {
|
|
|
480
425
|
"data": data,
|
|
481
426
|
"success": 0
|
|
482
427
|
};
|
|
483
|
-
log.
|
|
484
|
-
log.
|
|
428
|
+
log.debug("Sent:");
|
|
429
|
+
log.debug(msg);
|
|
485
430
|
return msg;
|
|
486
431
|
}
|
|
487
432
|
__name(getRandom, "getRandom");
|
|
@@ -506,8 +451,8 @@ async function getInfo(ctx, session) {
|
|
|
506
451
|
"success": 0
|
|
507
452
|
};
|
|
508
453
|
}
|
|
509
|
-
log.
|
|
510
|
-
log.
|
|
454
|
+
log.debug("Sent:");
|
|
455
|
+
log.debug(msg);
|
|
511
456
|
return msg;
|
|
512
457
|
}
|
|
513
458
|
__name(getInfo, "getInfo");
|
|
@@ -515,7 +460,6 @@ async function getRandomWord(ctx, session) {
|
|
|
515
460
|
const log = ctx.logger("rw");
|
|
516
461
|
log.debug(`Got: {"form":"${session.event.guild?.id}","user":"${session.event.user?.id}","timestamp":${session.event.timestamp},"messageId":"${session.event.message?.id}"}`);
|
|
517
462
|
let msg;
|
|
518
|
-
let data;
|
|
519
463
|
const time = getHongKongTime();
|
|
520
464
|
if (ctx.config.rwAPI == void 0) {
|
|
521
465
|
msg = {
|
|
@@ -523,40 +467,33 @@ async function getRandomWord(ctx, session) {
|
|
|
523
467
|
"data": "未指定 API",
|
|
524
468
|
"success": 2
|
|
525
469
|
};
|
|
526
|
-
log.
|
|
527
|
-
log.
|
|
470
|
+
log.warn("Sent:");
|
|
471
|
+
log.warn(msg);
|
|
528
472
|
return msg;
|
|
529
473
|
}
|
|
530
|
-
const response = await
|
|
474
|
+
const response = await request(ctx.config.rwAPI + "?format=json", {}, ctx.config.timeout, log);
|
|
531
475
|
if (response.success) {
|
|
532
|
-
data = response.data;
|
|
533
476
|
msg = {
|
|
534
477
|
"time": time,
|
|
535
|
-
"data": data
|
|
478
|
+
"data": response.data,
|
|
536
479
|
"success": 0
|
|
537
480
|
};
|
|
538
|
-
log.
|
|
539
|
-
log.
|
|
481
|
+
log.debug("Sent:");
|
|
482
|
+
log.debug(msg);
|
|
540
483
|
} else {
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
"time": time,
|
|
545
|
-
"data": data["name"] === "AbortError" ? session.text(".error") : data["message"],
|
|
546
|
-
"success": 2
|
|
547
|
-
};
|
|
548
|
-
log.info("Sent:");
|
|
549
|
-
log.info(msg);
|
|
484
|
+
let err;
|
|
485
|
+
if (response.code) {
|
|
486
|
+
err = response.isJson ? response.error["data"] : response.error;
|
|
550
487
|
} else {
|
|
551
|
-
|
|
552
|
-
msg = {
|
|
553
|
-
"time": time,
|
|
554
|
-
"data": data["data"],
|
|
555
|
-
"success": 1
|
|
556
|
-
};
|
|
557
|
-
log.info("Sent:");
|
|
558
|
-
log.info(msg);
|
|
488
|
+
err = response.error.message;
|
|
559
489
|
}
|
|
490
|
+
msg = {
|
|
491
|
+
"time": time,
|
|
492
|
+
"data": err,
|
|
493
|
+
"success": 1
|
|
494
|
+
};
|
|
495
|
+
log.warn("Sent:");
|
|
496
|
+
log.warn(msg);
|
|
560
497
|
}
|
|
561
498
|
return msg;
|
|
562
499
|
}
|
|
@@ -678,7 +615,8 @@ async function getCat(ctx, session) {
|
|
|
678
615
|
const response = await request(ctx.config.catAPI, {}, ctx.config.timeout, log);
|
|
679
616
|
if (response.success) {
|
|
680
617
|
log.debug(response.data);
|
|
681
|
-
await session.send(session.text(".msg", { "quote": import_koishi2.h.quote(session.messageId), "image": import_koishi2.h.image(response.data[0].url) }));
|
|
618
|
+
const status = await session.send(session.text(".msg", { "quote": import_koishi2.h.quote(session.messageId), "image": import_koishi2.h.image(response.data[0].url) }));
|
|
619
|
+
if (!status) await session.send(session.text(".msg", { "quote": import_koishi2.h.quote(session.messageId), "image": import_koishi2.h.image(response.data[0].url) }));
|
|
682
620
|
log.debug("Sent:");
|
|
683
621
|
log.debug(response.data[0].url);
|
|
684
622
|
} else {
|
|
@@ -706,18 +644,29 @@ async function getQQInfo(ctx, session, qq) {
|
|
|
706
644
|
}
|
|
707
645
|
const response = await request(ctx.config.qqAPI + `?qq=${qq}`, {}, ctx.config.timeout, log);
|
|
708
646
|
if (response.success) {
|
|
709
|
-
log.info(response.data);
|
|
710
647
|
const fullHtml = await readUserCardFile(response.data);
|
|
711
648
|
const page = await ctx.puppeteer.page();
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
649
|
+
try {
|
|
650
|
+
await page.setViewport({
|
|
651
|
+
width: 450,
|
|
652
|
+
height: 650,
|
|
653
|
+
deviceScaleFactor: 2
|
|
654
|
+
});
|
|
655
|
+
await page.setContent(fullHtml, { waitUntil: "networkidle0" });
|
|
656
|
+
const { width, height } = await page.evaluate(() => ({
|
|
657
|
+
width: document.body.scrollWidth,
|
|
658
|
+
height: document.body.scrollHeight
|
|
659
|
+
}));
|
|
660
|
+
await page.setViewport({ width, height, deviceScaleFactor: 2 });
|
|
661
|
+
const image = await page.screenshot({ type: "png", omitBackground: true });
|
|
662
|
+
await session.send(session.text(".msg", { "quote": import_koishi2.h.quote(session.messageId), "image": import_koishi2.h.image(image, "image/png") }));
|
|
663
|
+
} catch (err) {
|
|
664
|
+
await session.send(session.text(".failed", { "quote": import_koishi2.h.quote(session.messageId), "time": time, "data": "图片渲染失败" }));
|
|
665
|
+
log.error("图片渲染失败:", err);
|
|
666
|
+
return 1;
|
|
667
|
+
} finally {
|
|
668
|
+
if (page && !page.isClosed()) await page.close();
|
|
669
|
+
}
|
|
721
670
|
log.debug("Sent: Image");
|
|
722
671
|
} else {
|
|
723
672
|
if (response.code) {
|
|
@@ -733,9 +682,58 @@ async function getQQInfo(ctx, session, qq) {
|
|
|
733
682
|
return 0;
|
|
734
683
|
}
|
|
735
684
|
__name(getQQInfo, "getQQInfo");
|
|
685
|
+
async function getMsg(ctx, session) {
|
|
686
|
+
const log = ctx.logger("getQQInfo");
|
|
687
|
+
log.debug(`Got: {"form":"${session.event.guild?.id}","user":"${session.event.user?.id}","timestamp":${session.event.timestamp},"messageId":"${session.event.message?.id}"}`);
|
|
688
|
+
const time = getHongKongTime();
|
|
689
|
+
if (!session.quote || !session.quote.user) {
|
|
690
|
+
await session.send(session.text(".null"));
|
|
691
|
+
log.warn("未引用任何信息");
|
|
692
|
+
return 1;
|
|
693
|
+
}
|
|
694
|
+
const user = await session.bot.getUser(session.quote.user.id, session.channelId);
|
|
695
|
+
const msg = session.quote.content;
|
|
696
|
+
if (!user.name || !user.avatar) {
|
|
697
|
+
await session.send(session.text(".failed", { "quote": import_koishi2.h.quote(session.messageId), "time": time, "data": "获取用户信息失败。" }));
|
|
698
|
+
log.error("获取用户信息失败");
|
|
699
|
+
return 1;
|
|
700
|
+
}
|
|
701
|
+
const page = await ctx.puppeteer.page();
|
|
702
|
+
const html = await readUserMsgFile(user.name, user.avatar, msg);
|
|
703
|
+
try {
|
|
704
|
+
await page.setViewport({
|
|
705
|
+
width: 400,
|
|
706
|
+
height: 800,
|
|
707
|
+
deviceScaleFactor: 2
|
|
708
|
+
});
|
|
709
|
+
await page.setContent(html, { waitUntil: "networkidle0" });
|
|
710
|
+
const element = await page.$("#target-element");
|
|
711
|
+
if (element) {
|
|
712
|
+
const image = await element.screenshot({
|
|
713
|
+
type: "png",
|
|
714
|
+
omitBackground: true
|
|
715
|
+
// 使得 CSS 中未定义的背景部分透明
|
|
716
|
+
});
|
|
717
|
+
await session.send(session.text(".msg", { "quote": import_koishi2.h.quote(session.messageId), "image": import_koishi2.h.image(image, "image/png") }));
|
|
718
|
+
log.debug("Sent: Image");
|
|
719
|
+
} else {
|
|
720
|
+
await session.send(session.text(".failed", { "quote": import_koishi2.h.quote(session.messageId), "time": time, "data": "未找到目标元素。" }));
|
|
721
|
+
log.error("未找到目标元素");
|
|
722
|
+
return 1;
|
|
723
|
+
}
|
|
724
|
+
} catch (err) {
|
|
725
|
+
await session.send(session.text(".failed", { "quote": import_koishi2.h.quote(session.messageId), "time": time, "data": "图片渲染失败" }));
|
|
726
|
+
log.error("图片渲染失败:", err);
|
|
727
|
+
return 1;
|
|
728
|
+
} finally {
|
|
729
|
+
if (page && !page.isClosed()) await page.close();
|
|
730
|
+
}
|
|
731
|
+
return 0;
|
|
732
|
+
}
|
|
733
|
+
__name(getMsg, "getMsg");
|
|
736
734
|
|
|
737
735
|
// package.json
|
|
738
|
-
var version = "2.
|
|
736
|
+
var version = "2.20.0";
|
|
739
737
|
|
|
740
738
|
// src/index.ts
|
|
741
739
|
var inject = ["database", "installer", "puppeteer"];
|
|
@@ -824,10 +822,8 @@ function apply(ctx) {
|
|
|
824
822
|
const rw = await getRandomWord(ctx, session);
|
|
825
823
|
if (rw["success"] == 0) {
|
|
826
824
|
return session?.text(".msg", rw);
|
|
827
|
-
} else if (rw["success"] == 1) {
|
|
828
|
-
return session?.text(".failed1", rw);
|
|
829
825
|
} else {
|
|
830
|
-
return session?.text(".
|
|
826
|
+
return session?.text(".failed", rw);
|
|
831
827
|
}
|
|
832
828
|
});
|
|
833
829
|
ctx.command("randomBA").alias("随机ba图").action(async ({ session }) => {
|
|
@@ -853,6 +849,9 @@ function apply(ctx) {
|
|
|
853
849
|
if (qq == void 0 || isNaN(Number(qq))) return session?.text(".command");
|
|
854
850
|
await getQQInfo(ctx, session, qq);
|
|
855
851
|
});
|
|
852
|
+
ctx.command("msg2img").alias("消息转图").alias("m").action(async ({ session }) => {
|
|
853
|
+
await getMsg(ctx, session);
|
|
854
|
+
});
|
|
856
855
|
}
|
|
857
856
|
__name(apply, "apply");
|
|
858
857
|
// Annotate the CommonJS export names for ESM import in node:
|
package/package.json
CHANGED
package/res/userCard.html
CHANGED
package/res/userMsg.html
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>UserMsg</title>
|
|
7
|
+
<style>
|
|
8
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
9
|
+
body {
|
|
10
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
11
|
+
background-color: transparent; /* 背景透明 */
|
|
12
|
+
padding: 20px;
|
|
13
|
+
display: flex;
|
|
14
|
+
justify-content: flex-start;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/* 消息容器 */
|
|
18
|
+
.msg-container {
|
|
19
|
+
display: flex;
|
|
20
|
+
flex-direction: row;
|
|
21
|
+
align-items: flex-start;
|
|
22
|
+
max-width: 100%;
|
|
23
|
+
background: #f5f5f5; /* 卡片背景色,如果是生成透明图可以去掉 */
|
|
24
|
+
padding: 20px;
|
|
25
|
+
border-radius: 12px;
|
|
26
|
+
/* 如果你想让图片就是气泡本身,可以去掉上面的 background 和 padding,保留下面的布局 */
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* 头像 */
|
|
30
|
+
.avatar {
|
|
31
|
+
width: 50px;
|
|
32
|
+
height: 50px;
|
|
33
|
+
border-radius: 50%;
|
|
34
|
+
background-color: #ddd;
|
|
35
|
+
object-fit: cover;
|
|
36
|
+
flex-shrink: 0; /* 防止头像被压缩 */
|
|
37
|
+
margin-right: 12px;
|
|
38
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/* 右侧内容区域 */
|
|
42
|
+
.content-wrapper {
|
|
43
|
+
display: flex;
|
|
44
|
+
flex-direction: column;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/* 用户名 */
|
|
48
|
+
.username {
|
|
49
|
+
font-size: 14px;
|
|
50
|
+
color: #666;
|
|
51
|
+
margin-bottom: 4px;
|
|
52
|
+
font-weight: 500;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/* 消息气泡 */
|
|
56
|
+
.bubble {
|
|
57
|
+
background-color: #fff;
|
|
58
|
+
color: #333;
|
|
59
|
+
padding: 10px 14px;
|
|
60
|
+
border-radius: 0 12px 12px 12px; /* 左上角直角,其余圆角 */
|
|
61
|
+
font-size: 16px;
|
|
62
|
+
line-height: 1.5;
|
|
63
|
+
box-shadow: 0 1px 2px rgba(0,0,0,0.05);
|
|
64
|
+
position: relative;
|
|
65
|
+
word-wrap: break-word; /* 强制换行 */
|
|
66
|
+
}
|
|
67
|
+
</style>
|
|
68
|
+
</head>
|
|
69
|
+
<body>
|
|
70
|
+
<!-- 目标截图元素 -->
|
|
71
|
+
<div id="target-element" class="msg-container">
|
|
72
|
+
<img class="avatar" src="{userData.avatarUrl}" />
|
|
73
|
+
<div class="content-wrapper">
|
|
74
|
+
<div class="username">{userData.username}</div>
|
|
75
|
+
<div class="bubble">
|
|
76
|
+
{userData.message}
|
|
77
|
+
</div>
|
|
78
|
+
</div>
|
|
79
|
+
</div>
|
|
80
|
+
</body>
|
|
81
|
+
</html>
|