Uwev2/src/inc/trigger/hetzner.mjs
2023-07-09 03:10:28 +02:00

49 lines
1.5 KiB
JavaScript

import fetch from "flumm-fetch-cookies";
import config from "../config.mjs";
const api = `https://robot-ws.your-server.de`;
const auth = {
headers: {
Authorization: `Basic ${Buffer.from(config.apis.hetzner.user + ":" + config.apis.hetzner.pwd).toString("base64")}`
}
};
export default async bot => {
return [{
name: "hz",
call: /^(\.|\/)hz/i,
f: async e => {
switch(e.args[0]) {
case "traffic":
const now = {
year: new Date().getFullYear(),
month: new Date().getMonth() + 1
};
const opts = {
...auth,
method: "POST",
body: {
type: "month",
from: `${now.year}-${now.month}-01`,
to: `${now.year}-${now.month}-32`,
"subnet[]": config.apis.hetzner.subnets,
"ip[]": config.apis.hetzner.ips
}
};
const res = await (await fetch(`${api}/traffic`, opts)).json();
const traffic = {
in: Object.keys(res.traffic.data).reduce((a, b) => a + res.traffic.data[b].in, 0).toFixed(2),
out: Object.keys(res.traffic.data).reduce((a, b) => a + res.traffic.data[b].out, 0).toFixed(2),
sum: Object.keys(res.traffic.data).reduce((a, b) => a + res.traffic.data[b].sum, 0).toFixed(2)
};
e.reply(`Traffic (${now.year}-${now.month}) in GiB: rx: ${traffic.in} tx: ${traffic.out} total: ${traffic.sum}`);
break;
default:
// prints help
break;
}
}
}];
};