-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (54 loc) · 1.78 KB
/
index.js
File metadata and controls
68 lines (54 loc) · 1.78 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
//START
const { spawn } = require("child_process");
const http = require("http");
const logger = require("./log");
const { Port } = require('./config.json');
//CHECK FOR UPDATES
require('axios')
.get('https://raw.githubusercontent.com/senthanh/SenBot/main/package.json')
.then(res => {
const { version } = res.data;
const currentVersion = require('./package.json').version;
let compare = require('compare-versions')(currentVersion, version);
if (compare == -1) logger.info('Đã có phiên bản mới, hãy lên https://github.com/senthanh/SenBot để tham khảo')
else logger.info('Bạn đang sử dụng phiên bản mới nhất!')
})
.catch(e => logger.warn('Không thể kiểm tra cập nhật.'))
/*
CHECK NODE VERSION
*/
const majorNodeVersion = parseInt(process.version.slice(1,process.version.indexOf('.')));
if (majorNodeVersion != 14) {
logger.error("SenBot requires Node 14.x.x to run!");
process.exit(0);
}
/*
UPTIMEROBOT TRICK FOR 24/7
*/
http.createServer(function (_req, res) {
res.writeHead(200, "OK", { "Content-Type": "text/plain" });
res.write("Thanks for using SenBot");
res.end();
}).listen(Port || 8080);
logger.info("SenBot is starting...");
/*
CREATE A CHILD PROCESS AND HANDLE ERRORS
*/
function SenBot() {
const child = spawn("node", ["--trace-warnings", "--async-stack-traces", "sen.js"], {
cwd: __dirname,
stdio: "inherit",
shell: true
});
child.on("close", async (exitCode) => {
if (exitCode == 0) return;
await new Promise(resolve => setTimeout(resolve, 1000));
logger.warn("SenBot is restarting...");
SenBot();
});
child.on("error", function (error) {
logger.error("An error occurred: " + JSON.stringify(error));
});
};
SenBot();
//END