Compare commits
3 Commits
99281d209a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df078e2aa7 | ||
|
|
51beafed0f | ||
|
|
7005d4588d |
@@ -28,6 +28,9 @@ Create a `.env` file in the project root with your Discord bot token:
|
||||
|
||||
```
|
||||
DISCORD_BOT_TOKEN=your_discord_bot_token_here
|
||||
# Optional: Set Kuma monitoring variables:
|
||||
# KUMA_PUSH_URL=https://your-uptime-kuma-instance.com/api/push/
|
||||
# KUMA_PUSH_INTERVAL=60
|
||||
```
|
||||
|
||||
### 3. Run with Docker Compose
|
||||
|
||||
53
bot.go
53
bot.go
@@ -4,11 +4,14 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"thoughtful-dcbob/commands"
|
||||
|
||||
@@ -51,6 +54,22 @@ func main() {
|
||||
// continue running; commands may be registered later
|
||||
}
|
||||
|
||||
// Start Kuma push monitor if configured
|
||||
kumaPushURL, ok := os.LookupEnv("KUMA_PUSH_URL")
|
||||
if ok {
|
||||
intervalStr, ok := os.LookupEnv("KUMA_PUSH_INTERVAL")
|
||||
interval := 60 // default to 60 seconds
|
||||
if ok && intervalStr != "" {
|
||||
if parsed, err := strconv.Atoi(intervalStr); err == nil && parsed > 0 {
|
||||
interval = parsed
|
||||
}
|
||||
}
|
||||
go startKumaPush(kumaPushURL, interval)
|
||||
log.Printf("Kuma push monitor started (URL: %s, Interval: %ds)", kumaPushURL, interval)
|
||||
} else {
|
||||
log.Println("Kuma push monitor not configured.")
|
||||
}
|
||||
|
||||
// Wait here until CTRL-C or other term signal is received.
|
||||
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
||||
sc := make(chan os.Signal, 1)
|
||||
@@ -61,6 +80,40 @@ func main() {
|
||||
dg.Close()
|
||||
}
|
||||
|
||||
// startKumaPush periodically sends GET requests to the Kuma push URL
|
||||
func startKumaPush(url string, intervalSeconds int) {
|
||||
ticker := time.NewTicker(time.Duration(intervalSeconds) * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
pushToKuma(url)
|
||||
}
|
||||
}
|
||||
|
||||
// pushToKuma sends a GET request to the Kuma push URL with retry logic
|
||||
func pushToKuma(url string) {
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
// First attempt
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
log.Printf("Kuma push failed: %v (retrying in 15s)", err)
|
||||
time.Sleep(15 * time.Second)
|
||||
|
||||
// Retry attempt
|
||||
resp, err = client.Get(url)
|
||||
if err != nil {
|
||||
log.Printf("Kuma push retry failed: %v", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
log.Printf("Kuma push retry succeeded: status %d", resp.StatusCode)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
log.Printf("Kuma push: status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// This function will be called (due to AddHandler above) every time a new
|
||||
// message is created on any channel that the authenticated bot has access to.
|
||||
//
|
||||
|
||||
@@ -7,4 +7,6 @@ services:
|
||||
command: sh -c "git pull && if [ ! -f .env ]; then echo \".env file not found! Please create one based on .env.example and set your DISCORD_BOT_TOKEN.\"; exit 1; fi && go mod tidy && go run ."
|
||||
env_file:
|
||||
- .env
|
||||
|
||||
# Optional: Set Kuma monitoring variables in .env file:
|
||||
# KUMA_PUSH_URL=https://your-uptime-kuma-instance.com/api/push/xxxxx
|
||||
# KUMA_PUSH_INTERVAL=60
|
||||
Reference in New Issue
Block a user