forked from BOTCAHX/RTXZY-MD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
haruka.js
87 lines (73 loc) · 2.2 KB
/
haruka.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
78
79
80
81
82
83
84
85
86
87
const { spawn } = require('child_process');
const path = require('path');
const express = require('express');
const { createServer } = require('http');
const WebSocket = require('ws');
const fs = require('fs');
const app = express();
const server = createServer(app);
const wss = new WebSocket.Server({ server });
// Store the output from index.js
let output = '';
function start() {
let args = [path.join(__dirname, 'index.js'), ...process.argv.slice(2)];
console.log([process.argv[0], ...args].join('\n'));
let p = spawn(process.argv[0], args, { stdio: ['inherit', 'pipe', 'inherit', 'ipc'] });
// Listen for data events on the stdout stream
p.stdout.on('data', (data) => {
const text = data.toString();
output += text;
console.log(text);
fs.appendFileSync('public/console.log', text); // Write to the console.log file
// Send the output to the connected WebSocket clients
wss.clients.forEach((client) => {
client.send(text);
});
});
p.on('message', (data) => {
if (data === 'reset') {
console.log('Restarting Bot...');
p.kill();
start();
delete p;
}
});
p.on('exit', (code) => {
console.error('Exited with code:', code);
if (code === '.' || code === 1 || code === 0 || code === null) {
start();
}
});
}
// Start the WebSocket server
wss.on('connection', (ws) => {
console.log('WebSocket client connected');
// Handle WebSocket connection
ws.on('message', (message) => {
console.log('Received message:', message);
// Handle client messages if needed
});
// Handle WebSocket disconnection
ws.on('close', () => {
console.log('WebSocket client disconnected');
});
});
// Serve the index.html file
app.use(express.static(path.join(__dirname, 'public')));
// Start the server
const PORT = 5000;
const serverInstance = server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// Start the bot
start();
// Handle server shutdown or Ctrl+C
process.on('SIGINT', () => {
console.log('Stopping server...');
serverInstance.close(() => {
console.log('Server stopped.');
// Clear the console.log file
fs.writeFileSync('public/console.log', '');
process.exit(0);
});
});