-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
219 lines (189 loc) Β· 5.48 KB
/
Copy pathindex.js
File metadata and controls
219 lines (189 loc) Β· 5.48 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import "./config.js";
import { spawn } from "child_process";
import path from "path";
import fs from "fs";
import os from "os";
import express from "express";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const ports = global.ports;
let availablePortIndex = 0;
let version = "1.0.0";
function checkPort(port) {
return new Promise((resolve, reject) => {
const app = express();
const server = app.listen(port, () => {
server.close();
resolve(true);
});
server.on("error", reject);
});
}
async function startServer() {
const port = ports[availablePortIndex];
try {
const isPortAvailable = await checkPort(port);
if (isPortAvailable) {
console.log("\x1b[33m%s\x1b[0m", `π Port ${port} is open`);
const app = express();
// ExpressJS buat akses dari http ip:port/
app.get("/", (req, res) => {
res.setHeader("Content-Type", "application/json");
const data = {
success: true,
status: 200,
author: "Feri",
message: "Bot Successfully Activated!",
};
const result = {
response: data,
};
res.send(JSON.stringify(result, null, 2));
});
app.listen(port);
}
} catch (error) {
console.log(`Port ${port} is already in use. Trying another port...`);
availablePortIndex++;
if (availablePortIndex >= ports.length) {
console.log("No more available ports. Exiting...");
process.exit(1);
} else {
ports[availablePortIndex] = parseInt(port) + 1;
await startServer();
}
}
}
startServer();
let isRunning = false;
function start(file) {
if (isRunning) return;
isRunning = true;
const args = [path.join(__dirname, file), ...process.argv.slice(2)];
const p = spawn(process.argv[0], args, {
stdio: ["inherit", "inherit", "inherit", "ipc"],
});
p.on("message", (data) => {
console.log("\x1b[36m%s\x1b[0m", `π’ RECEIVED ${data}`);
switch (data) {
case "reset":
p.kill();
isRunning = false;
start.apply(this, arguments);
break;
case "uptime":
p.send(process.uptime());
break;
}
});
p.on("exit", (code) => {
isRunning = false;
console.error("\x1b[31m%s\x1b[0m", `Exited with code: ${code}`);
if (code === 0) return;
fs.watchFile(args[0], () => {
fs.unwatchFile(args[0]);
console.error(
"\x1b[31m%s\x1b[0m",
`File ${args[0]} has been modified. Script will restart...`,
);
start("main.js");
});
setTimeout(() => {
start("main.js");
}, 1000);
});
p.on("error", (err) => {
console.error("\x1b[31m%s\x1b[0m", `Error: ${err}`);
p.kill();
isRunning = false;
console.error(
"\x1b[31m%s\x1b[0m",
`Error occurred. Script will restart...`,
);
setTimeout(() => {
start("main.js");
}, 1000);
});
const pluginsFolder = path.join(__dirname, "plugins");
try {
const telegrafPackagePath = path.join(
__dirname,
"node_modules",
"telegraf",
"package.json",
);
const packageData = JSON.parse(
fs.readFileSync(telegrafPackagePath, "utf8"),
);
version = packageData.version;
} catch (e) {
console.error(
"\x1b[31m%s\x1b[0m",
`β Telegraf library is not installed. Please run: npm install telegraf`,
);
}
fs.readdir(pluginsFolder, (err, files) => {
if (err) {
console.error(
"\x1b[31m%s\x1b[0m",
`Error reading plugins folder: ${err}`,
);
return;
}
if (!Array.isArray(files) || files.length === 0) {
console.log(
"\x1b[33m%s\x1b[0m",
`π‘ No plugins found in folder ${pluginsFolder}`,
);
return;
}
console.log(
"\x1b[33m%s\x1b[0m",
`π‘ Found ${files.length} plugins in folder ${pluginsFolder}`,
);
console.log(
"\x1b[32m%s\x1b[0m",
`β
Telegraf library version ${version} is installed`,
);
console.log("\x1b[36m%s\x1b[0m", "[");
const displayFiles = files.slice(0, 15);
displayFiles.forEach((file) => {
console.log("\x1b[32m%s\x1b[0m", ` '${file}',`);
});
if (files.length > 15) {
const remainingCount = files.length - 15;
console.log("\x1b[36m%s\x1b[0m", ` ... ${remainingCount} more items`);
}
console.log("\x1b[36m%s\x1b[0m", "]");
});
console.log(
`π₯οΈ \x1b[33m${os.type()}\x1b[0m, \x1b[33m${os.release()}\x1b[0m - \x1b[33m${os.arch()}\x1b[0m`,
);
const ramInGB = os.totalmem() / (1024 * 1024 * 1024);
console.log(`πΎ \x1b[33mTotal RAM: ${ramInGB.toFixed(2)} GB\x1b[0m`);
const freeRamInGB = os.freemem() / (1024 * 1024 * 1024);
console.log(`π½ \x1b[33mFree RAM: ${freeRamInGB.toFixed(2)} GB\x1b[0m`);
console.log("\x1b[33m%s\x1b[0m", `π Modified by FeriPratama`);
console.log("\x1b[33m%s\x1b[0m", `π Original Script by BETABOTZ`);
setInterval(() => {}, 1000);
}
const tmpDir = "./tmp";
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir);
console.log("\x1b[33m%s\x1b[0m", `π Created directory ${tmpDir}`);
}
start("main.js");
process.on("unhandledRejection", (reason) => {
console.error("\x1b[31m%s\x1b[0m", `Unhandled promise rejection: ${reason}`);
console.error(
"\x1b[31m%s\x1b[0m",
"Unhandled promise rejection. Script will restart...",
);
setTimeout(() => {
start("main.js");
}, 1000);
});
process.on("exit", (code) => {
console.error(`Exited with code: ${code}`);
});