From cb309e8850bbbdb78625f2d561ad6c8c94476c0b Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Mon, 28 Oct 2024 22:53:26 -0600 Subject: [PATCH] buh --- index.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 2e63597..1a05376 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,16 @@ const hook = new Discord.WebhookClient({ url: process.env.DISCORD_WEBHOOK_URL }) const JSON_FILE = process.env.JSON_FILE || "records.json"; + +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 +} + async function loadRecords() { try { const data = await fs.readFile(JSON_FILE, 'utf-8'); @@ -28,7 +38,7 @@ async function saveRecords(root) { async function getPreviousDayData() { return new Promise(async (resolve, reject) => { - const previousDay = new Date(Date.now() - 86400000); // 24 hours ago + const previousDay = new Date(getStartOfYesterdayTimestamp()) // 24 hours ago const startTime = new Date(previousDay.setHours(0, 0, 0, 0)); const endTime = new Date(previousDay.setHours(23, 59, 59, 999)); const connection = await mysql.createConnection({ @@ -75,7 +85,7 @@ function getSystemUptime() { } function updateRecords(data, root) { - const currentDate = new Date(Date.now() - 86400000).toISOString().split('T')[0]; + const currentDate = new Date(getStartOfYesterdayTimestamp()).toISOString().split('T')[0]; const month = currentDate.slice(0, 7); let isNewRecord = false; @@ -102,18 +112,13 @@ function updateRecords(data, root) { return data; } -function getStartOfDayTimestamp(date) { - const startOfDay = new Date(date.getFullYear(), date.getMonth(), date.getDate()); - return startOfDay.getTime(); // Returns the timestamp in milliseconds -} - async function sendSummary() { const data = await getPreviousDayData(); const root = await loadRecords(); const updatedData = await updateRecords(data, root); await saveRecords(root); - const previousDayStart = new Date(getStartOfDayTimestamp(new Date())); + const previousDayStart = new Date(getStartOfYesterdayTimestamp()); const previousDayEnd = new Date(previousDayStart); previousDayEnd.setHours(23, 59, 59, 999);