From eb7f781165169bbf72e0dcdfbd249b916be27c95 Mon Sep 17 00:00:00 2001 From: phntxx Date: Thu, 11 Mar 2021 12:28:29 +0100 Subject: [PATCH 1/2] Add support for Discord --- README.md | 5 +-- index.js | 48 ++++++++++++++++++++++----- package.json | 3 +- yarn.lock | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index fe26bcd..cdb1249 100644 --- a/README.md +++ b/README.md @@ -4,17 +4,18 @@ [![MicroBadger Size](https://img.shields.io/microbadger/image-size/hackaburg/watchdog)](https://hub.docker.com/r/hackaburg/watchdog) [![MicroBadger Layers](https://img.shields.io/microbadger/layers/hackaburg/watchdog)](https://hub.docker.com/r/hackaburg/watchdog) -If run inside a docker-compose setup, the watchdog will notify the configured Slack channel about its shutdown. +If run inside a docker-compose setup, the watchdog will notify the configured Slack channel (or Discord server) about its shutdown. ## Usage -To get the watchdog, simply pull the `hackaburg/watchdog` image from the Docker hub and pass in your Slack webhook url as an environment variable: +To get the watchdog, simply pull the `hackaburg/watchdog` image from the Docker hub and pass in your Slack (or Discord) webhook url as an environment variable: ```bash $ docker run \ --rm \ -it \ -e SLACK_WEBHOOK_URL=https://your-slack-webhook_url \ + -e DISCORD_WEBHOOK_URL=https://your-discord-webhook_url \ hackaburg/watchdog ``` diff --git a/index.js b/index.js index 4c7bd4e..fbeea53 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,54 @@ const { IncomingWebhook } = require("@slack/webhook"); -const token = process.env.SLACK_WEBHOOK_URL; -const hook = new IncomingWebhook(token); +const discord = require("discord.js"); + +const slackToken = process.env.SLACK_WEBHOOK_URL; +const discordURL = process.env.DISCORD_WEBHOOK_URL; + +const slackHook, discordHook; + +if (slackToken) { + slackHook = new IncomingWebhook(slackToken); +} + +if (discordURL) { + // Structure of discord webhook URL: https://discord.com/api/webhooks/[id]/[token] + const discordCredentials = discordURL + .replace("https://discord.com/api/webhooks/", "") + .split("/"); + + discordHook = new discord.WebhookClient( + discordCredentials[0], + discordCredentials[1] + ); +} for (const signal of ["SIGTERM", "SIGINT", "SIGUSR1", "SIGUSR2"]) { process.on(signal, async () => { console.log(`received signal ${signal}`); - await hook.send({ - text: `received signal ${signal}, about to shut down!`, - }); + if (slackToken) { + await slackHook.send({ + text: `received signal ${signal}, about to shut down!`, + }); + } + + if (discordID && discordToken) { + discordHook.send(`received signal ${signal}, about to shut down!`); + } process.exit(); }); } -hook.send({ - text: "watchdog started", -}); +if (slackToken) { + slackHook.send({ + text: "watchdog started", + }); +} + +if (discordID && discordToken) { + discordHook.send("watchdog started"); +} function wake() { setTimeout(wake, 60 * 60 * 1000); diff --git a/package.json b/package.json index 83adbed..fe4e148 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "license": "MIT", "private": true, "dependencies": { - "@slack/webhook": "^5.0.1" + "@slack/webhook": "^5.0.1", + "discord.js": "^12.5.1" }, "scripts": { "start": "node index.js" diff --git a/yarn.lock b/yarn.lock index 352854c..dfd0972 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,20 @@ # yarn lockfile v1 +"@discordjs/collection@^0.1.6": + version "0.1.6" + resolved "https://registry.yarnpkg.com/@discordjs/collection/-/collection-0.1.6.tgz#9e9a7637f4e4e0688fd8b2b5c63133c91607682c" + integrity sha512-utRNxnd9kSS2qhyivo9lMlt5qgAUasH2gb7BEOn6p0efFh24gjGomHzWKMAPn2hEReOPQZCJaRKoURwRotKucQ== + +"@discordjs/form-data@^3.0.1": + version "3.0.1" + resolved "https://registry.yarnpkg.com/@discordjs/form-data/-/form-data-3.0.1.tgz#5c9e6be992e2e57d0dfa0e39979a850225fb4697" + integrity sha512-ZfFsbgEXW71Rw/6EtBdrP5VxBJy4dthyC0tpQKGKmYFImlmmrykO14Za+BiIVduwjte0jXEBlhSKf0MWbFp9Eg== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + "@slack/types@^1.1.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@slack/types/-/types-1.1.0.tgz#64465b6c7e2db66498f335934335da81c15812e7" @@ -21,6 +35,18 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.1.tgz#3b5c3a26393c19b400844ac422bd0f631a94d69d" integrity sha512-aK9jxMypeSrhiYofWWBf/T7O+KwaiAHzM4sveCdWPn71lzUSMimRnKzhXDKfKwV1kWoBo2P1aGgaIYGLf9/ljw== +abort-controller@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392" + integrity sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg== + dependencies: + event-target-shim "^5.0.0" + +asynckit@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + integrity sha1-x57Zf380y48robyXkLzDZkdLS3k= + axios@^0.18.0: version "0.18.1" resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.1.tgz#ff3f0de2e7b5d180e757ad98000f1081b87bcea3" @@ -29,6 +55,13 @@ axios@^0.18.0: follow-redirects "1.5.10" is-buffer "^2.0.2" +combined-stream@^1.0.8: + version "1.0.8" + resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" + integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== + dependencies: + delayed-stream "~1.0.0" + debug@=3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" @@ -36,6 +69,30 @@ debug@=3.1.0: dependencies: ms "2.0.0" +delayed-stream@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk= + +discord.js@^12.5.1: + version "12.5.1" + resolved "https://registry.yarnpkg.com/discord.js/-/discord.js-12.5.1.tgz#992b45753e3815526a279914ccc281d3496f5990" + integrity sha512-VwZkVaUAIOB9mKdca0I5MefPMTQJTNg0qdgi1huF3iwsFwJ0L5s/Y69AQe+iPmjuV6j9rtKoG0Ta0n9vgEIL6w== + dependencies: + "@discordjs/collection" "^0.1.6" + "@discordjs/form-data" "^3.0.1" + abort-controller "^3.0.0" + node-fetch "^2.6.1" + prism-media "^1.2.2" + setimmediate "^1.0.5" + tweetnacl "^1.0.3" + ws "^7.3.1" + +event-target-shim@^5.0.0: + version "5.0.1" + resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789" + integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ== + follow-redirects@1.5.10: version "1.5.10" resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a" @@ -48,7 +105,44 @@ is-buffer@^2.0.2: resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.3.tgz#4ecf3fcf749cbd1e472689e109ac66261a25e725" integrity sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw== +mime-db@1.46.0: + version "1.46.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.46.0.tgz#6267748a7f799594de3cbc8cde91def349661cee" + integrity sha512-svXaP8UQRZ5K7or+ZmfNhg2xX3yKDMUzqadsSqi4NCH/KomcH75MAMYAGVlvXn4+b/xOPhS3I2uHKRUzvjY7BQ== + +mime-types@^2.1.12: + version "2.1.29" + resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.29.tgz#1d4ab77da64b91f5f72489df29236563754bb1b2" + integrity sha512-Y/jMt/S5sR9OaqteJtslsFZKWOIIqMACsJSiHghlCAyhf7jfVYjKBmLiX8OgpWeW+fjJ2b+Az69aPFPkUOY6xQ== + dependencies: + mime-db "1.46.0" + ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g= + +node-fetch@^2.6.1: + version "2.6.1" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" + integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw== + +prism-media@^1.2.2: + version "1.2.7" + resolved "https://registry.yarnpkg.com/prism-media/-/prism-media-1.2.7.tgz#697c9630e2a473a5dfab19c71eba398a083c2bf0" + integrity sha512-thS1z3L6BDmf724sqLC73bHGjSYArFTYHa7cqInyS3EdDNTHKgDCXy7l+IhRvlnX7aFNiUb8jJcC+R8ezxwgMA== + +setimmediate@^1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285" + integrity sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU= + +tweetnacl@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.3.tgz#ac0af71680458d8a6378d0d0d050ab1407d35596" + integrity sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw== + +ws@^7.3.1: + version "7.4.4" + resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.4.tgz#383bc9742cb202292c9077ceab6f6047b17f2d59" + integrity sha512-Qm8k8ojNQIMx7S+Zp8u/uHOx7Qazv3Yv4q68MiWWWOJhiwG5W3x7iqmRtJo8xxrciZUY4vRxUTJCKuRnF28ZZw== From 74dd6906b955215668cb5aa868fdf22396bf9ba5 Mon Sep 17 00:00:00 2001 From: phntxx Date: Thu, 11 Mar 2021 13:03:01 +0100 Subject: [PATCH 2/2] Update index.js --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index fbeea53..8ce717b 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ const { IncomingWebhook } = require("@slack/webhook"); const discord = require("discord.js"); const slackToken = process.env.SLACK_WEBHOOK_URL; -const discordURL = process.env.DISCORD_WEBHOOK_URL; +const discordToken = process.env.DISCORD_WEBHOOK_URL; const slackHook, discordHook; @@ -10,9 +10,9 @@ if (slackToken) { slackHook = new IncomingWebhook(slackToken); } -if (discordURL) { +if (discordToken) { // Structure of discord webhook URL: https://discord.com/api/webhooks/[id]/[token] - const discordCredentials = discordURL + const discordCredentials = discordToken .replace("https://discord.com/api/webhooks/", "") .split("/"); @@ -32,7 +32,7 @@ for (const signal of ["SIGTERM", "SIGINT", "SIGUSR1", "SIGUSR2"]) { }); } - if (discordID && discordToken) { + if (discordToken) { discordHook.send(`received signal ${signal}, about to shut down!`); } @@ -46,7 +46,7 @@ if (slackToken) { }); } -if (discordID && discordToken) { +if (discordToken) { discordHook.send("watchdog started"); }