-
Notifications
You must be signed in to change notification settings - Fork 6
/
start.mjs
262 lines (223 loc) · 6.09 KB
/
start.mjs
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import blessed from "blessed";
import chalk from "chalk";
import { spawn } from "child_process";
import os from "os";
import moment from "moment";
import figlet from "figlet";
// Create a screen object.
const screen = blessed.screen({
smartCSR: true,
title: "Taurus Bot Console",
});
// Create a box that fills the entire screen.
const box = blessed.box({
top: 0,
left: 0,
width: "39%",
height: "100%",
content: "",
tags: true,
border: {
type: "line",
},
style: {
fg: "white",
bg: "black",
border: {
fg: "#ff0000", // Red
},
},
});
// Create a console box at the top right.
const consoleBox = blessed.box({
top: 0,
right: 0,
width: "80%",
height: "100%",
content: "{underline}Taurus Console{/underline}",
tags: true,
border: {
type: "line",
},
style: {
fg: "white",
bg: "black",
border: {
fg: "#ff0000", // Red
},
},
});
// Append our boxes to the screen.
screen.append(box);
screen.append(consoleBox);
let bot = null;
let botStartTime = null;
let botStatus = "Offline";
// Function to start the Discord bot.
function startBot() {
if (bot) {
writeToConsole(
chalk.red("Bot is already running. Please stop the bot first."),
);
return;
}
// Modify this line to start your bot with the correct command and arguments
bot = spawn("node", ["bot.js"], {
stdio: ["pipe", "pipe", "pipe", "pipe", "pipe", "pipe", process.stderr],
});
botStartTime = moment();
botStatus = "Online";
updateStats(); // Update the box content with system and bot stats
bot.on("error", (err) => {
writeToConsole(chalk.red(`Error starting bot: ${err.message}`));
});
// Pipe the bot's stdout and stderr to the console
bot.stdout.on("data", (data) => {
writeToConsole(data.toString());
});
bot.stderr.on("data", (data) => {
writeToConsole(chalk.red(data.toString()));
});
}
// Function to stop the Discord bot.
function stopBot() {
if (!bot) {
writeToConsole(
chalk.red("Bot is not running. Please start the bot first."),
);
return;
}
writeToConsole(chalk.red("Stopping bot..."));
bot.kill();
bot = null;
botStartTime = null;
botStatus = "Offline";
setTimeout(() => {
updateStats(); // Update the box content with system and bot stats after stopping the bot
}, 1000);
}
// Function to restart the Discord bot.
function restartBot() {
stopBot();
const gitPull = spawn("git", ["pull"]);
gitPull.stdout.on("data", (data) => {
writeToConsole(chalk.italic(`[GIT] stdout: ${data}`));
});
gitPull.stderr.on("data", (data) => {
writeToConsole(chalk.italic(`[GIT] stderr: ${data}`));
});
gitPull.on("close", (code) => {
writeToConsole(
chalk.italic(`[GIT] child process exited with code ${code}`),
);
const pnpmInstall = spawn("pnpm", ["install"], { shell: true });
pnpmInstall.stdout.on("data", (data) => {
const message = data.toString().trim();
message && writeToConsole(chalk.italic(`[PNPM] stdout: ${message}`));
});
pnpmInstall.stderr.on("data", (data) => {
writeToConsole(chalk.italic(`[PNPM] stderr: ${data}`));
});
pnpmInstall.on("close", (code) => {
writeToConsole(
chalk.italic(`[PNPM] child process exited with code ${code}`),
);
setTimeout(() => {
startBot();
writeToConsole(chalk.red("Bot restarted."));
}, 1000);
});
});
}
// Function to refresh the console.
function refreshConsole() {
consoleBox.setContent("{underline}Taurus Console{/underline}");
writeToConsole("Console refreshed.");
screen.render();
}
let title = "";
figlet.text(
"Taurus",
{
font: "Standard",
horizontalLayout: "default",
verticalLayout: "default",
width: 100,
whitespaceBreak: true,
},
function (err, data) {
if (err) {
console.log("Something went wrong...");
console.dir(err);
return;
}
title = chalk.red(data); // Red
},
);
// Function to update the box content with system and bot stats
function updateStats() {
// System stats
const serverUptime = moment.duration(os.uptime() * 1000).humanize();
const totalMemory = (os.totalmem() / 1024 / 1024 / 1024).toFixed(2) + " GB";
const cpuCores = os.cpus().length.toString();
const osInfo = `${os.type()} (${os.release()})`;
const botUptime = botStartTime
? moment.duration(moment().diff(botStartTime)).humanize()
: "Not available";
const botMemoryUsage =
(process.memoryUsage().heapUsed / 1024 / 1024 / 1024).toFixed(2) + " GB";
box.setContent(
`${title}\n\n` +
`${chalk.red("Bot Status:")} ${botStatus === "Online" ? chalk.green(botStatus) : chalk.red(botStatus)}\n\n` +
`${chalk.red("VPS Uptime:")} ${chalk.white(serverUptime)}\n` +
`${chalk.red("Total Memory:")} ${chalk.white(totalMemory)}\n` +
`${chalk.red("Bot Uptime:")} ${chalk.white(botUptime)}\n` +
`${chalk.red("Bot Memory Usage:")} ${chalk.white(botMemoryUsage)}\n` +
`${chalk.red("CPU Threads:")} ${chalk.white(cpuCores)}\n` +
`${chalk.red("OS Info:")} ${chalk.white(osInfo)}\n\n` +
`${chalk.red("Commands:")}\n` +
`${chalk.green("S")} - ${chalk.white("Start Bot")}\n` +
`${chalk.green("X")} - ${chalk.white("Stop Bot")}\n` +
`${chalk.green("R")} - ${chalk.white("Restart & Update Bot")}\n` +
`${chalk.green("L")} - ${chalk.white("Refresh Console")}\n\n` +
`${chalk.red("Press")} ${chalk.white("Ctrl+C")} ${chalk.red("to stop the bot and exit.")}\n\n\n`,
);
screen.render();
}
// Function to write messages to the console box.
function writeToConsole(message) {
const content = consoleBox.getContent();
const newContent = `${content}\n${chalk.green(message)}`;
consoleBox.setContent(newContent);
screen.render();
}
// Update the stats immediately and then every minute
updateStats();
setInterval(updateStats, 60000);
// Terminate the bot process on Ctrl+C
process.on("SIGINT", () => {
stopBot();
setTimeout(() => {
process.exit(0);
}, 1000);
});
// Start the bot when script is run initially.
startBot();
// Listen for keystrokes and map them to commands.
screen.key(["S", "s"], () => {
startBot();
});
screen.key(["X", "x"], () => {
stopBot();
});
screen.key(["R", "r"], () => {
restartBot();
});
screen.key(["L", "l"], () => {
refreshConsole();
});
screen.key(["escape", "q", "C-c"], function (ch, key) {
return process.exit(0);
});
// Render the screen.
screen.render();