forked from steezeburger/palguybuddydude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (108 loc) · 2.94 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
type Player struct {
Name string `json:"name"`
PlayerUID string `json:"playeruid"`
SteamID string `json:"steamid"`
}
type ServerInfo struct {
Players []Player `json:"players"`
ServerName string `json:"serverName"`
ServerVersion string `json:"serverVersion"`
}
// fetchServerInfo makes an HTTP GET request to the server info API
func fetchServerInfo(apiURL string) (ServerInfo, error) {
resp, err := http.Get(apiURL)
if err != nil {
return ServerInfo{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ServerInfo{}, err
}
var info ServerInfo
err = json.Unmarshal(body, &info)
if err != nil {
return ServerInfo{}, err
}
return info, nil
}
// notifyDiscord sends a message to a Discord webhook
func notifyDiscord(webhookURL string, message string) error {
payload := map[string]string{"content": message}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return err
}
_, err = http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonPayload))
return err
}
func main() {
baseURL := os.Getenv("RCON_BUDDY_HOST")
apiURL := fmt.Sprintf("http://%s/info", baseURL)
webhookURL := os.Getenv("DISCORD_WEBHOOK_URL")
log.Printf("Starting palguybuddydude!")
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
previousPlayers := make(map[string]Player)
initialFetch := true
for {
select {
case <-ticker.C:
info, err := fetchServerInfo(apiURL)
if err != nil {
log.Printf("Failed to fetch server info: %v", err)
continue
}
if initialFetch {
log.Printf("Initial server info fetched: %+v", info)
}
log.Printf("Current players: %+v", info.Players)
currentPlayers := make(map[string]Player)
for _, player := range info.Players {
// NOTE - i think this fixes the issue where there's a duplicate user
if player.PlayerUID == "00000000" {
log.Printf("Skipping fake duplicate player: %+v", player)
continue
}
currentPlayers[player.PlayerUID] = player
if !initialFetch {
if _, exists := previousPlayers[player.PlayerUID]; !exists {
message := fmt.Sprintf("Player joined: %s", player.Name)
if err := notifyDiscord(webhookURL, message); err != nil {
log.Printf("Failed to send Discord notification: %v", err)
}
}
}
}
if !initialFetch {
for uid, player := range previousPlayers {
if player.PlayerUID == "00000000" {
log.Printf("Skipping fake duplicate user: %+v", player)
continue
}
if _, exists := currentPlayers[uid]; !exists {
message := fmt.Sprintf("Player left: %s", player.Name)
if err := notifyDiscord(webhookURL, message); err != nil {
log.Printf("Failed to send Discord notification: %v", err)
}
}
}
}
previousPlayers = currentPlayers
if initialFetch {
initialFetch = false
}
}
}
}