-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
128 lines (117 loc) · 3.07 KB
/
utils.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
const node_util = require("node:util");
const exec = node_util.promisify(require("node:child_process").exec);
const spawn = require("node:child_process").spawn;
const query = require("source-server-query");
const config = require("./config");
// Return a new date in 24-hour format: 14:22:39
const getTimestamp = () => {
let date = new Date();
return (
date.toLocaleDateString("en-US", {
month: "2-digit",
day: "2-digit",
year: "numeric",
}) +
" " +
date.toLocaleTimeString("en-us", { hour12: false })
);
};
// Uses tasklist command and checks for the server in the process list
const isRunning = async () => {
const { stdout, stderr } = await exec("tasklist");
return (
stdout.toLowerCase().indexOf("ShooterGameServer.exe".toLowerCase()) > -1
);
};
const startServer = async (map, interaction) => {
spawn(config.map_scripts[map], [], {
cwd: config.scripts_dir,
shell: true,
}).on("spawn", () => {
let heartbeat = setInterval(() => {
serverInfo().then((status) => {
if (status.map) {
console.log(`[${getTimestamp()}] startup complete`);
interaction.editReply("Server startup complete!");
interaction.followUp({
content: `<@${interaction.user.id}> Server startup complete!`,
ephemeral: true,
});
clearInterval(heartbeat);
}
});
}, 10000);
});
};
const stopServer = async () => {
// Attempt to stop the server process
exec(`taskkill /im ShooterGameServer.exe /t`, (err, stdout, stderr) => {
// This should never run since we already made sure the process exists
if (err) {
throw err;
}
// The /^(?!\s*$).+/ regex keeps any blank lines from cluttering the output
if (stdout) {
stdout.split("\n").forEach((line) => {
if (/^(?!\s*$).+/.test(line)) {
console.log(
`[${getTimestamp()}] (TASKKILL):`,
"stdout -",
line
);
}
});
}
// The /^(?!\s*$).+/ regex keeps any blank lines from cluttering the output
if (stderr) {
stderr.split("\n").forEach((line) => {
if (/^(?!\s*$).+/.test(line)) {
console.log(
`[${getTimestamp()}] (TASKKILL):`,
"stderr -",
line
);
}
});
}
});
};
const updateServer = (interaction) => {
// SteamCMD update script for ARK
let options = [
"+login",
"anonymous",
"+force_install_dir",
config.arkserver_dir,
"+app_update",
"376030",
"+quit",
];
let update = spawn("steamcmd", options, {
cwd: config.steamcmd_dir,
shell: true,
});
update.stdout.on("data", (data) => {
if (data.includes("Success!")) {
console.log(`[${getTimestamp()}] (SteamCMD): update - success`);
interaction.editReply("Server updated! :ok_hand:");
interaction.followUp({
content: `<@${interaction.user.id}> Server updated! :ok_hand:`,
ephemeral: true,
});
}
});
};
const serverInfo = async () => {
try {
return await query.info("127.0.0.1", 27015, 1000);
} catch (err) {
return -1;
}
};
exports.timestamp = getTimestamp;
exports.serverIsRunning = isRunning;
exports.startServer = startServer;
exports.stopServer = stopServer;
exports.updateServer = updateServer;
exports.serverInfo = serverInfo;