koishi-plugin-node-async-bot-all 2.4.0 → 2.5.1
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 +1 -1
- package/lib/index.d.ts +5 -2
- package/lib/index.js +116 -101
- package/package.json +1 -1
package/lib/fun.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Context, FlatPick } from "koishi";
|
|
|
2
2
|
import Analytics from "@koishijs/plugin-analytics";
|
|
3
3
|
export declare function getSystemUsage(): Promise<Object>;
|
|
4
4
|
export declare function getHongKongTime(): string;
|
|
5
|
-
export declare function fetchWithTimeout(url: string, options: {}, timeout: number, log: any): Promise<Response>;
|
|
5
|
+
export declare function fetchWithTimeout(url: string, options: {} | undefined, timeout: number | undefined, log: any): Promise<Response>;
|
|
6
6
|
export declare function readInfoFile(ctx: Context): Promise<string>;
|
|
7
7
|
export declare function formatTimestampDiff(start: number, end: number): string;
|
|
8
8
|
export declare function getMsgCount(array: FlatPick<Analytics.Message, "type" | "count">[]): Object;
|
package/lib/index.d.ts
CHANGED
|
@@ -11,9 +11,12 @@ interface botDataTables {
|
|
|
11
11
|
}
|
|
12
12
|
export declare const name = "node-async-bot-all";
|
|
13
13
|
export declare const usage = "\u8FD9\u662F\u4E00\u4E2A\u79C1\u6709\u63D2\u4EF6\u3002";
|
|
14
|
+
export interface ConfigCxV2 {
|
|
15
|
+
id: string;
|
|
16
|
+
api: Array<string>;
|
|
17
|
+
}
|
|
14
18
|
export interface Config {
|
|
15
|
-
|
|
16
|
-
cxV2API: Array<string>;
|
|
19
|
+
cxV2: Array<ConfigCxV2>;
|
|
17
20
|
rwAPI: string;
|
|
18
21
|
timeout: number;
|
|
19
22
|
}
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -33,7 +34,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
33
34
|
// src/locales/zh-CN.yml
|
|
34
35
|
var require_zh_CN = __commonJS({
|
|
35
36
|
"src/locales/zh-CN.yml"(exports2, module2) {
|
|
36
|
-
module2.exports = { commands: { cx: { description: "查询服务器当前人数。", messages: { msg: "{time}\n【MC
|
|
37
|
+
module2.exports = { commands: { cx: { description: "查询服务器当前人数。", messages: { msg: "{time}{list}\n进服指南请在群公告中查看。", list: "【MC 服务器 {count}】\n➣ {version}:{players}\n➣ 玩家列表:{list}\n➣ 协议版本:{protocol}", listNoPlayer: "【MC 服务器 {count}】\n➣ {version}:{players}\n➣ 协议版本:{protocol}", 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}", failed1: "{time}\n获取失败(1)。", failed2: "{time}\n获取失败(2)。" } } } };
|
|
37
38
|
}
|
|
38
39
|
});
|
|
39
40
|
|
|
@@ -128,6 +129,7 @@ async function fetchWithTimeout(url, options = {}, timeout = 5e3, log) {
|
|
|
128
129
|
return response;
|
|
129
130
|
} catch (error) {
|
|
130
131
|
clearTimeout(timeoutId);
|
|
132
|
+
log.error(error);
|
|
131
133
|
log.error(`${error.name}: ${error.message}`);
|
|
132
134
|
if (error.name === "AbortError") {
|
|
133
135
|
throw new Error("请求超时");
|
|
@@ -146,8 +148,8 @@ async function readInfoFile(ctx) {
|
|
|
146
148
|
"&version;",
|
|
147
149
|
(await ctx.database.get("botData", "version"))[0].data
|
|
148
150
|
);
|
|
149
|
-
} catch (
|
|
150
|
-
info =
|
|
151
|
+
} catch (error) {
|
|
152
|
+
info = error.message;
|
|
151
153
|
}
|
|
152
154
|
return info;
|
|
153
155
|
}
|
|
@@ -177,98 +179,109 @@ __name(getMsgCount, "getMsgCount");
|
|
|
177
179
|
// src/commands.ts
|
|
178
180
|
async function getServer(ctx, session) {
|
|
179
181
|
const log = ctx.logger("cx");
|
|
180
|
-
log.info(`Got: {"form":"${session.event.guild
|
|
182
|
+
log.info(`Got: {"form":"${session.event.guild?.id}","user":"${session.event.user?.id}","timestamp":${session.event.timestamp},"messageId":"${session.event.message?.id}"}`);
|
|
181
183
|
let msg;
|
|
182
184
|
let dataError;
|
|
183
185
|
let data;
|
|
184
186
|
let error;
|
|
185
187
|
const time = getHongKongTime();
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
msg = {
|
|
191
|
-
"time": time,
|
|
192
|
-
"data": "未指定查询 API",
|
|
193
|
-
"success": 1
|
|
194
|
-
};
|
|
195
|
-
log.info("Sent:");
|
|
196
|
-
log.info(msg);
|
|
197
|
-
return msg;
|
|
198
|
-
}
|
|
199
|
-
const response = await fetchWithTimeout(api, {}, ctx.config.timeout, log);
|
|
200
|
-
if (response.ok) {
|
|
201
|
-
data = await response.text();
|
|
202
|
-
log.info("Server data: " + data);
|
|
203
|
-
data = JSON.parse(data);
|
|
204
|
-
if (data["list"] == null) {
|
|
205
|
-
msg = {
|
|
206
|
-
"time": time,
|
|
207
|
-
"players": data["players"],
|
|
208
|
-
"version": data["version"],
|
|
209
|
-
"protocol": data["protocol"],
|
|
210
|
-
"success": 3
|
|
211
|
-
};
|
|
212
|
-
log.info("Sent:");
|
|
213
|
-
log.info(msg);
|
|
214
|
-
} else {
|
|
215
|
-
msg = {
|
|
216
|
-
"time": time,
|
|
217
|
-
"players": data["players"],
|
|
218
|
-
"version": data["version"],
|
|
219
|
-
"list": data["list"].join(", "),
|
|
220
|
-
"protocol": data["protocol"],
|
|
221
|
-
"success": 0
|
|
222
|
-
};
|
|
223
|
-
log.info("Sent:");
|
|
224
|
-
log.info(msg);
|
|
225
|
-
}
|
|
226
|
-
} else {
|
|
227
|
-
dataError = await response.text();
|
|
228
|
-
try {
|
|
229
|
-
const vError = JSON.parse(dataError);
|
|
230
|
-
error = vError["data"];
|
|
231
|
-
if (error.includes("Connection refused")) {
|
|
232
|
-
error = session.text(".close");
|
|
233
|
-
} else if (error.includes("No route to host")) {
|
|
234
|
-
error = session.text(".host");
|
|
235
|
-
} else if (error.includes("Connection timed out")) {
|
|
236
|
-
error = session.text(".timeout");
|
|
237
|
-
} else if (error.includes("Server returned too few data")) {
|
|
238
|
-
error = session.text(".fewData");
|
|
239
|
-
} else if (error.includes("Server read timed out")) {
|
|
240
|
-
error = session.text(".timeout2");
|
|
241
|
-
}
|
|
242
|
-
} catch (e) {
|
|
243
|
-
if (dataError.includes("CDN节点请求源服务器超时")) {
|
|
244
|
-
error = session.text(".timeout");
|
|
245
|
-
} else {
|
|
246
|
-
error = session.text(".unknown");
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
log.error(`Error fetching data: ${dataError}`);
|
|
250
|
-
msg = {
|
|
251
|
-
"time": time,
|
|
252
|
-
"data": error,
|
|
253
|
-
"success": 1
|
|
254
|
-
};
|
|
255
|
-
log.info("Sent:");
|
|
256
|
-
log.info(msg);
|
|
257
|
-
}
|
|
258
|
-
} catch (err) {
|
|
259
|
-
log.error(`Request error: ${err.message}`);
|
|
188
|
+
const index = ctx.config.cxV2.findIndex((item) => item.id === session.event.guild?.id);
|
|
189
|
+
if (index !== -1) {
|
|
190
|
+
const api = ctx.config.cxV2[index]["api"];
|
|
191
|
+
if (api == void 0) {
|
|
260
192
|
msg = {
|
|
261
193
|
"time": time,
|
|
262
|
-
"data":
|
|
263
|
-
"success":
|
|
194
|
+
"data": "未指定查询 API",
|
|
195
|
+
"success": 2
|
|
264
196
|
};
|
|
265
197
|
log.info("Sent:");
|
|
266
198
|
log.info(msg);
|
|
199
|
+
return msg;
|
|
267
200
|
}
|
|
201
|
+
let count = 0;
|
|
202
|
+
let list = "";
|
|
203
|
+
for (const item of api) {
|
|
204
|
+
count++;
|
|
205
|
+
try {
|
|
206
|
+
const response = await fetchWithTimeout(item, {}, ctx.config.timeout, log);
|
|
207
|
+
if (response.ok) {
|
|
208
|
+
data = await response.text();
|
|
209
|
+
log.info("Server data: " + data);
|
|
210
|
+
data = JSON.parse(data);
|
|
211
|
+
if (data["list"] == null) {
|
|
212
|
+
const temp = {
|
|
213
|
+
"count": count,
|
|
214
|
+
"players": data["players"],
|
|
215
|
+
"version": data["version"],
|
|
216
|
+
"protocol": data["protocol"]
|
|
217
|
+
};
|
|
218
|
+
log.info(`Server ${count}:`);
|
|
219
|
+
log.info(temp);
|
|
220
|
+
list = list + "\n" + session.text(".listNoPlayer", temp);
|
|
221
|
+
} else {
|
|
222
|
+
const temp = {
|
|
223
|
+
"count": count,
|
|
224
|
+
"players": data["players"],
|
|
225
|
+
"version": data["version"],
|
|
226
|
+
"list": data["list"].join(", "),
|
|
227
|
+
"protocol": data["protocol"]
|
|
228
|
+
};
|
|
229
|
+
log.info(`Server ${count}:`);
|
|
230
|
+
log.info(temp);
|
|
231
|
+
list = list + "\n" + session.text(".list", temp);
|
|
232
|
+
}
|
|
233
|
+
} else {
|
|
234
|
+
dataError = await response.text();
|
|
235
|
+
try {
|
|
236
|
+
const vError = JSON.parse(dataError);
|
|
237
|
+
error = vError["data"];
|
|
238
|
+
if (error.includes("Connection refused")) {
|
|
239
|
+
error = session.text(".close");
|
|
240
|
+
} else if (error.includes("No route to host")) {
|
|
241
|
+
error = session.text(".host");
|
|
242
|
+
} else if (error.includes("Connection timed out")) {
|
|
243
|
+
error = session.text(".timeout");
|
|
244
|
+
} else if (error.includes("Server returned too few data")) {
|
|
245
|
+
error = session.text(".fewData");
|
|
246
|
+
} else if (error.includes("Server read timed out")) {
|
|
247
|
+
error = session.text(".timeout2");
|
|
248
|
+
}
|
|
249
|
+
} catch (e) {
|
|
250
|
+
if (dataError.includes("CDN节点请求源服务器超时")) {
|
|
251
|
+
error = session.text(".timeout");
|
|
252
|
+
} else {
|
|
253
|
+
error = session.text(".unknown");
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
log.error(`Error fetching data: ${dataError}`);
|
|
257
|
+
const temp = {
|
|
258
|
+
"count": count,
|
|
259
|
+
"data": error
|
|
260
|
+
};
|
|
261
|
+
log.info(`Server ${count}:`);
|
|
262
|
+
log.info(temp);
|
|
263
|
+
list = list + "\n" + session.text(".listFailed", temp);
|
|
264
|
+
}
|
|
265
|
+
} catch (err) {
|
|
266
|
+
log.error(`Request error: ${err.message}`);
|
|
267
|
+
const temp = {
|
|
268
|
+
"count": count,
|
|
269
|
+
"data": session.text(".error")
|
|
270
|
+
};
|
|
271
|
+
log.info(`Server ${count}:`);
|
|
272
|
+
log.info(temp);
|
|
273
|
+
list = list + "\n" + session.text(".listFailed", temp);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
msg = {
|
|
277
|
+
"time": time,
|
|
278
|
+
"list": list,
|
|
279
|
+
"success": 0
|
|
280
|
+
};
|
|
268
281
|
} else {
|
|
269
282
|
msg = {
|
|
270
283
|
"time": time,
|
|
271
|
-
"success":
|
|
284
|
+
"success": 1
|
|
272
285
|
};
|
|
273
286
|
log.info("Sent:");
|
|
274
287
|
log.info(msg);
|
|
@@ -278,7 +291,7 @@ async function getServer(ctx, session) {
|
|
|
278
291
|
__name(getServer, "getServer");
|
|
279
292
|
async function getStatus(ctx, session) {
|
|
280
293
|
const log = ctx.logger("status");
|
|
281
|
-
log.info(`Got: {"form":"${session.event.guild
|
|
294
|
+
log.info(`Got: {"form":"${session.event.guild?.id}","user":"${session.event.user?.id}","timestamp":${session.event.timestamp},"messageId":"${session.event.message?.id}"}`);
|
|
282
295
|
const time = getHongKongTime();
|
|
283
296
|
let msg;
|
|
284
297
|
const vMsg = await getSystemUsage();
|
|
@@ -312,7 +325,7 @@ async function getStatus(ctx, session) {
|
|
|
312
325
|
__name(getStatus, "getStatus");
|
|
313
326
|
async function getRandom(ctx, session, min, max) {
|
|
314
327
|
const log = ctx.logger("random");
|
|
315
|
-
log.info(`Got: {"form":"${session.event.guild
|
|
328
|
+
log.info(`Got: {"form":"${session.event.guild?.id}","user":"${session.event.user?.id}","timestamp":${session.event.timestamp},"messageId":"${session.event.message?.id}"}`);
|
|
316
329
|
const time = getHongKongTime();
|
|
317
330
|
let msg;
|
|
318
331
|
let data;
|
|
@@ -335,7 +348,7 @@ async function getRandom(ctx, session, min, max) {
|
|
|
335
348
|
__name(getRandom, "getRandom");
|
|
336
349
|
async function getInfo(ctx, session) {
|
|
337
350
|
const log = ctx.logger("info");
|
|
338
|
-
log.info(`Got: {"form":"${session.event.guild
|
|
351
|
+
log.info(`Got: {"form":"${session.event.guild?.id}","user":"${session.event.user?.id}","timestamp":${session.event.timestamp},"messageId":"${session.event.message?.id}"}`);
|
|
339
352
|
const time = getHongKongTime();
|
|
340
353
|
let msg;
|
|
341
354
|
let data = await readInfoFile(ctx);
|
|
@@ -361,7 +374,7 @@ async function getInfo(ctx, session) {
|
|
|
361
374
|
__name(getInfo, "getInfo");
|
|
362
375
|
async function getRW(ctx, session) {
|
|
363
376
|
const log = ctx.logger("rw");
|
|
364
|
-
log.info(`Got: {"form":"${session.event.guild
|
|
377
|
+
log.info(`Got: {"form":"${session.event.guild?.id}","user":"${session.event.user?.id}","timestamp":${session.event.timestamp},"messageId":"${session.event.message?.id}"}`);
|
|
365
378
|
let msg;
|
|
366
379
|
let data;
|
|
367
380
|
const time = getHongKongTime();
|
|
@@ -401,7 +414,7 @@ async function getRW(ctx, session) {
|
|
|
401
414
|
__name(getRW, "getRW");
|
|
402
415
|
|
|
403
416
|
// package.json
|
|
404
|
-
var version = "2.
|
|
417
|
+
var version = "2.5.1";
|
|
405
418
|
|
|
406
419
|
// src/index.ts
|
|
407
420
|
var inject = ["database"];
|
|
@@ -412,8 +425,12 @@ var Config = import_koishi2.Schema.intersect([
|
|
|
412
425
|
timeout: import_koishi2.Schema.number().default(8e3).description("超时时间(毫秒)")
|
|
413
426
|
}).description("基础"),
|
|
414
427
|
import_koishi2.Schema.object({
|
|
415
|
-
|
|
416
|
-
|
|
428
|
+
cxV2: import_koishi2.Schema.array(
|
|
429
|
+
import_koishi2.Schema.object({
|
|
430
|
+
id: import_koishi2.Schema.string().required().description("查询 群"),
|
|
431
|
+
api: import_koishi2.Schema.array(String).description("查询 API")
|
|
432
|
+
})
|
|
433
|
+
).default([]).description("查询的 API 和 群")
|
|
417
434
|
}).description("查询"),
|
|
418
435
|
import_koishi2.Schema.object({
|
|
419
436
|
rwAPI: import_koishi2.Schema.string().default("https://api.tasaed.top/rw/").description("随机文本 API")
|
|
@@ -436,43 +453,41 @@ function apply(ctx) {
|
|
|
436
453
|
ctx.command("cx").action(async ({ session }) => {
|
|
437
454
|
const cx = await getServer(ctx, session);
|
|
438
455
|
if (cx["success"] == 0) {
|
|
439
|
-
return session
|
|
456
|
+
return session?.text(".msg", cx);
|
|
440
457
|
} else if (cx["success"] == 1) {
|
|
441
|
-
return session
|
|
458
|
+
return session?.text(".forbidden", cx);
|
|
442
459
|
} else if (cx["success"] == 2) {
|
|
443
|
-
return session
|
|
444
|
-
} else if (cx["success"] == 3) {
|
|
445
|
-
return session.text(".msgNoPlayer", cx);
|
|
460
|
+
return session?.text(".failed", cx);
|
|
446
461
|
}
|
|
447
462
|
});
|
|
448
463
|
ctx.command("status").action(async ({ session }) => {
|
|
449
464
|
const status = await getStatus(ctx, session);
|
|
450
465
|
if (status["success"] == 0) {
|
|
451
|
-
return session
|
|
466
|
+
return session?.text(".msg", status);
|
|
452
467
|
} else {
|
|
453
|
-
return session
|
|
468
|
+
return session?.text(".failed", status);
|
|
454
469
|
}
|
|
455
470
|
});
|
|
456
471
|
ctx.command("random [最小数:number] [最大数:number]").action(async ({ session }, min, max) => {
|
|
457
472
|
const random = await getRandom(ctx, session, min, max);
|
|
458
|
-
return session
|
|
473
|
+
return session?.text(".msg", random);
|
|
459
474
|
});
|
|
460
475
|
ctx.command("info").action(async ({ session }) => {
|
|
461
476
|
const info = await getInfo(ctx, session);
|
|
462
477
|
if (info["success"] == 0) {
|
|
463
|
-
return session
|
|
478
|
+
return session?.text(".msg", info);
|
|
464
479
|
} else {
|
|
465
|
-
return session
|
|
480
|
+
return session?.text(".failed", info);
|
|
466
481
|
}
|
|
467
482
|
});
|
|
468
483
|
ctx.command("rw").action(async ({ session }) => {
|
|
469
484
|
const rw = await getRW(ctx, session);
|
|
470
485
|
if (rw["success"] == 0) {
|
|
471
|
-
return session
|
|
486
|
+
return session?.text(".msg", rw);
|
|
472
487
|
} else if (rw["success"] == 1) {
|
|
473
|
-
return session
|
|
488
|
+
return session?.text(".failed1", rw);
|
|
474
489
|
} else {
|
|
475
|
-
return session
|
|
490
|
+
return session?.text(".failed2", rw);
|
|
476
491
|
}
|
|
477
492
|
});
|
|
478
493
|
}
|