-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (61 loc) · 1.48 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
package main
import (
"os"
"os/signal"
"time"
"github.com/Asthetic/DiscordGameServerBot/config"
"github.com/Asthetic/DiscordGameServerBot/discord"
"github.com/Asthetic/DiscordGameServerBot/network"
"github.com/Asthetic/DiscordGameServerBot/storage"
log "github.com/sirupsen/logrus"
)
func main() {
config, err := config.NewConfig()
if err != nil {
log.Error(err, "unable to read configuration file")
return
}
discordBot, err := discord.New(config.DiscordCfg)
if err != nil {
log.WithError(err).Error("Failed to initialize discord session")
return
}
defer discordBot.Close()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop()
done := make(chan bool)
for {
getIP(discordBot)
select {
case <-done:
return
case s := <-sig:
log.Infof("Got signal to kill program: %v", s)
return
case <-ticker.C:
getIP(discordBot)
}
}
}
func getIP(discord *discord.Discord) {
currentIP, err := storage.GetIP()
if err != nil {
log.WithError(err).Errorf("Error fetching file: %v", err)
}
ip, err := network.GetPublicIP()
if err != nil {
log.WithError(err).Errorf("Error getting IP address: %v")
return
}
log.Infof("Sucessfully got public IP address: %v", ip)
if currentIP != ip {
currentIP = ip
err = storage.WriteIP(network.Network{IP: currentIP})
if err != nil {
log.WithError(err).Errorf("Error writing IP to local storage")
}
discord.SendUpdatedIP(ip)
}
}