-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.ts
64 lines (56 loc) · 1.54 KB
/
test.ts
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
import { Client } from "@utils/serverListPingAPI";
import { writeFileSync } from "fs";
import * as readline from "readline";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
console.clear();
console.log(`请输入服务器地址
如:bgp.mortar.top:25565
输入 'exit | quit | end | stop' 退出程序`);
// 首次调用函数以开始循环
promptForServerAddress();
process.on("uncaughtException", (error) => {
console.error("Uncaught Exception: ", error);
process.exit(1);
});
function promptForServerAddress() {
rl.question(":", async (input) => {
switch (input.trim().toLowerCase()) {
case "exit":
case "quit":
case "end":
case "stop":
// 检查是否输入了退出命令
console.log(`now exiting...`);
rl.close(); // 关闭readline接口
return;
case "clear":
console.clear();
next();
return;
default:
}
let [host, port] = input.split(":");
if (!host.trim().length) {
console.log("请输入正确的服务器地址");
// 异步递归
next();
return;
}
if (!port) port = "25565";
const client = new Client(host, port, "1.16.5");
const request = client.getServerListPingWithCache();
const res = await request().then((res) => res);
writeFileSync("test.json", JSON.stringify(res, null, 2));
console.log("已写入到 ./test.json 中");
next();
});
}
function next() {
setTimeout(() => {
promptForServerAddress();
}, 300);
}