-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (63 loc) · 2.45 KB
/
index.js
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
const { spawn } = require("child_process");
const axios = require("axios");
const logger = require("./utils/log");
///////////////////////////////////////////////////////////
//========= Create website for dashboard/uptime =========//
///////////////////////////////////////////////////////////
const express = require('express');
const path = require('path');
const app = express();
const port = process.env.PORT || 8080;
// Serve the index.html file
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '/index.html'));
});
// Start the server and add error handling
app.listen(port, () => {
logger(`Server is running on port ${port}...`, "[ Starting ]");
}).on('error', (err) => {
if (err.code === 'EACCES') {
logger(`Permission denied. Cannot bind to port ${port}.`, "[ Error ]");
} else {
logger(`Server error: ${err.message}`, "[ Error ]");
}
});
/////////////////////////////////////////////////////////
//========= Create start bot and make it loop =========//
/////////////////////////////////////////////////////////
// Initialize global restart counter
global.countRestart = global.countRestart || 0;
function startBot(message) {
if (message) logger(message, "[ Starting ]");
const child = spawn("node", ["--trace-warnings", "--async-stack-traces", "Priyansh.js"], {
cwd: __dirname,
stdio: "inherit",
shell: true
});
child.on("close", (codeExit) => {
if (codeExit !== 0 && global.countRestart < 5) {
global.countRestart += 1;
logger(`Bot exited with code ${codeExit}. Restarting... (${global.countRestart}/5)`, "[ Restarting ]");
startBot();
} else {
logger(`Bot stopped after ${global.countRestart} restarts.`, "[ Stopped ]");
}
});
child.on("error", (error) => {
logger(`An error occurred: ${JSON.stringify(error)}`, "[ Error ]");
});
};
////////////////////////////////////////////////
//========= Check update from Github =========//
////////////////////////////////////////////////
axios.get("https://raw.githubusercontent.com/priyanshu192/bot/main/package.json")
.then((res) => {
logger(res.data.name, "[ NAME ]");
logger(`Version: ${res.data.version}`, "[ VERSION ]");
logger(res.data.description, "[ DESCRIPTION ]");
})
.catch((err) => {
logger(`Failed to fetch update info: ${err.message}`, "[ Update Error ]");
});
// Start the bot
startBot();