/
main.go
222 lines (190 loc) · 6.05 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/jmoiron/sqlx"
"github.com/robfig/cron/v3"
)
// FLAGS
var token string
var adminID string
var noSlashCommands bool
const discordMaxMessageLength = 2000
func main() {
initFlags()
initDB()
ds := initDiscordSession()
initCRONs(ds)
if !noSlashCommands {
removeSlashCommands := initSlashCommands(ds)
defer removeSlashCommands()
}
// Wait here until CTRL-C or other term signal is received.
log.Println("Bot is now running. Press CTRL-C to exit.")
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-signalChan
ds.Close()
}
func initFlags() {
flag.StringVar(&token, "token", "", "Bot Token")
flag.StringVar(&adminID, "adminID", "", "The ID of the bot's admin")
flag.BoolVar(&noSlashCommands, "noSlashCommands", false, "The bot will not init slash commands, boots faster.")
flag.Parse()
if token == "" {
panic("Provide a token flag!")
}
if adminID == "" {
log.Println("Warning: Admin user ID not set")
}
}
func initDB() {
db := sqlx.MustOpen("sqlite3", dbFilename)
if err := db.Ping(); err != nil {
panic("DB did not answer ping: " + err.Error())
}
createTables(db)
genshinDS = genshinDataStore{db}
commandDS = commandDataStore{db}
moddingDS = moddingDataStore{db}
serverDS = serverDataStore{db}
}
func initDiscordSession() *discordgo.Session {
log.Println("Initiating Discord Session")
ds, err := discordgo.New("Bot " + token)
if err != nil {
panic("error creating Discord session: " + err.Error())
}
backgroundCtx := context.Background()
ds.AddHandler(onMessageCreated(backgroundCtx))
ds.AddHandler(onMessageUpdated(backgroundCtx))
ds.AddHandler(onMessageDeleted(backgroundCtx))
ds.AddHandler(onMessageReacted(backgroundCtx))
ds.AddHandler(onMessageUnreacted(backgroundCtx))
ds.Identify.Intents |= discordgo.IntentGuilds
ds.Identify.Intents |= discordgo.IntentGuildMembers
ds.Identify.Intents |= discordgo.IntentGuildMessages
ds.Identify.Intents |= discordgo.IntentGuildMessageReactions
ds.Identify.Intents |= discordgo.IntentDirectMessages
ds.State.MaxMessageCount = maxMessageCount
// Open a websocket connection to Discord and begin listening.
err = ds.Open()
if err != nil {
panic("error opening connection: " + err.Error())
}
return ds
}
func initCRONs(ds *discordgo.Session) {
log.Println("Initiating CRONs")
initCron := func(name string, cronSpec string, f func()) {
cronJob := cron.New()
_, err := cronJob.AddFunc(cronSpec, f)
if err != nil {
notifyIfErr("AddFunc to "+name, err, ds)
} else {
cronJob.Start()
}
}
initCron("backupCRON", backupCRON, backupCRONFunc(ds))
initCron("dailyCheckInCRON", dailyCheckInReminderCRON, dailyCheckInCRONFunc(ds))
initCron("cleanStateMessagesCRON", cleanStateMessagesCRON, cleanStateMessagesCRONFunc(ds))
initCron("parametricCRON", parametricReminderCRON, parametricCRONFunc(ds))
initCron("playStoreCRON", playStoreReminderCRON, playStoreCRONFunc(ds))
initCron("react4RolesCRON", react4RolesCRON, react4RolesCRONFunc(ds))
}
// initSlashCommands returns a function to remove the registered slash commands for graceful shutdowns
func initSlashCommands(ds *discordgo.Session) func() {
ds.AddHandler(func(ds *discordgo.Session, ic *discordgo.InteractionCreate) {
if h, ok := slashHandlers[ic.ApplicationCommandData().Name]; ok {
h(ds, ic)
} else {
log.Println("ERROR couldnt add handler for slash command:", ic.ApplicationCommandData().Name)
}
})
registeredCommands := make([]*discordgo.ApplicationCommand, len(slashCommands))
for i, slashCommand := range slashCommands {
log.Println("Registering command:", slashCommand.Name)
cmd, err := ds.ApplicationCommandCreate(ds.State.User.ID, "", slashCommand)
if err != nil {
notifyIfErr("Creating command: "+slashCommand.Name, err, ds)
log.Printf("Cannot create '%v' command: %v", slashCommand.Name, err)
}
registeredCommands[i] = cmd
}
return func() {
log.Println("Removing registered slash commands...")
for _, v := range registeredCommands {
err := ds.ApplicationCommandDelete(ds.State.User.ID, "", v.ID)
if err != nil {
notifyIfErr("Deleting command: "+v.Name, err, ds)
log.Printf("Cannot delete '%v' command: %v", v.Name, err)
}
}
}
}
func cleanStateMessagesCRONFunc(ds *discordgo.Session) func() {
return func() {
log.Println("Doing state message cleanup")
for _, pc := range ds.State.PrivateChannels {
cleanStateMessagesInChannel(ds, pc)
}
for _, gc := range ds.State.Guilds {
for _, gc := range gc.Channels {
cleanStateMessagesInChannel(ds, gc)
}
}
}
}
func cleanStateMessagesInChannel(ds *discordgo.Session, channel *discordgo.Channel) {
for _, msg := range channel.Messages {
if msg.Timestamp.Before(time.Now().Add(-stateMessageMaxLifetime)) {
ds.State.MessageRemove(msg)
}
}
}
func sendAsUserWebhook(ds *discordgo.Session, channelID string) (*discordgo.Webhook, error) {
hooks, err := ds.ChannelWebhooks(channelID)
if err != nil {
return nil, err
}
if len(hooks) == 0 {
return ds.WebhookCreate(channelID, "SendAsUser", ds.State.User.AvatarURL(""))
}
return hooks[0], nil
}
func sendAsUser(ds *discordgo.Session, user *discordgo.User, channelID string, content string) (*discordgo.Message, error) {
if user == nil || ds == nil || channelID == "" || content == "" {
return nil, nil
}
webhook, err := sendAsUserWebhook(ds, channelID)
if err != nil {
return nil, err
}
return ds.WebhookExecute(webhook.ID, webhook.Token, false, &discordgo.WebhookParams{
Content: content,
Username: user.GlobalName,
AvatarURL: user.AvatarURL(""),
})
}
func diff(body, prefix string) string {
lines := strings.Split(body, "\n")
var formattedBody string
for _, line := range lines {
formattedBody += prefix + line + "\n"
}
return "```diff\n" + formattedBody + "```"
}
func notifyIfErr(context string, err error, ds *discordgo.Session) {
if err != nil {
msg := "ERROR [" + context + "]: " + err.Error()
log.Println(msg)
sendDirectMessage(adminID, diff(msg, "- "), ds)
}
}