-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
524 lines (456 loc) · 17.3 KB
/
bot.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package main
import (
"fmt"
"log"
"math"
"strings"
"time"
"unicode"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/state"
"github.com/diamondburned/arikawa/v3/utils/json/option"
)
var verifiedRoles map[discord.GuildID]*discord.Role = map[discord.GuildID]*discord.Role{}
// colour_button_prefix defines a prefix for the IDs on the buttons that set a person's colour roles.
const colour_button_prefix = "colour_button_"
// pronoun_button_prefix defines a prefix for the IDs on the buttons that set a person's pronoun roles.
const pronoun_button_prefix = "pronoun_button_"
// role_button_prefix defines a prefix for the IDs on the buttons that set a person's generic roles.
const role_button_prefix = "role_button_"
// verify_button_guild_prefix defines a prefix for the IDs on buttons that allow someone to verify in a specified guild - used for DMs.
const verify_button_guild_prefix = "verifyme_button_guild_"
// Bot holds the current Discord state, and allows access to all of the bot's methods.
type Bot struct {
State *state.State
}
// CreatePronounPicker is run by the interaction event dispatcher when the command
// to create a pronoun picker in the current channel is activated.
func (bot *Bot) CreatePronounPicker(e *gateway.InteractionCreateEvent, guild discord.Guild) error {
// the event dispatcher has already checked we're in a guild, etc.
if err := bot.State.RespondInteraction(e.ID, e.Token,
generateInteractionResponseWithButtons(pronoun_button_prefix, config.Pronouns, "👋 What pronouns do you use?")); err != nil {
log.Println("failed to send interaction callback in pronoun picker:", err)
return err
} else {
return nil
}
}
// CreateColourPicker is run by the interaction event dispatcher when the command
// to create a colour picker in the current channel is activated.
func (bot *Bot) CreateColourPicker(e *gateway.InteractionCreateEvent, guild discord.Guild) error {
// the event dispatcher has already checked we're in a guild, etc.
if err := bot.State.RespondInteraction(e.ID, e.Token,
generateInteractionResponseWithButtons(colour_button_prefix, config.Guilds[guild.ID].Colours, "🎨 Pick a colour for your username!")); err != nil {
log.Println("failed to send interaction callback in colour picker:", err)
return err
} else {
return nil
}
}
// CreateRolePicker is run by the interaction event dispatcher when the command
// to create a generic role picker in the current channel is activated.
func (bot *Bot) CreateRolePicker(e *gateway.InteractionCreateEvent, guild discord.Guild) error {
// the event dispatcher has already checked we're in a guild, etc.
if err := bot.State.RespondInteraction(e.ID, e.Token,
generateInteractionResponseWithButtons(role_button_prefix, config.Guilds[guild.ID].Roles, "📋 Collect any extra roles you'd like.")); err != nil {
log.Println("failed to send interaction callback in role picker:", err)
return err
} else {
return nil
}
}
// generateInteractionResponseWithButtons generates an InteractionResponse with a series of buttons,
// in the appropriate number of action rows.
func generateInteractionResponseWithButtons(prefix string, buttons []string, content string) api.InteractionResponse {
actionRows := discord.ContainerComponents{}
// Each row can only hold five components, so we need to do this for
// ceil(n/5) times.
for i := 0; i < int(math.Ceil(float64(len(buttons))/5)); i++ {
actionRowComponents := discord.ActionRowComponent{}
limit := 5
// Reduce the limit if we have less than 5 buttons left
if len(buttons)-(i*5) < 5 {
limit = len(buttons) - (i * 5)
}
// j will start at 0, then go up to 4. next time, it starts at 5
for j := i * 5; j < (i*5)+limit; j++ {
thisButton := buttons[j]
// Golang has no built-in ability to just capitalise the first letter of a string...
// :wut:
// So we have to do it manually *sighs*
thisButtonRuneArray := []rune(thisButton)
thisButtonRuneArray[0] = unicode.ToUpper(thisButtonRuneArray[0])
actionRowComponents = append(actionRowComponents, &discord.ButtonComponent{
CustomID: discord.ComponentID(prefix + thisButton),
Label: string(thisButtonRuneArray),
Style: discord.SecondaryButtonStyle(),
})
}
actionRows = append(actionRows, &actionRowComponents)
}
return api.InteractionResponse{
Type: api.MessageInteractionWithSource,
Data: &api.InteractionResponseData{
Content: option.NewNullableString(content),
Components: &actionRows,
},
}
}
// CreateVerificationButton is run by the interaction event dispatcher when the command
// to create a verification button in the current channel is activated.
func (bot *Bot) CreateVerificationButton(e *gateway.InteractionCreateEvent) error {
// the event dispatcher has already checked we're in a guild, etc.
data := api.InteractionResponse{
Type: api.MessageInteractionWithSource,
Data: &api.InteractionResponseData{
Content: option.NewNullableString("Ready to get verified? Click here to start the process..."),
Components: discord.ComponentsPtr(
&discord.ActionRowComponent{
&discord.ButtonComponent{
CustomID: "verifyme_button",
Label: "Let's get verified!",
Emoji: &discord.ComponentEmoji{
Name: "🎉",
},
Style: discord.PrimaryButtonStyle(),
},
},
),
},
}
if err := bot.State.RespondInteraction(e.ID, e.Token, data); err != nil {
log.Println("failed to send interaction callback:", err)
return err
} else {
return nil
}
}
// OnVerifyMeButton is run by the interaction event dispatcher when the "Verify me"
// button is pressed.
func (bot *Bot) OnVerifyMeButton(e *gateway.InteractionCreateEvent) error {
verifiedRole, err := bot.getVerifiedRole(e.GuildID)
if err != nil {
return err
}
authenticated, memberType := isDiscordAuthenticated(e.Member.User, getMemberTypeForGuild(e.GuildID))
if authenticated {
// Optionally add the role if they aren't already owning it - so ignore errors here!
bot.State.AddRole(e.GuildID, e.Member.User.ID, *verifiedRole, api.AddRoleData{
AuditLogReason: api.AuditLogReason(fmt.Sprintf("Pre-registered, button verified with the bot as %s", memberType)),
})
data := api.InteractionResponse{
Type: api.MessageInteractionWithSource,
Data: &api.InteractionResponseData{
Content: option.NewNullableString("You're already registered, so all's good! Enjoy your day 😊"),
Flags: api.EphemeralResponse,
},
}
if err := bot.State.RespondInteraction(e.ID, e.Token, data); err != nil {
log.Println("failed to send interaction callback for already-registered interaction:", err)
return err
} else {
return nil
}
} else {
bot.State.RemoveRole(e.GuildID, e.Member.User.ID, *verifiedRole, "Pressed the button to verify themselves, not entitled to verification yet")
data := api.InteractionResponse{
Type: api.MessageInteractionWithSource,
Data: &api.InteractionResponseData{
Content: option.NewNullableString("Check your DMs to complete verification!"),
Flags: api.EphemeralResponse,
},
}
if err := bot.State.RespondInteraction(e.ID, e.Token, data); err != nil {
log.Println("failed to send interaction callback for failed interaction:", err)
return err
} else {
return bot.VerifyUser(e.Member.User, e.GuildID)
}
}
}
// InteractionToggleUserRole responds to an InteractionCreateEvent from the dispatcher by
// assigning a user a role, wrapping toggleUserRole.
func (bot *Bot) InteractionToggleUserRole(e *gateway.InteractionCreateEvent, member *discord.Member, roleName string, guildID discord.GuildID, auditLogReason string) error {
assigned, err := bot.toggleUserRole(member, roleName, guildID, auditLogReason)
if err != nil {
return err
}
var message string
if assigned {
message = "Now you've got the"
} else {
message = "No more"
}
data := api.InteractionResponse{
Type: api.MessageInteractionWithSource,
Data: &api.InteractionResponseData{
Content: option.NewNullableString(fmt.Sprintf("Nice job! %s %s role 😊", message, roleName)),
Flags: api.EphemeralResponse,
},
}
if err := bot.State.RespondInteraction(e.ID, e.Token, data); err != nil {
log.Println("failed to send interaction callback for assigning user role interaction:", err)
return err
} else {
return nil
}
}
// toggleUserRole internally assigns a user a role, or creates a role
// and assigns it to the user if it did not already exist. It returns
// a boolean indicating whether it assigned (true) or removed (false)
// the role, and an error.
func (bot *Bot) toggleUserRole(member *discord.Member, roleName string, guildID discord.GuildID, auditLogReason string) (bool, error) {
roles, err := bot.State.Roles(guildID)
if err != nil {
return false, err
}
var roleToUse *discord.Role
for _, role := range roles {
if strings.EqualFold(role.Name, roleName) {
roleToUse = &role
// Failing to break early from the loop here creates an issue where the pointer moves to the end of the list.
break
}
}
if roleToUse == nil {
roleToUse, err = bot.State.CreateRole(guildID, api.CreateRoleData{
Name: roleName,
})
if err != nil {
return false, err
}
}
hasRole := false
for _, roleID := range member.RoleIDs {
if roleID == roleToUse.ID {
hasRole = true
break
}
}
if hasRole {
err = bot.State.RemoveRole(guildID, member.User.ID, roleToUse.ID, api.AuditLogReason(auditLogReason))
return false, err
} else {
err = bot.State.AddRole(guildID, member.User.ID, roleToUse.ID, api.AddRoleData{
AuditLogReason: api.AuditLogReason(auditLogReason),
})
return true, err
}
}
// VerifyUser starts the verification process with a user, and manages it through to the end.
func (bot *Bot) VerifyUser(user discord.User, guildID discord.GuildID) error {
memberChannel, err := bot.State.CreatePrivateChannel(user.ID)
if err != nil {
return err
}
bot.State.SendMessageComplex(memberChannel.ID, api.SendMessageData{
Content: "Hi, welcome to the LGBTQ+ Society server! To verify that you're a student, please click here, and sign in within the next 10 minutes 😃",
Components: discord.ContainerComponents{
&discord.ActionRowComponent{
&discord.ButtonComponent{
Label: "Click here to verify",
Emoji: &discord.ComponentEmoji{
Name: "🔑",
},
Style: discord.LinkButtonStyle(getDiscordAuthLink(user)),
},
&discord.ButtonComponent{
Label: "Read our Member Data Policy",
Emoji: &discord.ComponentEmoji{
Name: "🔒",
},
Style: discord.LinkButtonStyle("https://www.sotonlgbt.org.uk/privacy"),
},
},
},
})
bot.State.SendMessageComplex(memberChannel.ID, api.SendMessageData{
Content: "Once you've verified, please click here!",
Components: discord.ContainerComponents{
&discord.ActionRowComponent{
&discord.ButtonComponent{
Label: "I've verified!",
CustomID: "verified_button",
Emoji: &discord.ComponentEmoji{
Name: "✔️",
},
Style: discord.SuccessButtonStyle(),
},
},
},
})
var interactionToRespondTo *gateway.InteractionCreateEvent
hasValidatedEventChannel, cancelEventChannel := bot.State.ChanFor(func(v interface{}) bool {
// Incoming event is a component interaction:
ci, ok := v.(*gateway.InteractionCreateEvent)
if ok {
switch d := ci.Data.(type) {
case *discord.ButtonInteraction:
if ci.ChannelID == memberChannel.ID && ci.User.ID == user.ID && d.CustomID == "verified_button" {
interactionToRespondTo = ci
return true
}
default:
}
}
// Incoming event is a message create event:
mg, ok := v.(*gateway.MessageCreateEvent)
if !ok {
return false
}
// Message is from the same author and is a DM:
return mg.Author.ID == user.ID && mg.ChannelID == memberChannel.ID
})
halfElapsed := false
timedOut := false
repeatSelect:
for {
select {
case <-hasValidatedEventChannel:
break repeatSelect
case <-time.After(time.Minute * 5):
if halfElapsed {
timedOut = true
break repeatSelect
} else {
isAuthenticated, _ := isDiscordAuthenticated(user, getMemberTypeForGuild(guildID))
if isAuthenticated {
break repeatSelect
} else {
halfElapsed = true
bot.State.SendMessage(memberChannel.ID, "You've got five minutes left to verify - if you're not verified by then, you'll need to rejoin the server 😢 Having trouble? Message a committee member or email [email protected].")
}
}
}
}
cancelEventChannel()
verifiedRole, err := bot.getVerifiedRole(guildID)
if err != nil {
return err
}
isAuthenticated, memberCode := isDiscordAuthenticated(user, getMemberTypeForGuild(guildID))
if isAuthenticated {
err = bot.State.AddRole(guildID, user.ID, *verifiedRole, api.AddRoleData{
AuditLogReason: api.AuditLogReason(fmt.Sprintf("Verified successfully with the bot as %s", memberCode)),
})
if err != nil {
return err
}
const message = "Thanks! You're now verified. Have a great day!"
if interactionToRespondTo != nil {
data := api.InteractionResponse{
Type: api.UpdateMessage,
Data: &api.InteractionResponseData{
Content: option.NewNullableString(message),
},
}
if err := bot.State.RespondInteraction(interactionToRespondTo.ID, interactionToRespondTo.Token, data); err != nil {
log.Println("failed to send interaction callback:", err)
}
} else {
bot.State.SendMessage(memberChannel.ID, message)
}
} else {
reinviteMessageData, err := bot.createReinviteMessage(guildID, user)
if err != nil {
return err
}
if timedOut {
member, err := bot.State.Member(guildID, user.ID)
if err != nil {
return err
}
for _, roleID := range member.RoleIDs {
if roleID == *verifiedRole {
// the member was verified manually - ignore them
bot.State.SendMessage(memberChannel.ID, "Looks like you were verified manually! Clipping through the map 😉 see ya!")
return nil
}
}
// the member doesn't have the verified role - kick them.
reinviteMessageData.Content = "Whoops - time's up, and it doesn't look like you've verified. Please try joining the server again."
bot.State.SendMessageComplex(memberChannel.ID, *reinviteMessageData)
bot.State.Kick(guildID, user.ID, "Timed out without verification, took too long to verify")
return nil
} else {
if memberCode == "" {
reinviteMessageData.Content = "Sorry, that doesn't look like you authenticated successfully. Please try joining the server again."
bot.State.Kick(guildID, user.ID, "Claimed to be authenticated but was not in fact registered")
} else {
reinviteMessageData.Content = "Hmm - that doesn't look like you have the right type of University account for this server. If you've recently graduated, you may need to get a committee member to manually verify you ([email protected]), or contact iSolutions to get them to correct your account. Otherwise, please try joining the server again."
bot.State.Kick(guildID, user.ID, api.AuditLogReason(fmt.Sprintf("Was not authenticated successfully - authenticated as %s which is invalid for this guild", memberCode)))
}
bot.State.SendMessageComplex(memberChannel.ID, *reinviteMessageData)
return nil
}
}
return nil
}
func (bot *Bot) createReinviteMessage(guildID discord.GuildID, user discord.User) (*api.SendMessageData, error) {
guildChannels, err := bot.State.Channels(guildID)
if err != nil {
return nil, err
}
var inviteChannel discord.Channel
// start off with a ridiculous position - our first channel must be below this.
var lowestChannelPosition int = 10000
for _, channel := range guildChannels {
if channel.Type == discord.GuildText && lowestChannelPosition > channel.Position {
inviteChannel = channel
lowestChannelPosition = channel.Position
}
}
invite, err := bot.State.CreateInvite(inviteChannel.ID, api.CreateInviteData{
MaxUses: 1,
Unique: true,
AuditLogReason: api.AuditLogReason(fmt.Sprintf("%s failed authentication, so creating them an easy re-invite link", user.Username)),
})
if err != nil {
return nil, err
}
reinviteMessageData := api.SendMessageData{
Components: discord.ContainerComponents{
&discord.ActionRowComponent{
&discord.ButtonComponent{
Label: "Let's try again",
Emoji: &discord.ComponentEmoji{
Name: "🚶",
},
Style: discord.LinkButtonStyle(fmt.Sprintf("https://discord.gg/%s", invite.Code)),
},
},
},
}
return &reinviteMessageData, nil
}
// getVerifiedRole gets either the cached or the new "verified" role for the server.
func (bot *Bot) getVerifiedRole(guildID discord.GuildID) (*discord.RoleID, error) {
if verifiedRoles[guildID] != nil {
return &verifiedRoles[guildID].ID, nil
}
roles, err := bot.State.Roles(guildID)
if err != nil {
return nil, err
}
for _, role := range roles {
if strings.EqualFold(role.Name, "verified") {
verifiedRoles[guildID] = &role
return &role.ID, nil
}
}
return nil, fmt.Errorf("no verified role found on server %d! Please ensure that there is a role on the server called 'verified', with case insensitive", guildID)
}
// getMemberTypeForGuild takes a guild ID and gets the type of
// student meant to be on that guild.
func getMemberTypeForGuild(guildID discord.GuildID) StudentType {
var memberType StudentType
memberType = &CurrentStudent{}
for configGuildID, guildConfig := range config.Guilds {
if guildID == configGuildID && guildConfig.AlumniGuild {
memberType = &Alumnus{}
}
}
return memberType
}