-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
discord.go
392 lines (308 loc) · 11 KB
/
discord.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package servermanager
import (
"errors"
"fmt"
"net/url"
"strings"
"time"
embed "github.com/Clinet/discordgo-embed"
"github.com/bwmarrin/discordgo"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
type DiscordManager struct {
store Store
discord *discordgo.Session
scheduledRacesManager *ScheduledRacesManager
enabled bool
}
// NewDiscordManager instantiates the DiscordManager type. On error, it will log the error and return the type
// flagged as disabled
func NewDiscordManager(store Store, scheduledRacesManager *ScheduledRacesManager) (*DiscordManager, error) {
discordManager := &DiscordManager{
store: store,
scheduledRacesManager: scheduledRacesManager,
discord: nil,
enabled: false,
}
opts, err := store.LoadServerOptions()
if err != nil {
logrus.WithError(err).Errorf("couldn't load server options")
return discordManager, err
}
var session *discordgo.Session
if opts.DiscordAPIToken != "" {
session, err = discordgo.New("Bot " + opts.DiscordAPIToken)
if err == nil {
err = session.Open()
}
if err != nil {
logrus.WithError(err).Errorf("couldn't open discord session")
return discordManager, err
}
} else {
logrus.Debugf("Discord notification bot not enabled")
return discordManager, err
}
logrus.Infof("Discord notification bot connected")
discordManager.enabled = true
discordManager.discord = session
session.AddHandler(discordManager.CommandHandler)
return discordManager, nil
}
func (dm *DiscordManager) SaveServerOptions(oldServerOpts *GlobalServerConfig, newServerOpts *GlobalServerConfig) error {
if newServerOpts.DiscordAPIToken != "" && (oldServerOpts.DiscordAPIToken != newServerOpts.DiscordAPIToken) {
// existing token changed, so stop
if oldServerOpts.DiscordAPIToken != "" && dm.enabled {
_ = dm.Stop()
}
// token added (or changed), so attempt to connect
session, err := discordgo.New("Bot " + newServerOpts.DiscordAPIToken)
if err == nil {
err = session.Open()
}
if err != nil {
logrus.WithError(err).Errorf("couldn't open discord session")
return err
}
dm.discord = session
dm.enabled = true
session.AddHandler(dm.CommandHandler)
logrus.Infof("Discord notification bot reconnected")
} else if newServerOpts.DiscordAPIToken == "" && oldServerOpts.DiscordAPIToken != "" {
// token removed, so close session (also sets enabled to false)
_ = dm.Stop()
logrus.Infof("Discord notification bot stopped")
}
return nil
}
// CommandSessions outputs a full list of all scheduled sessions (P, Q & R), using buildCalendar as a base
func (dm *DiscordManager) CommandSessions() (string, error) {
serverOpts, err := dm.store.LoadServerOptions()
if err != nil {
return "A server error occurred, please try again later", err
}
start := time.Now()
end := start.AddDate(0, 0, 7)
calendar, err := dm.scheduledRacesManager.buildCalendar(start, end)
if err != nil {
return "A server error occurred, please try again later", err
}
msg := fmt.Sprintf("Upcoming sessions on server %s\n", serverOpts.Name)
for _, event := range calendar {
msg += event.Start.Format("Mon, 02 Jan 2006 15:04:05 MST") + "\n"
msg += event.Title + "\n"
msg += event.Description + "\n\n"
}
return msg, nil
}
// CommandSchedule outputs an abbreviated list of all scheduled events
func (dm *DiscordManager) CommandSchedule() (string, error) {
serverOpts, err := dm.store.LoadServerOptions()
if err != nil {
return "A server error occurred, please try again later", err
}
start := time.Now()
end := start.AddDate(0, 0, 7)
scheduled, err := dm.scheduledRacesManager.getScheduledRaces(true)
if err != nil {
return "A server error occurred, please try again later", err
}
var recurring []ScheduledEvent
for _, scheduledEvent := range scheduled {
if scheduledEvent.HasRecurrenceRule() {
customRace, ok := scheduledEvent.(*CustomRace)
if !ok {
continue
}
rule, err := customRace.GetRecurrenceRule()
if err != nil {
continue
}
for _, startTime := range rule.Between(start, end, true) {
newEvent := *customRace
newEvent.Scheduled = startTime
newEvent.UUID = uuid.New()
if customRace.GetScheduledTime() == newEvent.GetScheduledTime() {
continue
}
recurring = append(recurring, &newEvent)
}
}
}
scheduled = append(scheduled, recurring...)
msg := fmt.Sprintf("\nUpcoming events on server %s\n\n", serverOpts.Name)
for _, scheduledEvent := range scheduled {
raceSetup := scheduledEvent.GetRaceSetup()
cars := carList(scheduledEvent.GetRaceSetup().Cars)
msg += fmt.Sprintf("Date: %s\n", scheduledEvent.GetScheduledTime().Format("Mon, 02 Jan 2006 15:04:05 MST"))
msg += fmt.Sprintf("Track: %s\n", trackSummary(raceSetup.Track, raceSetup.TrackLayout))
msg += fmt.Sprintf("Cars: %s\n", cars)
msg += "\n\n"
}
return msg, nil
}
// CommandNotify attempts to add a role ID (if configured) to the user issuing the !notify command
// The role will be added as a mention on all Discord notifications
func (dm *DiscordManager) CommandNotify(s *discordgo.Session, m *discordgo.MessageCreate) (string, error) {
serverOpts, err := dm.store.LoadServerOptions()
if err != nil {
logrus.WithError(err).Infof("couldn't get server options")
return "A server error occurred, try again later", err
}
if serverOpts.DiscordRoleID == "" || serverOpts.DiscordRoleCommand == "" {
return "", nil
}
// get the member
member, err := s.State.Member(m.GuildID, m.Author.ID)
if err != nil {
// if it's not in the state, punt to asking the server
if err == discordgo.ErrStateNotFound {
member, err = s.GuildMember(m.GuildID, m.Author.ID)
}
if err != nil {
return "You don't seem to exist, so I can't assign you that role. Try again later.", err
}
}
// get the role name from ID, for use in user feedback
roleName := "notification"
roles, err := s.GuildRoles(m.GuildID)
if err != nil {
// meh, just log it and carry on
logrus.WithError(err).Infof("failed to get Discord roles (make sure you have set the bot permissions to see roles)")
} else {
for _, role := range roles {
if strings.TrimSpace(role.ID) == strings.TrimSpace(serverOpts.DiscordRoleID) {
roleName = role.Name
break
}
}
}
for _, roleID := range member.Roles {
if roleID == serverOpts.DiscordRoleID {
// they have the role, so remove it
err = s.GuildMemberRoleRemove(m.GuildID, m.Author.ID, serverOpts.DiscordRoleID)
if err != nil {
// meh, log the error here, and just return some feedback to the user
logrus.WithError(err).Infof("failed to remove Discord role (make sure you have set the bot permissions to manage roles)")
return fmt.Sprintf("You already have the %s role, and an error occurred trying to remove it. Try again later.", roleName), nil
}
return fmt.Sprintf("You already had the %s role, it has now been removed. Type the command again to add it back.", roleName), nil
}
}
// they didn't have the role, so add it
err = s.GuildMemberRoleAdd(m.GuildID, m.Author.ID, serverOpts.DiscordRoleID)
if err != nil {
// meh, log the error here and return feedback to the user
logrus.WithError(err).Infof("failed to set Discord role (make sure you have set the bot permissions to manage roles)")
return fmt.Sprintf("A server error occurred trying to assign you the %s role, try again later", roleName), nil
}
// w00t!
return fmt.Sprintf("The %s role has been assigned, you will now get pinged with notifications. Type the command again to remove it.", roleName), nil
}
func (dm *DiscordManager) CommandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
serverOpts, err := dm.store.LoadServerOptions()
if err != nil {
logrus.WithError(err).Errorf("couldn't load server opts")
return
}
if m.Author.ID == s.State.User.ID {
return
}
msg := ""
switch m.Content {
case "!schedule":
msg, err = dm.CommandSchedule()
case "!sessions":
msg, err = dm.CommandSessions()
case "!" + serverOpts.DiscordRoleCommand:
msg, err = dm.CommandNotify(s, m)
default:
return
}
// if error, log it, but continue with message sending, handler may have put user feedback in msg
if err != nil {
logrus.WithError(err).Errorf("Error during handling of Discord command")
}
if msg != "" {
_, err = s.ChannelMessageSend(m.ChannelID, msg)
if err != nil {
logrus.WithError(err).Errorf("couldn't send Discord msg")
}
}
}
func (dm *DiscordManager) Stop() error {
if dm.enabled {
dm.enabled = false
return dm.discord.Close()
}
return nil
}
// SendMessage sends a message to the configured channel and logs any errors
func (dm *DiscordManager) SendMessage(title string, msg string) error {
if dm.enabled {
opts, err := dm.store.LoadServerOptions()
if err != nil {
logrus.WithError(err).Errorf("couldn't load server options")
return err
}
// could check DiscordChannelID in new, but plan is to allow per-championship channels, so will need to pass
// it in as an arg and check it here anyway
if opts.DiscordChannelID != "" {
if opts.DiscordRoleID != "" {
mention := fmt.Sprintf("Attention <@&%s> - %s\n", opts.DiscordRoleID, title)
messageSend := &discordgo.MessageSend{
Content: mention,
Embed: embed.NewEmbed().SetDescription(msg).SetColor(0x1c1c1c).MessageEmbed,
}
_, err = dm.discord.ChannelMessageSendComplex(opts.DiscordChannelID, messageSend)
} else {
_, err = dm.discord.ChannelMessageSendEmbed(opts.DiscordChannelID, embed.NewGenericEmbed(title, msg))
}
if err != nil {
logrus.WithError(err).Errorf("couldn't send discord message")
return err
}
} else {
err = errors.New("no channel ID set in config")
logrus.WithError(err).Errorf("couldn't send discord message")
return err
}
}
return nil
}
// SendMessage sends a message to the configured channel and logs any errors
func (dm *DiscordManager) SendMessageWithLink(title string, msg string, linkText string, link *url.URL) error {
if !dm.enabled {
return nil
}
opts, err := dm.store.LoadServerOptions()
if err != nil {
logrus.WithError(err).Errorf("couldn't load server options")
return err
}
linkMsg := "[" + linkText + "](" + link.String() + ")"
// could check DiscordChannelID in new, but plan is to allow per-championship channels, so will need to pass
// it in as an arg and check it here anyway
if opts.DiscordChannelID != "" {
if opts.DiscordRoleID != "" {
mention := fmt.Sprintf("Attention <@&%s> - %s\n", opts.DiscordRoleID, title)
messageSend := &discordgo.MessageSend{
Content: mention,
Embed: embed.NewEmbed().SetDescription(msg + "\n" + linkMsg).SetColor(0x1c1c1c).MessageEmbed,
}
_, err = dm.discord.ChannelMessageSendComplex(opts.DiscordChannelID, messageSend)
} else {
_, err = dm.discord.ChannelMessageSendEmbed(opts.DiscordChannelID, embed.NewGenericEmbed(title, msg+"\n"+linkMsg))
}
if err != nil {
logrus.WithError(err).Errorf("couldn't send discord message")
return err
}
} else {
err = errors.New("no channel ID set in config")
logrus.WithError(err).Errorf("couldn't send discord message")
return err
}
return err
}