paper-dynasty-discord/notify_restart.py
Cal Corum eb2a511507 Update restart notification message to be more accurate
Changed from 'Auto-Restarted' to 'Restarted' and made the message
generic since the bot restarts for multiple reasons (manual, deployment,
healthcheck) - not just healthcheck failures.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-03 23:29:51 -06:00

58 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""
Notify admin channel when bot restarts due to healthcheck failure.
Uses Discord webhook for instant notification.
"""
import os
import urllib.request
import json
from datetime import datetime
def send_restart_notification():
"""Send notification to Discord via webhook."""
webhook_url = os.getenv("RESTART_WEBHOOK_URL")
if not webhook_url:
print("No RESTART_WEBHOOK_URL configured, skipping notification")
return False
try:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S CST")
data = {
"content": (
f"**Paper Dynasty Bot Restarted** at {timestamp}\n\n"
"Bot has successfully connected to Discord.\n\n"
"Check `/logs/discord.log` for startup details."
),
"username": "Paper Dynasty Monitor",
}
req = urllib.request.Request(
webhook_url,
data=json.dumps(data).encode("utf-8"),
headers={
"Content-Type": "application/json",
"User-Agent": "Paper-Dynasty-Discord-Bot/1.0",
},
)
response = urllib.request.urlopen(req, timeout=5)
if response.status == 204:
print(f"Restart notification sent successfully at {timestamp}")
return True
else:
print(f"Webhook returned status {response.status}")
return False
except Exception as e:
print(f"Failed to send restart notification: {e}")
return False
if __name__ == "__main__":
send_restart_notification()