Add support for sending stats to Matrix, split out some code into more generic classes. #1
32
index.js
32
index.js
|
@ -8,11 +8,12 @@ const { DateBuilder } = require("./dateBuilder");
|
|||
const { CallStats } = require("./callStats");
|
||||
const { CallRecord, Records } = require("./records");
|
||||
const fs = require('fs').promises;
|
||||
const fsSync = require('fs');
|
||||
|
||||
const hook = !!process.env.DISCORD_WEBHOOK_URL ? new Discord.WebhookClient({ url: process.env.DISCORD_WEBHOOK_URL }) : null;
|
||||
|
||||
const JSON_FILE = process.env.JSON_FILE || "records.json";
|
||||
const records = Records.fromJSONFile(JSON_FILE);
|
||||
const records = fsSync.existsSync(JSON_FILE) ? Records.fromJSONFile(JSON_FILE) : new Records();
|
||||
|
||||
function getYesterday() {
|
||||
return new TimeSpan(
|
||||
|
@ -21,15 +22,6 @@ function getYesterday() {
|
|||
);
|
||||
}
|
||||
|
||||
function getStartOfYesterdayTimestamp() {
|
||||
const today = new Date();
|
||||
// Set the date to yesterday
|
||||
today.setDate(today.getDate() - 1);
|
||||
// Create a new Date object for the start of yesterday
|
||||
const startOfYesterday = new Date(today.getFullYear(), today.getMonth(), today.getDate());
|
||||
return startOfYesterday.getTime(); // Returns the timestamp in milliseconds
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch call statistics
|
||||
* @returns {Promise<CallStats>}
|
||||
|
@ -128,21 +120,27 @@ async function sendSummary() {
|
|||
await records.toJSONFile(JSON_FILE);
|
||||
|
||||
const yesterday = getYesterday();
|
||||
const makeField = (name, value) => ({ name, value: value.toString(), inline: false });
|
||||
|
||||
let embed = {
|
||||
title: `Summary from <t:${Math.floor(yesterday.start / 1000)}:f> to <t:${Math.floor(yesterday.end / 1000)}:f>`,
|
||||
color: 0x1E90FF,
|
||||
fields: [],
|
||||
fields: [
|
||||
makeField("Calls Made", updatedData.totalCallsMade),
|
||||
makeField("Monthly Total", updatedData.totalCallsThisMonth),
|
||||
makeField("Total Calls Ever Placed", updatedData.totalCallsEverPlaced),
|
||||
makeField("System Uptime", getSystemUptime().toString(false, false)),
|
||||
makeField("All Time Record", updatedData.allTimeRecord)
|
||||
],
|
||||
timestamp: new Date(),
|
||||
footer: {}
|
||||
}
|
||||
for (const [ key, value ] of Object.entries(updatedData)) {
|
||||
if (key === "NEW RECORD") {
|
||||
embed.fields.push({ name: "NEW RECORD!", value: "A new record has been set!", inline: false });
|
||||
} else {
|
||||
embed.fields.push({ name: key, value: value, inline: false });
|
||||
}
|
||||
|
||||
if (updatedData.isNewRecord) {
|
||||
embed.color = 0xFFD700; // Gold color for new record
|
||||
embed.fields.push(makeField("🎉 NEW RECORD! 🎉", `A new record has been set, at ${updatedData.totalCallsMade}!`));
|
||||
}
|
||||
|
||||
const payload = { embeds: [ embed ] };
|
||||
console.log("Sending Discord message:", payload);
|
||||
if (hook)
|
||||
|
|
Loading…
Reference in a new issue