diff --git a/index.js b/index.js index e1b6ddd..4c2ff8b 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,8 @@ const config = require("./config.json"); const funcs = require("./funcs.js"); const colors = require("colors"); const embeds = require("./embeds.json") +const axios = require('axios'); +const ping = require("ping") // FreePBX GraphQL Client const { FreepbxGqlClient, @@ -670,7 +672,35 @@ dcClient.on('ready', async () => { }); + // Uptime Kuma Ping + // Calculate ping to Discord API + // Every X seconds (defined in config.status.interval), send a ping to Uptime Kuma, send push request to config.status.url + setInterval(() => { + // Send a ping to Uptime Kuma + // Send a push request to config.status.url + // Define URL arguments ?status=up&msg=OK&ping= + + // Calculate ping to Discord API + const start = Date.now(); + axios.get("https://discord.com/api/gateway").then((result) => { + const latency = Date.now() - start; + axios.get(config.status.url + `?status=up&msg=OK&ping=${latency}`).then((result) => { + //sendLog(`${colors.cyan("[INFO]")} Sent ping to Uptime Kuma`); + }).catch((error) => { + sendLog(`${colors.red("[ERROR]")} Error sending ping ${error}`); + }); + }) + }, config.status.interval * 1000); + const start = Date.now(); + axios.get("https://discord.com/api/gateway").then((result) => { + const latency = Date.now() - start; + axios.get(config.status.url + `?status=up&msg=OK&ping=${latency}`).then((result) => { + //sendLog(`${colors.cyan("[INFO]")} Sent ping to Uptime Kuma`); + }).catch((error) => { + sendLog(`${colors.red("[ERROR]")} Error sending ping ${error}`); + }); + }) }); dcClient.on("guildMemberRemove", (member) => { @@ -1282,5 +1312,35 @@ dcClient.on('interactionCreate', async interaction => { } }); +// Lets actually handle exceptions now +process.on('unhandledRejection', (error) => { + // Log a full error with line number + sendLog(`${colors.red("[ERROR]")} ${error}`); + // If config.ntfyUrl is set, Send the exception to ntfy + if (config.ntfyUrl) fetch(config.ntfyUrl, { + method: 'POST', // PUT works too + body: error, + headers: { + 'Title': 'FreePBX Bot Rejection', + 'Priority': 5, + 'Tags': 'warning,phone,FreePBX Manager' + } + }); +}); + +process.on('uncaughtException', (error) => { + // Log a full error with line number + sendLog(`${colors.red("[ERROR]")} ${error}`); + // If config.ntfyUrl is set, Send the exception to ntfy + if (config.ntfyUrl) fetch(config.ntfyUrl, { + method: 'POST', // PUT works too + body: error, + headers: { + 'Title': 'FreePBX Bot Exception', + 'Priority': 5, + 'Tags': 'warning,phone,FreePBX Manager' + } + }); +}); dcClient.login(config.discord.token); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index f43f12a..beae983 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,10 +9,12 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "axios": "^1.6.0", "colors": "^1.4.0", "discord.js": "^14.7.1", "freepbx-graphql-client": "^0.1.1", "mariadb": "^3.2.0", + "ping": "^0.4.4", "sqlite3": "^5.1.4" } }, @@ -285,6 +287,29 @@ "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, + "node_modules/axios": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.0.tgz", + "integrity": "sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/axios/node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -520,6 +545,25 @@ "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" }, + "node_modules/follow-redirects": { + "version": "1.15.3", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", + "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/form-data": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", @@ -1149,6 +1193,14 @@ "node": ">=0.10.0" } }, + "node_modules/ping": { + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/ping/-/ping-0.4.4.tgz", + "integrity": "sha512-56ZMC0j7SCsMMLdOoUg12VZCfj/+ZO+yfOSjaNCRrmZZr6GLbN2X/Ui56T15dI8NhiHckaw5X2pvyfAomanwqQ==", + "engines": { + "node": ">=4.0.0" + } + }, "node_modules/promise-inflight": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/promise-inflight/-/promise-inflight-1.0.1.tgz", @@ -1168,6 +1220,11 @@ "node": ">=10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/readable-stream": { "version": "3.6.2", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", diff --git a/package.json b/package.json index ace8544..5212796 100644 --- a/package.json +++ b/package.json @@ -9,10 +9,12 @@ "author": "", "license": "ISC", "dependencies": { + "axios": "^1.6.0", "colors": "^1.4.0", "discord.js": "^14.7.1", "freepbx-graphql-client": "^0.1.1", "mariadb": "^3.2.0", + "ping": "^0.4.4", "sqlite3": "^5.1.4" } }