kuma-discord/index.js

121 lines
3.9 KiB
JavaScript

require("dotenv").config()
const uptimekuma = require("uptimekuma-api")
const kuma = new uptimekuma(process.env.KUMA_BASE_URL);
const subs = require("./subs.json")
const Discord = require("discord.js")
const Client = new Discord.Client({
intents: [
"Guilds",
"GuildMessages"
]
})
const cron = require("node-cron")
function getStatus(input) {
return new Promise((resolve, reject) => {
kuma.status(input).then((status) => {
let total = 0;
let offline = 0;
let embed = {
title: status.info.title,
thumbnail: {
url: status.info.icon
},
fields: []
}
// for (cat of status.status) {
// let fieldText = "";
// for (monitor of cat.monitors) {
// if (!monitor.heartbeats[0].status) offline++;
// total++;
// fieldText += `${monitor.heartbeats[0].status ? "<:online:1307359583785713744>" : "<:offline:1307359600592424971>"} ${monitor.name}\n`
// }
// embed.fields.push({
// name: cat.name,
// value: fieldText
// })
// }
// let statusDesc = "<:online:1307359583785713744> All Systems Operational"
// if (offline > 0 && offline !== total) {
// statusDesc = "<:degraded:1307359619491954778> Degraded Service"
// } else if(offline === total) {
// statusDesc = "<:Offline:1307359600592424971> Full Outage"
// }
// embed.description = `${status.info.description}\n\n${statusDesc}`
for (cat of status.status) {
let fieldText = "";
let fieldParts = [];
for (monitor of cat.monitors) {
const isOnline = Boolean(monitor.heartbeats[0].status); // Use Boolean() for clarity
console.log(`Monitor: ${monitor.name}, Status: ${isOnline}`);
const monitorText = `${isOnline ? "<:online:1307359583785713744>" : "<:offline:1307359600592424971>"} ${monitor.name}\n`;
// If the monitor is offline, increment the offline count
if (!isOnline) offline++;
total++;
// Check if adding this monitor's text will exceed 1024 characters
if (fieldText.length + monitorText.length > 1024) {
// Push the current field text and start a new field
fieldParts.push(fieldText.trim());
fieldText = monitorText; // Start new field with this monitor's text
} else {
// Otherwise, just add the monitor's text to the current field
fieldText += monitorText;
}
}
// Push any remaining text in fieldText after processing all monitors
if (fieldText.trim()) {
fieldParts.push(fieldText.trim());
}
// Now, add each part as a separate field
for (let i = 0; i < fieldParts.length; i++) {
embed.fields.push({
name: i === 0 ? cat.name : `More ${cat.name}`,
value: fieldParts[i]
});
}
}
let statusDesc = "<:online:1307359583785713744> All Systems Operational";
embed.color = 0x43b581
if (offline > 0 && offline !== total) {
statusDesc = "<:degraded:1307359619491954778> Degraded Service";
embed.color = 0xfaa61a
} else if(offline === total) {
statusDesc = "<:Offline:1307359600592424971> Full Outage";
embed.color = 0xf04747
}
embed.description = `${status.info.description !== null ? `${status.info.description}\n\n` : ""}${statusDesc}`;
resolve(embed)
})
})
}
Client.on('ready', async () => {
console.log(`Logged in as ${Client.user.displayName}`);
for (const channelId in subs) {
const channel = await Client.channels.fetch(channelId)
messages = await channel.messages.fetch({ limit: 10 })
var message = messages.find(msg => msg.author.id === Client.user.id);
if (!message) message = await channel.send({embeds:[{description:"Loading..."}]});
getStatus(subs[channelId]).then((status) => {
status.timestamp = new Date()
message.edit({embeds:[status]})
})
cron.schedule("*/5 * * * *", () => {
getStatus(subs[channelId]).then((status) => {
status.timestamp = new Date()
message.edit({embeds:[status]})
})
})
}
});
Client.login(process.env.TOKEN)