-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (107 loc) · 2.89 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
package main
import (
"RoleKeeper/cfg"
"RoleKeeper/command"
"RoleKeeper/cons"
"RoleKeeper/cwlog"
"RoleKeeper/db"
"RoleKeeper/disc"
"RoleKeeper/glob"
"flag"
"fmt"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
)
func main() {
/* Make data directory */
errr := os.MkdirAll("data", os.ModePerm)
if errr != nil {
fmt.Print(errr.Error())
return
}
/* Make log directory */
errr = os.MkdirAll("data/db", os.ModePerm)
if errr != nil {
fmt.Print(errr.Error())
return
}
glob.ServerRunning = true
glob.DoRegisterCommands = flag.Bool("regCommands", false, "Register discord commands")
glob.DoDeregisterCommands = flag.Bool("deregCommands", false, "Deregister discord commands")
flag.Parse()
db.ThreadCount = runtime.NumCPU()
glob.Uptime = time.Now().UTC().Round(time.Second)
cwlog.StartLog()
cfg.ReadCfg()
cfg.WriteCfg()
db.GuildLookup = make(map[uint64]*db.GuildData, cons.TSize)
db.ReadAllClusters()
db.UpdateGuildLookup()
go startbot()
/* Wait here for process signals */
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
tNow := time.Now().Round(time.Second)
cwlog.DoLog("uptime: " + tNow.Sub(glob.Uptime).String())
command.ClearCommands()
}
var DiscordConnectAttempts int
func startbot() {
if cfg.Config.Token == "" {
cwlog.DoLog("No discord token.")
return
}
cwlog.DoLog(cons.BotName + " " + cons.Version + " starting.")
cwlog.DoLog("Max Guilds: " + strconv.FormatInt(cons.MaxGuilds, 10))
bot, err := discordgo.New("Bot " + cfg.Config.Token)
if err != nil {
cwlog.DoLog(fmt.Sprintf("An error occurred when attempting to create the Discord session. Details: %v", err))
time.Sleep(time.Minute * (5 * cons.MaxDiscordAttempts))
DiscordConnectAttempts++
if DiscordConnectAttempts < cons.MaxDiscordAttempts {
startbot()
}
return
}
bot.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAllWithoutPrivileged)
bot.AddHandler(botReady)
//bot.AddHandler(db.GuildRoleCreate)
bot.AddHandler(db.GuildRoleUpdate)
bot.AddHandler(db.GuildRoleDelete)
errb := bot.Open()
if errb != nil {
cwlog.DoLog(fmt.Sprintf("An error occurred when attempting to create the Discord session. Details: %v", errb))
time.Sleep(time.Minute * (5 * cons.MaxDiscordAttempts))
DiscordConnectAttempts++
if DiscordConnectAttempts < cons.MaxDiscordAttempts {
startbot()
}
return
}
bot.LogLevel = discordgo.LogWarning
}
func botReady(s *discordgo.Session, r *discordgo.Ready) {
botstatus := cfg.Config.Domain
err := s.UpdateGameStatus(0, botstatus)
if err != nil {
cwlog.DoLog(err.Error())
}
s.AddHandler(command.SlashCommand)
disc.Session = s
disc.Ready = r
cwlog.DoLog("Discord bot ready")
go db.LookupRoleNames(s, nil)
if *glob.DoRegisterCommands {
command.RegisterCommands(s)
}
if *glob.DoDeregisterCommands {
command.ClearCommands()
}
go MainLoop()
}