From 43ba83cf729f8d2ef9d43da2e46577e9236e642c Mon Sep 17 00:00:00 2001
From: a <a@trwnh.com>
Date: Wed, 12 Mar 2025 03:16:49 +0000
Subject: [PATCH] feature: webhook notify on systemd service start/stop

---
 README.md                            | 4 ++++
 contrib/notify.sh                    | 7 +++++++
 umi@.service => contrib/umi@.service | 2 ++
 3 files changed, 13 insertions(+)
 create mode 100644 contrib/notify.sh
 rename umi@.service => contrib/umi@.service (68%)

diff --git a/README.md b/README.md
index 2ceb8e8..521c7c0 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,10 @@ Copy `.env.example` to `.env`:
   - Settings > Advanced > Developer Mode: enabled
   - Right click the guild and select "Copy ID"
 
+- `DISCORD_STATUS_WEBHOOK_URL` (optional) is used to monitor when the bot fails
+  - Channel Settings > Integrations > New Webhook (don't forget to save changes)
+  - Copy Webhook URL
+
 - `DISCORD_BOT_ADMIN_ROLE_ID` for reloading cogs
 - `DISCORD_BOT_ADMIN_USER_ID` for reloading cogs
 - `DISCORD_VCJOIN_CHANNEL` for the VCJoin cog
diff --git a/contrib/notify.sh b/contrib/notify.sh
new file mode 100644
index 0000000..9aeb7b7
--- /dev/null
+++ b/contrib/notify.sh
@@ -0,0 +1,7 @@
+#!/usr/bin/bash
+
+curl \
+        -X POST \
+        -H "Content-Type: application/json" \
+        -d '{"username": "system", "content": "'"$1"'"}' \
+        $DISCORD_STATUS_WEBHOOK_URL
diff --git a/umi@.service b/contrib/umi@.service
similarity index 68%
rename from umi@.service
rename to contrib/umi@.service
index 9bbc1b2..22abbf2 100644
--- a/umi@.service
+++ b/contrib/umi@.service
@@ -8,6 +8,8 @@ User=discord
 WorkingDirectory=/srv/discord/%I
 EnvironmentFile=/srv/discord/%I/.env
 ExecStart=/srv/discord/.venv/bin/python -u app.py
+ExecStopPost=/srv/discord/%I/notify.sh "umi instance stopped: `%i`"
+ExecStartPost=/srv/discord/%I/notify.sh "umi instance started: `%i`"
 
 [Install]
 WantedBy=multi-user.target