Started working on checkers...

This commit is contained in:
BurnyLlama 2022-05-28 15:03:43 +02:00
parent 2fe077592a
commit 35e27546b7
7 changed files with 79 additions and 9 deletions

3
.gitignore vendored
View File

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

View File

@ -1,18 +1,39 @@
import settings from "./libs/settings.js" import settings from "./libs/settings.js"
import getNotifier from "./libs/getNotifier.js" import getNotifier from "./libs/getNotifier.js"
import getLogger from "./libs/logger.js" import getLogger from "./libs/logger.js"
import getChecker from "./libs/getChecker.js"
const log = getLogger("MAIN", "blue") const log = getLogger("MAIN", "blue")
log("Starting up maintenance-bot...") 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)))
log("Starting up checkers...") 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...") log("Sending out start-up messages...")
notifiers.forEach(notifier => notifier("Started up 'maintenance-bot'...")) sendMessage("Started up 'maintenance-bot'...")
log("Start up complete!")
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

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

View File

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