koishi-plugin-node-async-bot-all 2.9.0 → 2.10.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/fun.d.ts +3 -3
- package/lib/index.js +94 -59
- package/package.json +1 -1
package/lib/fun.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { Context
|
|
2
|
-
import Analytics from "@koishijs/plugin-analytics";
|
|
1
|
+
import { Context } from "koishi";
|
|
3
2
|
export declare function getSystemUsage(): Promise<Object>;
|
|
4
3
|
export declare function getHongKongTime(): string;
|
|
5
4
|
export declare function fetchWithTimeout(url: string, options: {} | undefined, timeout: number | undefined, log: any): Promise<Response>;
|
|
6
5
|
export declare function readInfoFile(ctx: Context): Promise<string>;
|
|
7
6
|
export declare function formatTimestampDiff(start: number, end: number): string;
|
|
8
|
-
export declare function getMsgCount(
|
|
7
|
+
export declare function getMsgCount(ctx: Context): Promise<Object>;
|
|
9
8
|
export declare function hostPing(host: string): Promise<{
|
|
10
9
|
success: boolean;
|
|
11
10
|
data?: any;
|
|
@@ -13,3 +12,4 @@ export declare function hostPing(host: string): Promise<{
|
|
|
13
12
|
alive?: boolean;
|
|
14
13
|
packetLoss?: string;
|
|
15
14
|
}>;
|
|
15
|
+
export declare function random(type: number | undefined, data: any, data2?: any): number;
|
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: {
|
|
37
|
+
module2.exports = { commands: { cxmc: { 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: "执行错误。", unknown: "未知错误。", 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: "执行错误。", failed1: "{time}\n获取失败(1)。", failed2: "{time}\n获取失败({data})。" } }, randomba: { description: "随机BA图。", messages: { wait: "{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}" } } } };
|
|
38
38
|
}
|
|
39
39
|
});
|
|
40
40
|
|
|
@@ -48,16 +48,17 @@ __export(src_exports, {
|
|
|
48
48
|
usage: () => usage
|
|
49
49
|
});
|
|
50
50
|
module.exports = __toCommonJS(src_exports);
|
|
51
|
-
var
|
|
51
|
+
var import_koishi3 = require("koishi");
|
|
52
52
|
|
|
53
53
|
// src/commands.ts
|
|
54
|
-
var
|
|
54
|
+
var import_koishi2 = require("koishi");
|
|
55
55
|
|
|
56
56
|
// src/fun.ts
|
|
57
57
|
var import_os = __toESM(require("os"));
|
|
58
58
|
var import_fs = __toESM(require("fs"));
|
|
59
59
|
var import_ping = __toESM(require("ping"));
|
|
60
60
|
var import_path = __toESM(require("path"));
|
|
61
|
+
var import_koishi = require("koishi");
|
|
61
62
|
function getSystemName() {
|
|
62
63
|
return import_os.default.type() + " " + import_os.default.release();
|
|
63
64
|
}
|
|
@@ -66,19 +67,29 @@ function getMemoryUsage() {
|
|
|
66
67
|
const totalMemory = import_os.default.totalmem();
|
|
67
68
|
const freeMemory = import_os.default.freemem();
|
|
68
69
|
const usedMemory = totalMemory - freeMemory;
|
|
69
|
-
return Math.round(usedMemory / totalMemory * 100
|
|
70
|
+
return Math.round(usedMemory / totalMemory * 1e4) / 100;
|
|
70
71
|
}
|
|
71
72
|
__name(getMemoryUsage, "getMemoryUsage");
|
|
72
73
|
async function getCpuUsage() {
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
74
|
+
const cpus1 = import_os.default.cpus();
|
|
75
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
76
|
+
const cpus2 = import_os.default.cpus();
|
|
77
|
+
let totalIdle = 0;
|
|
78
|
+
let totalTick = 0;
|
|
79
|
+
for (let i = 0; i < cpus1.length; i++) {
|
|
80
|
+
const cpu1 = cpus1[i];
|
|
81
|
+
const cpu2 = cpus2[i];
|
|
82
|
+
const idle1 = cpu1.times.idle;
|
|
83
|
+
const total1 = Object.values(cpu1.times).reduce((acc, time) => acc + time, 0);
|
|
84
|
+
const idle2 = cpu2.times.idle;
|
|
85
|
+
const total2 = Object.values(cpu2.times).reduce((acc, time) => acc + time, 0);
|
|
86
|
+
const idleDiff = idle2 - idle1;
|
|
87
|
+
const totalDiff = total2 - total1;
|
|
88
|
+
totalIdle += idleDiff;
|
|
89
|
+
totalTick += totalDiff;
|
|
90
|
+
}
|
|
91
|
+
const usage2 = 100 - 100 * totalIdle / totalTick;
|
|
92
|
+
return Math.round(usage2 * 100) / 100;
|
|
82
93
|
}
|
|
83
94
|
__name(getCpuUsage, "getCpuUsage");
|
|
84
95
|
async function getSystemUsage() {
|
|
@@ -101,30 +112,38 @@ async function getSystemUsage() {
|
|
|
101
112
|
__name(getSystemUsage, "getSystemUsage");
|
|
102
113
|
function getHongKongTime() {
|
|
103
114
|
const now = /* @__PURE__ */ new Date();
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
const formatter = new Intl.DateTimeFormat("zh-CN", {
|
|
116
|
+
timeZone: "Asia/Hong_Kong",
|
|
117
|
+
year: "numeric",
|
|
118
|
+
month: "2-digit",
|
|
119
|
+
day: "2-digit",
|
|
120
|
+
hour: "2-digit",
|
|
121
|
+
minute: "2-digit",
|
|
122
|
+
second: "2-digit",
|
|
123
|
+
hour12: false
|
|
124
|
+
});
|
|
125
|
+
const parts = formatter.formatToParts(now);
|
|
126
|
+
const dateObj = {};
|
|
127
|
+
parts.forEach((part) => {
|
|
128
|
+
if (part.type !== "literal") {
|
|
129
|
+
dateObj[part.type] = part.value;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return `${dateObj.year}-${dateObj.month}-${dateObj.day} ${dateObj.hour}:${dateObj.minute}:${dateObj.second}`;
|
|
115
133
|
}
|
|
116
134
|
__name(getHongKongTime, "getHongKongTime");
|
|
117
135
|
async function fetchWithTimeout(url, options = {}, timeout = 5e3, log) {
|
|
118
136
|
const controller = new AbortController();
|
|
137
|
+
const { signal } = controller;
|
|
138
|
+
const fetchOptions = {
|
|
139
|
+
...options,
|
|
140
|
+
signal
|
|
141
|
+
};
|
|
119
142
|
const timeoutId = setTimeout(() => {
|
|
120
143
|
controller.abort();
|
|
121
144
|
}, timeout);
|
|
122
145
|
try {
|
|
123
|
-
const response = await fetch(url,
|
|
124
|
-
...options,
|
|
125
|
-
signal: controller.signal
|
|
126
|
-
// 绑定终止信号
|
|
127
|
-
});
|
|
146
|
+
const response = await fetch(url, fetchOptions);
|
|
128
147
|
clearTimeout(timeoutId);
|
|
129
148
|
log.info(`Fetch code: ${response.status}`);
|
|
130
149
|
return response;
|
|
@@ -133,7 +152,7 @@ async function fetchWithTimeout(url, options = {}, timeout = 5e3, log) {
|
|
|
133
152
|
log.error(error);
|
|
134
153
|
log.error(`${error.name}: ${error.message}`);
|
|
135
154
|
if (error.name === "AbortError") {
|
|
136
|
-
throw new Error(
|
|
155
|
+
throw new Error(`请求超时。(${timeout}ms)`);
|
|
137
156
|
} else {
|
|
138
157
|
throw error;
|
|
139
158
|
}
|
|
@@ -163,7 +182,9 @@ function formatTimestampDiff(start, end) {
|
|
|
163
182
|
return `${hours} 时 ${minutes} 分 ${seconds} 秒`;
|
|
164
183
|
}
|
|
165
184
|
__name(formatTimestampDiff, "formatTimestampDiff");
|
|
166
|
-
function getMsgCount(
|
|
185
|
+
async function getMsgCount(ctx) {
|
|
186
|
+
const array = await ctx.database.get("analytics.message", { date: import_koishi.Time.getDateNumber() - 1 }, ["type", "count"]);
|
|
187
|
+
ctx.logger.info(import_koishi.Time.getDateNumber() - 1);
|
|
167
188
|
let receive = 0;
|
|
168
189
|
let send = 0;
|
|
169
190
|
array.forEach((item) => {
|
|
@@ -195,6 +216,20 @@ async function hostPing(host) {
|
|
|
195
216
|
}
|
|
196
217
|
}
|
|
197
218
|
__name(hostPing, "hostPing");
|
|
219
|
+
function random(type = 0, data, data2) {
|
|
220
|
+
const random2 = new import_koishi.Random(() => Math.random());
|
|
221
|
+
switch (type) {
|
|
222
|
+
case 0:
|
|
223
|
+
return random2.int(data, data2);
|
|
224
|
+
case 1:
|
|
225
|
+
return random2.real(data, data2);
|
|
226
|
+
case 2:
|
|
227
|
+
return random2.pick(data);
|
|
228
|
+
default:
|
|
229
|
+
return 0;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
__name(random, "random");
|
|
198
233
|
|
|
199
234
|
// src/commands.ts
|
|
200
235
|
async function getServer(ctx, session) {
|
|
@@ -284,10 +319,10 @@ async function getServer(ctx, session) {
|
|
|
284
319
|
list = list + "\n" + session.text(".listFailed", temp);
|
|
285
320
|
}
|
|
286
321
|
} catch (err) {
|
|
287
|
-
log.error(`Request error:
|
|
322
|
+
log.error(`Request error: ${err.message}`);
|
|
288
323
|
const temp = {
|
|
289
324
|
"count": count,
|
|
290
|
-
"data": session.text(".error")
|
|
325
|
+
"data": err.name === "AbortError" ? session.text(".error") : err.message
|
|
291
326
|
};
|
|
292
327
|
log.info(`Server ${count}:`);
|
|
293
328
|
log.info(temp);
|
|
@@ -324,7 +359,7 @@ async function getStatus(ctx, session) {
|
|
|
324
359
|
"success": 1
|
|
325
360
|
};
|
|
326
361
|
} else {
|
|
327
|
-
const msgCount = getMsgCount(
|
|
362
|
+
const msgCount = await getMsgCount(ctx);
|
|
328
363
|
msg = {
|
|
329
364
|
"time": time,
|
|
330
365
|
"name": vMsg["name"],
|
|
@@ -425,6 +460,7 @@ async function getRW(ctx, session) {
|
|
|
425
460
|
log.error(`Request error: ${err.message}`);
|
|
426
461
|
msg = {
|
|
427
462
|
"time": time,
|
|
463
|
+
"data": err.name === "AbortError" ? session.text(".error") : err.message,
|
|
428
464
|
"success": 2
|
|
429
465
|
};
|
|
430
466
|
log.info("Sent:");
|
|
@@ -436,14 +472,13 @@ __name(getRW, "getRW");
|
|
|
436
472
|
async function getBA(ctx, session) {
|
|
437
473
|
const log = ctx.logger("ba");
|
|
438
474
|
log.info(`Got: {"form":"${session.event.guild?.id}","user":"${session.event.user?.id}","timestamp":${session.event.timestamp},"messageId":"${session.event.message?.id}"}`);
|
|
439
|
-
const random = new import_koishi.Random(() => Math.random());
|
|
440
475
|
const time = getHongKongTime();
|
|
441
476
|
const vid = await session.send(session.text(".wait", { "time": time }));
|
|
442
|
-
const ms = random
|
|
443
|
-
const link = random
|
|
477
|
+
const ms = random(0, 0, 1500);
|
|
478
|
+
const link = random(2, ctx.config.baAPI) + `?cacheBuster=${random(1, 1, 2147483647)}`;
|
|
444
479
|
log.info(`Link: ${link}`);
|
|
445
|
-
await (0,
|
|
446
|
-
await session.send(
|
|
480
|
+
await (0, import_koishi2.sleep)(ms);
|
|
481
|
+
await session.send(import_koishi2.h.image(link));
|
|
447
482
|
await session.bot.deleteMessage(session.event.guild?.id, vid[0]);
|
|
448
483
|
return 0;
|
|
449
484
|
}
|
|
@@ -487,33 +522,33 @@ async function serverTest(ctx, session) {
|
|
|
487
522
|
__name(serverTest, "serverTest");
|
|
488
523
|
|
|
489
524
|
// package.json
|
|
490
|
-
var version = "2.
|
|
525
|
+
var version = "2.10.0";
|
|
491
526
|
|
|
492
527
|
// src/index.ts
|
|
493
528
|
var inject = ["database"];
|
|
494
529
|
var name = "node-async-bot-all";
|
|
495
530
|
var usage = "这是一个私有插件。";
|
|
496
|
-
var Config =
|
|
497
|
-
|
|
498
|
-
timeout:
|
|
531
|
+
var Config = import_koishi3.Schema.intersect([
|
|
532
|
+
import_koishi3.Schema.object({
|
|
533
|
+
timeout: import_koishi3.Schema.number().default(8e3).description("超时时间(毫秒)")
|
|
499
534
|
}).description("基础"),
|
|
500
|
-
|
|
501
|
-
cxV2:
|
|
502
|
-
|
|
503
|
-
id:
|
|
504
|
-
api:
|
|
505
|
-
note:
|
|
535
|
+
import_koishi3.Schema.object({
|
|
536
|
+
cxV2: import_koishi3.Schema.array(
|
|
537
|
+
import_koishi3.Schema.object({
|
|
538
|
+
id: import_koishi3.Schema.string().required().description("查询 群"),
|
|
539
|
+
api: import_koishi3.Schema.array(String).description("查询 API"),
|
|
540
|
+
note: import_koishi3.Schema.array(String).description("查询 备注")
|
|
506
541
|
})
|
|
507
542
|
).default([]).description("查询的 API 和 群")
|
|
508
543
|
}).description("查询"),
|
|
509
|
-
|
|
510
|
-
rwAPI:
|
|
544
|
+
import_koishi3.Schema.object({
|
|
545
|
+
rwAPI: import_koishi3.Schema.string().default("https://api.tasaed.top/rw/").description("随机文本 API")
|
|
511
546
|
}).description("随机文本"),
|
|
512
|
-
|
|
513
|
-
baAPI:
|
|
547
|
+
import_koishi3.Schema.object({
|
|
548
|
+
baAPI: import_koishi3.Schema.array(String).default(["https://rba.kanostar.top/portrait"]).description("随机BA图 API")
|
|
514
549
|
}).description("随机BA图"),
|
|
515
|
-
|
|
516
|
-
serverPing:
|
|
550
|
+
import_koishi3.Schema.object({
|
|
551
|
+
serverPing: import_koishi3.Schema.dict(String).role("table").description("键:群号;值:Host")
|
|
517
552
|
}).description("服之测测(Ping)")
|
|
518
553
|
]).description("基础设置");
|
|
519
554
|
function apply(ctx) {
|
|
@@ -530,7 +565,7 @@ function apply(ctx) {
|
|
|
530
565
|
{ id: "version", data: version }
|
|
531
566
|
]);
|
|
532
567
|
});
|
|
533
|
-
ctx.command("
|
|
568
|
+
ctx.command("cxMc").action(async ({ session }) => {
|
|
534
569
|
const cx = await getServer(ctx, session);
|
|
535
570
|
if (cx["success"] == 0) {
|
|
536
571
|
return session?.text(".msg", cx);
|
|
@@ -549,8 +584,8 @@ function apply(ctx) {
|
|
|
549
584
|
}
|
|
550
585
|
});
|
|
551
586
|
ctx.command("random [最小数:number] [最大数:number]").alias("随机数").action(async ({ session }, min, max) => {
|
|
552
|
-
const
|
|
553
|
-
return session?.text(".msg",
|
|
587
|
+
const random2 = await getRandom(ctx, session, min, max);
|
|
588
|
+
return session?.text(".msg", random2);
|
|
554
589
|
});
|
|
555
590
|
ctx.command("info").action(async ({ session }) => {
|
|
556
591
|
const info = await getInfo(ctx, session);
|
|
@@ -570,7 +605,7 @@ function apply(ctx) {
|
|
|
570
605
|
return session?.text(".failed2", rw);
|
|
571
606
|
}
|
|
572
607
|
});
|
|
573
|
-
ctx.command("randomBA").alias("随机
|
|
608
|
+
ctx.command("randomBA").alias("随机ba图").action(async ({ session }) => {
|
|
574
609
|
await getBA(ctx, session);
|
|
575
610
|
});
|
|
576
611
|
ctx.command("serverTest").alias("服之测测").action(async ({ session }) => {
|