-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (99 loc) · 2.75 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
package main
import (
"io/ioutil"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/JoshuaDoes/json"
"github.com/JoshuaDoes/logger"
)
type Config struct {
Verbosity int `json:"verbosity"` //How verbose logs should be
Token string `json:"token"` //Bot token to login with
OwnerIDs []string `json:"ownerIDs"` //Discord user IDs for accessing debug commands
Ranks []*Rank `json:"ranks"` //The ranks to track
ChannelResults string `json:"channelResults"` //TODO: Move to leaderboards with /channel to specify //The channelID to post duel results to
ColorMain int `json:"colorMain"` //Main color of embeds
}
var (
log *logger.Logger
Discord *DiscordClient
config *Config
)
var (
leaderboards map[string]*Leaderboard
)
func init() {
if err := loadJSON(&config, "config.json"); err != nil {
log.Error(err)
panic(err)
}
log = logger.NewLogger("SF", config.Verbosity)
if err := loadJSON(&leaderboards, "states/leaderboards.json"); err != nil {
log.Error(err)
log.Warn("If the leaderboards state exists, back it up before exiting!")
}
if leaderboards == nil {
leaderboards = make(map[string]*Leaderboard)
}
}
func main() {
log.Debug("... Initializing Discord session")
discordSession, err := discordgo.New("Bot " + config.Token)
if err != nil {
log.Fatal(err)
}
Discord = &DiscordClient{discordSession, nil}
log.Info("... Registering Discord event handlers")
Discord.AddHandler(discordReady)
Discord.AddHandler(discordGuildMemberRemove)
Discord.AddHandler(discordInteractionCreate)
log.Info("... Connecting to Discord")
err = Discord.Open()
if err != nil {
log.Fatal(err)
}
log.Info("Connected to Discord!")
defer Discord.Shutdown()
//Make a channel to listen to OS signals on
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT) //Request notifications for SIGINT signals
signal.Notify(sc, syscall.SIGKILL) //Request notifications for SIGKILL signals
//watchdogTicker := time.Tick(watchdogDelay) //TODO: Spawn separate process and watch over it
//Loop endlessly so we don't exit until required to
for {
select {
//Check for one of our registered signals from the OS
case sig, ok := <-sc:
if ok {
log.Trace("Received signal: ", sig)
return
}
}
}
}
func saveJSON(data interface{}, path string) error {
dataJSON, err := json.Marshal(data, true)
if err != nil {
return err
}
err = ioutil.WriteFile(path, dataJSON, 0644)
return err
}
func loadJSON(data interface{}, path string) error {
dataJSON, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = json.Unmarshal(dataJSON, data)
return err
}
//intDiff returns the difference between two ints, always >= 0
func intDiff(int1, int2 int) int {
intDiff := int1-int2
if intDiff < 0 {
intDiff *= -1
}
return intDiff
}