-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (135 loc) · 3.85 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
const (
ipCheckURL = "https://api.ipify.org" // Service to get external IP
checkInterval = 1 * time.Hour // Interval between IP checks
)
var (
previousIP string
)
func main() {
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
log.Println("No .env file found or error reading .env file")
}
// Now you can use os.Getenv to get the variables
botToken := os.Getenv("DISCORD_BOT_TOKEN")
channelID := os.Getenv("DISCORD_CHANNEL_ID")
if botToken == "" || channelID == "" {
log.Fatal("Bot token or channel ID not set. Please set DISCORD_BOT_TOKEN and DISCORD_CHANNEL_ID.")
}
// Create a new Discord session
dg, err := discordgo.New("Bot " + botToken)
if err != nil {
log.Fatalf("Error creating Discord session: %v", err)
}
// Open a WebSocket connection to Discord
err = dg.Open()
if err != nil {
log.Fatalf("Error opening connection to Discord: %v", err)
}
defer func(dg *discordgo.Session) {
err := dg.Close()
if err != nil {
log.Fatalf("Error closing connection to Discord: %v", err)
}
}(dg)
log.Println("Bot is now running. Press CTRL+C to exit.")
// Handle graceful shutdown
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
// Start the IP monitoring in a goroutine
go func() {
for {
currentIP, err := getExternalIP()
if err != nil {
log.Printf("Error fetching external IP: %v", err)
time.Sleep(checkInterval)
continue
}
if currentIP != previousIP {
log.Printf("IP changed from %s to %s", previousIP, currentIP)
err = updateDiscordMessage(dg, channelID, currentIP)
if err != nil {
log.Printf("Error updating Discord message: %v", err)
}
previousIP = currentIP
}
time.Sleep(checkInterval)
}
}()
// Wait for a termination signal
<-stop
log.Println("Shutting down bot.")
}
func getExternalIP() (string, error) {
resp, err := http.Get(ipCheckURL)
if err != nil {
return "", err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Printf("Error closing connection to IP Checker: %v", err)
}
}(resp.Body)
ip, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(ip), nil
}
func updateDiscordMessage(dg *discordgo.Session, channelID, currentIP string) error {
// Fetch pinned messages
pinnedMessages, err := dg.ChannelMessagesPinned(channelID)
if err != nil {
return fmt.Errorf("error fetching pinned messages: %w", err)
}
var pinnedMessageID string
if len(pinnedMessages) > 0 {
for _, message := range pinnedMessages {
// Assume the first pinned message is the one to update
if strings.Contains(message.Content, "Current IP Address:") {
pinnedMessageID = message.ID
_, err = dg.ChannelMessageEdit(channelID, pinnedMessageID, fmt.Sprintf("Current IP Address: `%s`", currentIP))
if err != nil {
return fmt.Errorf("error editing pinned message: %w", err)
}
// Send a broadcast message
_, err = dg.ChannelMessageSend(channelID, fmt.Sprintf("IP Address has changed to `%s`", currentIP))
if err != nil {
return fmt.Errorf("error sending broadcast message: %w", err)
}
return nil
}
}
}
// Send a new message and pin it
msg, err := dg.ChannelMessageSend(channelID, fmt.Sprintf("Current IP Address: `%s`", currentIP))
if err != nil {
return fmt.Errorf("error sending message: %w", err)
}
err = dg.ChannelMessagePin(channelID, msg.ID)
if err != nil {
return fmt.Errorf("error pinning message: %w", err)
}
// Send a broadcast message
_, err = dg.ChannelMessageSend(channelID, fmt.Sprintf("IP Address has changed to `%s`", currentIP))
if err != nil {
return fmt.Errorf("error sending broadcast message: %w", err)
}
return nil
}