Compare commits

...

2 Commits

Author SHA1 Message Date
35e27546b7 Started working on checkers... 2022-05-28 15:03:43 +02:00
2fe077592a More verbosity... 2022-05-27 14:55:32 +02:00
8 changed files with 84 additions and 9 deletions

3
.gitignore vendored
View File

@ -132,4 +132,5 @@ dist
# Hihi
config.yml
pnpm-lock.yaml
pnpm-lock.yaml
ip.cache

View File

@ -1,13 +1,39 @@
import settings from "./libs/settings.js"
import getNotifier from "./libs/getNotifier.js"
import getLogger from "./libs/logger.js"
import getChecker from "./libs/getChecker.js"
const log = getLogger("MAIN", "blue")
log("Starting up maintenance-bot...")
log("Starting up notifiers...")
const enabledNotifiers = Object.keys(settings.notifiers).filter(notifier => settings.notifiers[notifier].enabled)
const notifiers = await Promise.all(enabledNotifiers.map(notifier => getNotifier(notifier)))
notifiers.forEach(notifier => notifier("Started up 'maintenance-bot'..."))
log("Finding enabled notifiers and checkers...")
const enabledNotifiers = Object.keys(settings.notifiers).filter(notifier => settings.notifiers[notifier].enabled)
const enabledCheckers = Object.keys(settings.checkers).filter(checker => settings.checkers[checker].enabled)
log("Enabled notifiers:", enabledNotifiers)
log("Enabled checkers: ", enabledCheckers)
log("Starting up notifiers and checkers...")
const notifiers = await Promise.all(enabledNotifiers.map(notifier => getNotifier(notifier)))
const sendMessage = msg => notifiers.forEach(notifier => notifier(msg))
await Promise.all(enabledCheckers.map(checker => getChecker(checker, sendMessage)))
log("Started all checkers!")
log("Sending out start-up messages...")
sendMessage("Started up 'maintenance-bot'...")
log("Start up complete!")
function onExitWarn() {
sendMessage("OH NO I AM DYING!")
setTimeout(() => process.exit(0), 200)
}
process.on("beforeExit", onExitWarn)
process.on("exit", onExitWarn)
process.on("SIGINT", onExitWarn)
process.on("SIGTERM", onExitWarn)
process.on("uncaughtException", onExitWarn)
process.on("unhandledRejection", onExitWarn)

5
libs/getChecker.js Normal file
View File

@ -0,0 +1,5 @@
export default function getChecker(checker, sendMessage) {
return new Promise(
resolve => import(`../modules/checkers/${checker}.js`).then(
module => resolve(module.start(sendMessage))))
}

View File

@ -0,0 +1,9 @@
import getLogger from "../../libs/logger.js"
import settings from "../../libs/settings.js"
const log = getLogger("Health Checker", settings.checkers["health-checker"].color)
export function start() {
log("Starting up the health checker...")
}

View File

@ -0,0 +1,33 @@
import getLogger from "../../libs/logger.js"
import settings from "../../libs/settings.js"
import fs from "fs"
import publicIp from "public-ip"
if (!fs.existsSync("./ip.cache")) fs.writeFileSync("./ip.cache", "")
const log = getLogger("IP Watcher", settings.checkers["ip-changed"].color)
const getIp = async () => await publicIp.v4({ onlyHttps: true })
const currentIp = await getIp()
const cachedOldIp = fs.readFileSync("./ip.cache").toString()
let oldIp = cachedOldIp ? oldIp : currentIp
async function checkIp() {
log("Checking for IP changes...")
const nowIp = await getIp()
if (nowIp !== oldIp) {
sendMessage(`IP change detected! Old IP was '${oldIp}', new IP is '${nowIp}'.`)
oldIp = nowIp
fs.writeFileSync("./ip.cache", nowIp)
return
}
log("No IP change detected!")
}
export async function start(sendMessage) {
log("Starting up the IP watcher...")
checkIp()
setInterval(checkIp, (settings.checkers["ip-changed"].interval ?? 3600) * 1000)
return
}

View File

@ -11,7 +11,7 @@ export async function start() {
async function sendMessage(msg) {
const receivers = await Promise.all(settings.notifiers.discord.receivers.map(id => client.users.fetch(id)))
log("Sending messages to:", receivers.map(e => e.tag))
log("Sending messages to:", receivers.map(e => e.tag), "\nMessage:", msg)
receivers.forEach(async receiver => {
await receiver.createDM()
await receiver.send(msg)

View File

@ -1,6 +1,5 @@
import settings from "../../libs/settings.js"
import { client, jid, xml } from "@xmpp/client"
import debug from "@xmpp/debug"
import { client, xml } from "@xmpp/client"
import getLogger from "../../libs/logger.js"
const log = getLogger("XMPP", settings.notifiers.xmpp.color)
@ -19,7 +18,7 @@ export async function start() {
async function sendMessage(msg) {
const receivers = settings.notifiers.xmpp.receivers
log("Sending messages to:", receivers)
log("Sending messages to:", receivers, "\nMessage:", msg)
const stanzas = receivers.map(
address => xml(
"message",
@ -36,6 +35,7 @@ export async function start() {
}
xmpp.on("online", address => {
xmpp.send(xml("presence", { type: "available" }))
log(`Logged in as ${address.toString()}`)
})

View File

@ -18,6 +18,7 @@
"@xmpp/debug": "^0.13.0",
"colors": "^1.4.0",
"discord.js": "^13.7.0",
"public-ip": "^5.0.0",
"yaml": "^2.1.0"
}
}