-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.js
More file actions
84 lines (76 loc) · 2.06 KB
/
main.js
File metadata and controls
84 lines (76 loc) · 2.06 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Function created by C3C BotChat
* https://github.com/c3cbot/legacy-c3cbot
*/
const childProcess = require("child_process");
const fs = require("fs");
const path = require("path");
const log = require("./core/util/log.js"); log.sync();
(async () => {
ensureExists(path.join(__dirname, "data"));
// fs.writeFileSync(path.join(__dirname, "data", "isStart.txt"), "1");
var semver = require("semver");
var nodeVersion = semver.parse(process.version);
if (nodeVersion.major < 16) {
console.error("MAIN", "ERROR: Node.JS 16+ required in this version!");
console.error("MAIN", "Node.JS version running this bot:", process.version);
process.exit(1);
}
function spawn(cmd, arg) {
return new Promise(resolve => {
var npmProcess = childProcess.spawn(cmd, arg, {
shell: true,
stdio: "pipe",
cwd: __dirname
});
npmProcess.on("close", function (code) {
resolve(code);
});
});
}
async function loader(first) {
if (!first) {
console.log();
console.log("MAIN", `7378278(RESTART) error code found. Restarting...`);
}
child = childProcess.spawn("node", ["--trace-warnings", "index.js"], {
cwd: __dirname,
maxBuffer: 16384 * 1024,
stdio: 'inherit',
shell: true
});
child.on("close", async (code) => {
//UNIX, why? (limited to 8-bit)
//Original code: 7378278
if (code % 256 == 102) {
await loader(false);
return;
}
console.log();
console.log("MAIN", `Function Index throw ${code} (not 7378278(RESTART)). Shutting down...`);
// fs.writeFileSync(path.join(__dirname, "data", "isStart.txt"), "0");
process.exit();
});
child.on("error", function (err) {
console.log();
console.log("MAIN", "Error:" + err);
});
}
await loader(true);
})();
function ensureExists(path, func, mask) {
if (typeof mask != 'number') {
mask = 0o777;
}
try {
fs.mkdirSync(path, {
mode: mask,
recursive: true
});
return;
} catch (ex) {
return {
err: ex
};
}
}