From 241679cc7f9ddd165f75cea4a760e5e220560f9a Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 10 Aug 2023 22:12:14 -0400 Subject: [PATCH] Add heartbeat url for uptime monitoring --- functions.js | 13 +++++++++++++ main.js | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/functions.js b/functions.js index 3960dda..7b25d71 100644 --- a/functions.js +++ b/functions.js @@ -670,6 +670,19 @@ const functions = { } const errorId = digits.join(""); return errorId; + }, + async sendHeartbeat(url) { + console.log(url); + https.get(url, async (response) => { + let data = ''; + + response.on('data', (chunk) => data += chunk); + + response.on('end', () => { + parsedData = JSON.parse(data); + if ( !(parsedData.ok) ) console.error("Heartbeat failed"); + }); + }).on("error", (error) => console.error(error)); } }; diff --git a/main.js b/main.js index b959c78..716a727 100644 --- a/main.js +++ b/main.js @@ -5,6 +5,8 @@ const dotenv = require('dotenv'); dotenv.config(); const token = process.env.TOKEN; const statusChannelId = process.env.statusChannelId; +const heartbeatUrl = process.env.HEARTBEAT_URL; +const sendHeartbeat = typeof heartbeatUrl === 'string'; // Discord.JS const { Client, Intents } = require('discord.js'); @@ -44,6 +46,13 @@ client.once('ready', async () => { await client.channels.fetch(statusChannelId).then(channel => { channel.send(`${new Date().toISOString()} -- <@${process.env.ownerId}>\nStartup Sequence Complete`); }); + // Heartbeat Timer + if (sendHeartbeat) { + setInterval(() => { + fn.sendHeartbeat(heartbeatUrl); + }, 30000); + if (isDev) console.log("Heartbeat interval set."); + } }); // slash-commands