koishi-plugin-node-async-bot-all 2.3.0 → 2.5.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/index.d.ts CHANGED
@@ -11,8 +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
- cxAPI: string;
19
+ cxV2: Array<ConfigCxV2>;
16
20
  rwAPI: string;
17
21
  timeout: number;
18
22
  }
package/lib/index.js CHANGED
@@ -33,7 +33,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
33
33
  // src/locales/zh-CN.yml
34
34
  var require_zh_CN = __commonJS({
35
35
  "src/locales/zh-CN.yml"(exports2, module2) {
36
- module2.exports = { commands: { cx: { description: "查询服务器当前人数。", messages: { msg: "{time}\n【MC服务器当前人数】\n➣ {version}:{players}\n➣ 玩家列表:{list}\n➣ 协议版本:{protocol}\n进服指南请在群公告中查看。", msgNoPlayer: "{time}\n【MC服务器当前人数】\n➣ {version}:{players}\n➣ 协议版本:{protocol}\n进服指南请在群公告中查看。", forbidden: "{time}\n此指令不允许在本群使用。", failed: "{time}\n查询失败:{data}\n请稍后重试。", 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)。" } } } };
36
+ 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
37
  }
38
38
  });
39
39
 
@@ -128,6 +128,7 @@ async function fetchWithTimeout(url, options = {}, timeout = 5e3, log) {
128
128
  return response;
129
129
  } catch (error) {
130
130
  clearTimeout(timeoutId);
131
+ log.error(error);
131
132
  log.error(`${error.name}: ${error.message}`);
132
133
  if (error.name === "AbortError") {
133
134
  throw new Error("请求超时");
@@ -179,91 +180,107 @@ async function getServer(ctx, session) {
179
180
  const log = ctx.logger("cx");
180
181
  log.info(`Got: {"form":"${session.event.guild.id}","user":"${session.event.user.id}","timestamp":${session.event.timestamp},"messageId":"${session.event.message.id}"}`);
181
182
  let msg;
182
- let api;
183
183
  let dataError;
184
184
  let data;
185
185
  let error;
186
186
  const time = getHongKongTime();
187
- if (session.event.guild.id == "757729218" || session.event.guild.id == "1047732162" || session.event.guild.id == "917260212") {
188
- try {
189
- if (session.event.guild.id == "917260212") {
190
- api = ctx.config.cxAPI2;
191
- } else {
192
- api = ctx.config.cxAPI;
193
- }
194
- const response = await fetchWithTimeout(api, {}, ctx.config.timeout, log);
195
- if (response.ok) {
196
- data = await response.text();
197
- log.info("Server data: " + data);
198
- data = JSON.parse(data);
199
- if (data["list"] == null) {
200
- msg = {
201
- "time": time,
202
- "players": data["players"],
203
- "version": data["version"],
204
- "protocol": data["protocol"],
205
- "success": 3
206
- };
207
- log.info("Sent:");
208
- log.info(msg);
209
- } else {
210
- msg = {
211
- "time": time,
212
- "players": data["players"],
213
- "version": data["version"],
214
- "list": data["list"].join(", "),
215
- "protocol": data["protocol"],
216
- "success": 0
217
- };
218
- log.info("Sent:");
219
- log.info(msg);
220
- }
221
- } else {
222
- dataError = await response.text();
223
- try {
224
- const vError = JSON.parse(dataError);
225
- error = vError["data"];
226
- if (error.includes("Connection refused")) {
227
- error = session.text(".close");
228
- } else if (error.includes("No route to host")) {
229
- error = session.text(".host");
230
- } else if (error.includes("Connection timed out")) {
231
- error = session.text(".timeout");
232
- } else if (error.includes("Server returned too few data")) {
233
- error = session.text(".fewData");
234
- } else if (error.includes("Server read timed out")) {
235
- error = session.text(".timeout2");
236
- }
237
- } catch (e) {
238
- if (dataError.includes("CDN节点请求源服务器超时")) {
239
- error = session.text(".timeout");
240
- } else {
241
- error = session.text(".unknown");
242
- }
243
- }
244
- log.error(`Error fetching data: ${dataError}`);
245
- msg = {
246
- "time": time,
247
- "data": error,
248
- "success": 1
249
- };
250
- log.info("Sent:");
251
- log.info(msg);
252
- }
253
- } catch (err) {
254
- log.error(`Request error: ${err.message}`);
187
+ const index = ctx.config.cxV2.findIndex((item) => item.id === session.event.guild.id);
188
+ if (index !== -1) {
189
+ const api = ctx.config.cxV2[index]["api"];
190
+ if (api == void 0) {
255
191
  msg = {
256
192
  "time": time,
257
- "data": session.text(".error"),
258
- "success": 1
193
+ "data": "未指定查询 API",
194
+ "success": 2
259
195
  };
260
196
  log.info("Sent:");
261
197
  log.info(msg);
198
+ return msg;
262
199
  }
200
+ let count = 0;
201
+ let list = "";
202
+ for (const item of api) {
203
+ count++;
204
+ try {
205
+ const response = await fetchWithTimeout(item, {}, ctx.config.timeout, log);
206
+ if (response.ok) {
207
+ data = await response.text();
208
+ log.info("Server data: " + data);
209
+ data = JSON.parse(data);
210
+ if (data["list"] == null) {
211
+ const temp = {
212
+ "count": count,
213
+ "players": data["players"],
214
+ "version": data["version"],
215
+ "protocol": data["protocol"]
216
+ };
217
+ log.info(`Server ${count}:`);
218
+ log.info(temp);
219
+ list = list + "\n" + session.text(".listNoPlayer", temp);
220
+ } else {
221
+ const temp = {
222
+ "count": count,
223
+ "players": data["players"],
224
+ "version": data["version"],
225
+ "list": data["list"].join(", "),
226
+ "protocol": data["protocol"]
227
+ };
228
+ log.info(`Server ${count}:`);
229
+ log.info(temp);
230
+ list = list + "\n" + session.text(".list", temp);
231
+ }
232
+ } else {
233
+ dataError = await response.text();
234
+ try {
235
+ const vError = JSON.parse(dataError);
236
+ error = vError["data"];
237
+ if (error.includes("Connection refused")) {
238
+ error = session.text(".close");
239
+ } else if (error.includes("No route to host")) {
240
+ error = session.text(".host");
241
+ } else if (error.includes("Connection timed out")) {
242
+ error = session.text(".timeout");
243
+ } else if (error.includes("Server returned too few data")) {
244
+ error = session.text(".fewData");
245
+ } else if (error.includes("Server read timed out")) {
246
+ error = session.text(".timeout2");
247
+ }
248
+ } catch (e) {
249
+ if (dataError.includes("CDN节点请求源服务器超时")) {
250
+ error = session.text(".timeout");
251
+ } else {
252
+ error = session.text(".unknown");
253
+ }
254
+ }
255
+ log.error(`Error fetching data: ${dataError}`);
256
+ const temp = {
257
+ "count": count,
258
+ "data": error
259
+ };
260
+ log.info(`Server ${count}:`);
261
+ log.info(temp);
262
+ list = list + "\n" + session.text(".listFailed", temp);
263
+ }
264
+ } catch (err) {
265
+ log.error(`Request error: ${err.message}`);
266
+ const temp = {
267
+ "count": count,
268
+ "data": session.text(".error")
269
+ };
270
+ log.info(`Server ${count}:`);
271
+ log.info(temp);
272
+ list = list + "\n" + session.text(".listFailed", temp);
273
+ }
274
+ }
275
+ msg = {
276
+ "time": time,
277
+ "list": list,
278
+ "success": 0
279
+ };
263
280
  } else {
264
281
  msg = {
265
282
  "time": time,
266
- "success": 2
283
+ "success": 1
267
284
  };
268
285
  log.info("Sent:");
269
286
  log.info(msg);
@@ -396,7 +413,7 @@ async function getRW(ctx, session) {
396
413
  __name(getRW, "getRW");
397
414
 
398
415
  // package.json
399
- var version = "2.3.0";
416
+ var version = "2.5.0";
400
417
 
401
418
  // src/index.ts
402
419
  var inject = ["database"];
@@ -407,8 +424,12 @@ var Config = import_koishi2.Schema.intersect([
407
424
  timeout: import_koishi2.Schema.number().default(8e3).description("超时时间(毫秒)")
408
425
  }).description("基础"),
409
426
  import_koishi2.Schema.object({
410
- cxAPI: import_koishi2.Schema.string().default("https://api.tasaed.top/get/minecraftServer/").description("查询 API"),
411
- cxAPI2: import_koishi2.Schema.string().default("https://api.tasaed.top/get/minecraftServer/bas.php").description("查询 API 2")
427
+ cxV2: import_koishi2.Schema.array(
428
+ import_koishi2.Schema.object({
429
+ id: import_koishi2.Schema.string().required().description("查询 群"),
430
+ api: import_koishi2.Schema.array(String).description("查询 API")
431
+ })
432
+ ).default([]).description("查询的 API 和 群")
412
433
  }).description("查询"),
413
434
  import_koishi2.Schema.object({
414
435
  rwAPI: import_koishi2.Schema.string().default("https://api.tasaed.top/rw/").description("随机文本 API")
@@ -433,11 +454,9 @@ function apply(ctx) {
433
454
  if (cx["success"] == 0) {
434
455
  return session.text(".msg", cx);
435
456
  } else if (cx["success"] == 1) {
436
- return session.text(".failed", cx);
437
- } else if (cx["success"] == 2) {
438
457
  return session.text(".forbidden", cx);
439
- } else if (cx["success"] == 3) {
440
- return session.text(".msgNoPlayer", cx);
458
+ } else if (cx["success"] == 2) {
459
+ return session.text(".failed", cx);
441
460
  }
442
461
  });
443
462
  ctx.command("status").action(async ({ session }) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-node-async-bot-all",
3
3
  "description": "NodeAsync Bot插件(自用)",
4
- "version": "2.3.0",
4
+ "version": "2.5.0",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [