-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstderrLog.js
executable file
·70 lines (55 loc) · 1.98 KB
/
stderrLog.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
#!/usr/local/bin/node
// Info:
// To make this file runable, run `chmod +x ./stderrLog.js`
// Example useage: `node crash.js 2>&1 | ./stderrLog.js`
// Requires logWebhook in env.json
// Using fetch instead of discord.js, less libraries means less chance this script fails
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const fs = require("node:fs")
const logWebhook = require("./env.json").logWebhook;
// Collect stdin here
let data = "";
// Read input
process.stdin.on("readable", () => {
let chunk;
while (null !== (chunk = process.stdin.read())) {
data += chunk;
}
});
// When input is done, log it
process.stdin.on("end", () => {
// Make data a little more readable (this can be changed if the md parsing is too ugly)
data = "=====\n" + data + "\n====="
const isToLong = data.length > 2000;
// Save just the chunk that the webhook can send (2K chars) to a var
const webhookData = isToLong ? data.slice(0,1997) + "..." : data;
// Now, we'll write this error stream to a file instead
if (isToLong) {
// mm-dd-yyyy-<military time>
const dateTime = new Date().toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hour12: false
}).replaceAll("/", "-").replaceAll(":", "").replaceAll(", ", "-");
// write this error to file
fs.writeFileSync("./errorLogs/error-"+dateTime+".txt", data)
}
fetch(logWebhook, {
'method': 'POST',
'headers': {
"Content-Type": "application/json"
},
'body': JSON.stringify({
'username': "stderrLog.js",
"content": webhookData
})
}).then(re => re.text()).then(re => {
// There are only responses on errors
if (re.trim()) {
console.log("Response from stderr webhook:\n" + re)
}
});
});