-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
85 lines (83 loc) · 1.99 KB
/
lib.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
const { URL } = require("url");
const { version, name, description } = require("./package.json");
/**
* Parse argv
*/
const program = require("commander")
.version(version)
.name(name)
.description(description)
.arguments("[discord-webhook]")
.option("-l, --level <level>", "set log level pipe to webhook", "30")
.option("-m, --mentions <mentions>", "set mentions, id split with ','")
.option("-u, --username <bot>", "set bot name", "pino-discord")
.parse(process.argv);
const url = new URL(program.args[0]); // Validation
exports.cli = { url, opts: program.opts() };
/**
* Format to discord payload with options
*/
exports.makeBody = (opts, log) => {
let content = `---
time: ${log.time},
level ${log.level}
pid: ${log.pid},
hostname: ${log.hostname},
msg: ${log.msg}
---`;
if (opts.mentions) {
const names = opts.mentions.split(",").map((n) => {
const target = n.toLowerCase().trim();
if (target === "here" || target === "everyone") {
// Is Everyone
return "@" + n.trim();
} else {
// Is user or role
return "<@" + n.trim() + ">";
}
});
content = content + "\n" + names.join(", ");
}
return {
username: opts.username,
content,
allowed_mentions: {
parse: ["users"],
users: [],
},
};
};
const https = require("https");
/**
* https post helper
*
* @param {URL} url
* @param {object} body
*/
exports.post = (url, body) => {
const data = JSON.stringify(body, null, 2);
const req = https.request(
url,
{
method: "POST",
headers: {
"Content-type": "application/json",
"Content-Length": data.length,
},
},
(res) => {
let d = "";
res.on("data", (_d) => (d += _d));
if (res.statusCode >= 400) {
console.error("status:", res.statusCode);
res.on("end", () => {
console.error(d);
});
}
}
);
req.on("error", console.error);
req.on("end", () => process.exit());
req.write(data);
req.end();
};