Mmmmm Built-in cron

This commit is contained in:
Christopher Cookman 2024-10-29 07:11:47 -06:00
parent dd01233df9
commit 5652d9aa26
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42
3 changed files with 31 additions and 6 deletions

View file

@ -1,4 +1,5 @@
require("dotenv").config() require("dotenv").config()
const cron = require("node-cron");
const os = require("os") const os = require("os")
const Discord = require('discord.js'); const Discord = require('discord.js');
const { connect } = require("http2"); const { connect } = require("http2");
@ -9,7 +10,6 @@ const hook = new Discord.WebhookClient({ url: process.env.DISCORD_WEBHOOK_URL })
const JSON_FILE = process.env.JSON_FILE || "records.json"; const JSON_FILE = process.env.JSON_FILE || "records.json";
function getStartOfYesterdayTimestamp() { function getStartOfYesterdayTimestamp() {
const today = new Date(); const today = new Date();
// Set the date to yesterday // Set the date to yesterday
@ -113,9 +113,13 @@ function updateRecords(data, root) {
} }
async function sendSummary() { async function sendSummary() {
console.log("Preparing summary.")
const data = await getPreviousDayData(); const data = await getPreviousDayData();
console.log("Loading records.")
const root = await loadRecords(); const root = await loadRecords();
console.log("Updating records...")
const updatedData = await updateRecords(data, root); const updatedData = await updateRecords(data, root);
console.log("Saving.")
await saveRecords(root); await saveRecords(root);
const previousDayStart = new Date(getStartOfYesterdayTimestamp()); const previousDayStart = new Date(getStartOfYesterdayTimestamp());
@ -139,8 +143,8 @@ async function sendSummary() {
embed.fields.push({ name: key, value: value, inline: false }); embed.fields.push({ name: key, value: value, inline: false });
} }
} }
console.log("Sending message.")
await hook.send({ embeds: [embed] }); await hook.send({ embeds: [embed] });
} }
console.log("Initializing...")
sendSummary() const schedule = cron.schedule("0 1 * * *", sendSummary)

22
package-lock.json generated
View file

@ -11,7 +11,8 @@
"dependencies": { "dependencies": {
"discord.js": "^14.16.3", "discord.js": "^14.16.3",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"mysql": "^2.18.1" "mysql": "^2.18.1",
"node-cron": "^3.0.3"
} }
}, },
"node_modules/@discordjs/builders": { "node_modules/@discordjs/builders": {
@ -306,6 +307,17 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/node-cron": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz",
"integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==",
"dependencies": {
"uuid": "8.3.2"
},
"engines": {
"node": ">=6.0.0"
}
},
"node_modules/process-nextick-args": { "node_modules/process-nextick-args": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
@ -374,6 +386,14 @@
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="
}, },
"node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/ws": { "node_modules/ws": {
"version": "8.18.0", "version": "8.18.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz",

View file

@ -11,6 +11,7 @@
"dependencies": { "dependencies": {
"discord.js": "^14.16.3", "discord.js": "^14.16.3",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"mysql": "^2.18.1" "mysql": "^2.18.1",
"node-cron": "^3.0.3"
} }
} }