-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
76 lines (64 loc) · 1.86 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
const express = require('express');
const { spawn } = require('child_process');
const app = express();
const colors = require('colors')
const port = 3000;
let botProcess;
const config = require('./config.json');
const User = require("./database/users");
const Thread = require("./database/threads");
let users = [];
let threads = [];
/*
async function loadUsersAndThreads() {
try {
const users2 = await User.getAll();
const threads2 = await Thread.getAll();
users.push(...users2.map(i => i.userId))
threads.push(...threads2.map(i => i.threadId))
console.log("Users and threads data loaded.".green);
} catch (error) {
console.error('Error loading users and threads:', error);
}
}
*/
async function onBot() {
botProcess = spawn('node', ['main.js'], {
cwd: __dirname,
stdio: 'inherit',
shell: true
});
botProcess.on('close', (code) => {
if (code === 2) {
console.log('Restarting BotBee...'.red);
onBot();
} else if (code !== 0) {
console.error(`Bot process exited with code ${code}`);
}
});
}
onBot();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
// loadUsersAndThreads();
});
app.get('/dashboard-data', async (req, res) => {
try {
const uptime = process.uptime();
const dashboardData ={
botName: config.botName,
prefix: config.prefix,
adminName: config.adminName,
totalUsers: users,
totalThreads:threads,
uptime
}
res.json(dashboardData);
} catch (error) {
console.error('Error fetching dashboard data:', error);
res.status(500).send('Internal Server Error');
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`.cyan);
});