-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.js
135 lines (114 loc) · 3.83 KB
/
webserver.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
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
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const fs = require('fs');
const path = require('path');
module.exports = function(bot) {
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// bot toggle flags.
bot.chatPaused = false;
bot.antiAfkPaused = false;
// in-memory logs.
const fullChatLog = [];
const fullConsoleLog = [];
// create a logs folder if it doesn't exist.
const logsFolder = path.join(__dirname, 'logs');
if (!fs.existsSync(logsFolder)) {
fs.mkdirSync(logsFolder);
}
// generate filenames that include the Unix timestamp.
const timestamp = Date.now();
const chatLogPath = path.join(logsFolder, `chat_${timestamp}.log`);
const consoleLogPath = path.join(logsFolder, `console_${timestamp}.log`);
// flags to track if user has saved logs.
let chatLogSaved = false;
let consoleLogSaved = false;
// create/clear log files on startup.
fs.writeFileSync(chatLogPath, '');
fs.writeFileSync(consoleLogPath, '');
app.use(express.static('public'));
app.use(express.json());
// override console.log to capture logs in memory and file.
const originalLog = console.log;
console.log = function(...args) {
const message = args.join(' ');
fullConsoleLog.push(message);
fs.appendFileSync(consoleLogPath, message + '\n');
io.emit('consoleLog', message);
originalLog.apply(console, args);
};
// api endpoint to retrieve full console logs.
app.get('/api/consoleLog', (req, res) => {
res.json({ logs: fullConsoleLog });
});
// api endpoint to retrieve full chat log.
app.get('/api/chatLog', (req, res) => {
res.json({ logs: fullChatLog });
});
// endpoints for saving logs.
app.post('/saveChatLog', (req, res) => {
chatLogSaved = true;
res.json({ status: 'ok', message: 'Log will be saved.' });
});
app.post('/saveConsoleLog', (req, res) => {
consoleLogSaved = true;
res.json({ status: 'ok', message: 'Log will be saved.' });
});
// toggle chat processing.
app.post('/toggle/chat', (req, res) => {
bot.chatPaused = !bot.chatPaused;
res.json({ chatPaused: bot.chatPaused });
io.emit('toggleUpdate', { chatPaused: bot.chatPaused });
});
// toggle anti-AFK actions.
app.post('/toggle/antiAfk', (req, res) => {
bot.antiAfkPaused = !bot.antiAfkPaused;
res.json({ antiAfkPaused: bot.antiAfkPaused });
io.emit('toggleUpdate', { antiAfkPaused: bot.antiAfkPaused });
});
// endpoint to send a chat message.
app.post('/say', (req, res) => {
const message = req.body.message;
if (message) {
bot.chat(message);
res.json({ status: 'ok', message });
} else {
res.status(400).json({ status: 'error', error: 'No message provided' });
}
});
// stop the bot.
app.post('/stop', (req, res) => {
res.json({ status: 'ok', message: 'Bot is stopping' });
// delete log files if not saved.
if (!chatLogSaved && fs.existsSync(chatLogPath)) {
fs.unlinkSync(chatLogPath);
}
if (!consoleLogSaved && fs.existsSync(consoleLogPath)) {
fs.unlinkSync(consoleLogPath);
}
process.exit(0);
});
io.on('connection', (socket) => {
console.log('Client connected');
socket.emit('botInfo', { username: bot.username });
socket.on('disconnect', () => {
console.log('Client disconnected');
});
});
// broadcast each chat message and store in full log and file.
bot.on('message', (message) => {
const msgStr = message.toString();
fullChatLog.push(msgStr);
fs.appendFileSync(chatLogPath, msgStr + '\n');
io.emit('chat', msgStr);
});
// broadcast bot responses.
bot.on('botResponse', (message) => {
io.emit('botResponse', message);
});
server.listen(1024, () => {
console.log('Web interface listening on http://localhost:1024');
});
};