Dont crash when records doesnt eist, also redo discord embed

This commit is contained in:
Rory& 2025-09-20 19:50:47 +02:00
parent 36fc4c4a84
commit 464ee153af

View file

@ -8,11 +8,12 @@ const { DateBuilder } = require("./dateBuilder");
const { CallStats } = require("./callStats"); const { CallStats } = require("./callStats");
const { CallRecord, Records } = require("./records"); const { CallRecord, Records } = require("./records");
const fs = require('fs').promises; 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 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 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() { function getYesterday() {
return new TimeSpan( 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 * Fetch call statistics
* @returns {Promise<CallStats>} * @returns {Promise<CallStats>}
@ -128,21 +120,27 @@ async function sendSummary() {
await records.toJSONFile(JSON_FILE); await records.toJSONFile(JSON_FILE);
const yesterday = getYesterday(); const yesterday = getYesterday();
const makeField = (name, value) => ({ name, value: value.toString(), inline: false });
let embed = { let embed = {
title: `Summary from <t:${Math.floor(yesterday.start / 1000)}:f> to <t:${Math.floor(yesterday.end / 1000)}:f>`, title: `Summary from <t:${Math.floor(yesterday.start / 1000)}:f> to <t:${Math.floor(yesterday.end / 1000)}:f>`,
color: 0x1E90FF, 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(), timestamp: new Date(),
footer: {} footer: {}
} }
for (const [ key, value ] of Object.entries(updatedData)) {
if (key === "NEW RECORD") { if (updatedData.isNewRecord) {
embed.fields.push({ name: "NEW RECORD!", value: "A new record has been set!", inline: false }); embed.color = 0xFFD700; // Gold color for new record
} else { embed.fields.push(makeField("🎉 NEW RECORD! 🎉", `A new record has been set, at ${updatedData.totalCallsMade}!`));
embed.fields.push({ name: key, value: value, inline: false });
}
} }
const payload = { embeds: [ embed ] }; const payload = { embeds: [ embed ] };
console.log("Sending Discord message:", payload); console.log("Sending Discord message:", payload);
if (hook) if (hook)