Add heartbeat url for uptime monitoring

This commit is contained in:
Skylar Grant 2023-08-10 22:13:08 -04:00
parent dd313faa60
commit e2658c41e5
2 changed files with 24 additions and 0 deletions

10
main.js
View File

@ -5,6 +5,8 @@ const dotenv = require('dotenv');
dotenv.config(); dotenv.config();
const token = process.env.TOKEN; const token = process.env.TOKEN;
const statusChannelId = process.env.STATUSCHANNELID; const statusChannelId = process.env.STATUSCHANNELID;
const heartbeatUrl = process.env.HEARTBEAT_URL;
const sendHeartbeat = typeof heartbeatUrl === 'string';
// Discord.JS // Discord.JS
const { Client, GatewayIntentBits, Partials, ActivityType } = require('discord.js'); const { Client, GatewayIntentBits, Partials, ActivityType } = require('discord.js');
@ -43,6 +45,14 @@ client.once('ready', async () => {
if (isDev == 'false') { if (isDev == 'false') {
statusChannel.send(`${new Date().toISOString()} -- \nStartup Sequence Complete <@481933290912350209>`); statusChannel.send(`${new Date().toISOString()} -- \nStartup Sequence Complete <@481933290912350209>`);
} }
// Heartbeat Timer
if (sendHeartbeat) {
setInterval(() => {
fn.sendHeartbeat(heartbeatUrl);
}, 30000);
if (isDev) console.log("Heartbeat interval set.");
}
}); });
// slash-commands // slash-commands

View File

@ -20,6 +20,7 @@ const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => fil
const dotCommandFiles = fs.readdirSync('./dot-commands/').filter(file => file.endsWith('.js')); const dotCommandFiles = fs.readdirSync('./dot-commands/').filter(file => file.endsWith('.js'));
const dbfn = require('./dbfn.js'); const dbfn = require('./dbfn.js');
const { finished } = require('stream'); const { finished } = require('stream');
const https = require('https');
const functions = { const functions = {
// Functions for managing and creating Collections // Functions for managing and creating Collections
@ -1142,6 +1143,19 @@ const functions = {
} }
const errorId = digits.join(""); const errorId = digits.join("");
return errorId; 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));
} }
}; };