-
Notifications
You must be signed in to change notification settings - Fork 2
/
command.go
607 lines (524 loc) · 18.4 KB
/
command.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
package helpbot
import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
"text/template"
"time"
"github.com/andersfylling/disgord"
agpb "github.com/autograde/quickfeed/ag"
"github.com/jinzhu/gorm"
"google.golang.org/grpc/metadata"
)
type command func(m *disgord.MessageCreate)
type commandMap map[string]command
func (bot *HelpBot) initCommands() {
bot.baseCommands = commandMap{
"help": bot.baseHelpCommand,
"register": bot.registerCommand,
}
bot.studentCommands = commandMap{
"help": bot.studentHelpCommand,
"gethelp": func(m *disgord.MessageCreate) { bot.helpRequestCommand(m, "help") },
"approve": func(m *disgord.MessageCreate) { bot.helpRequestCommand(m, "approve") },
"cancel": bot.cancelRequestCommand,
"status": bot.studentStatusCommand,
}
bot.assistantCommands = commandMap{
"help": bot.assistantHelpCommand,
"length": bot.lengthCommand,
"list": bot.listCommand,
"next": bot.nextRequestCommand,
"clear": bot.clearCommand,
"unregister": bot.unregisterCommand,
"cancel": bot.assistantCancelCommand,
}
}
var privacy = `
Data collection and privacy:
By using this bot, you consent that your full name, student id, and discord id may be collected for the purposes of
identifying you on this server. If you wish to delete your data, please contact a teaching assistant.`
var baseHelp = createTemplate("baseHelp", `Available commands:
`+"```"+`
{{.Prefix}}help Shows this help text
{{.Prefix}}register [github username] Register your discord account as a student.
`+"```"+privacy)
var studentHelp = createTemplate("studentHelp", `Available commands:
`+"```"+`
{{.Prefix}}help: Shows this help text
{{.Prefix}}gethelp: Request help from a teaching assistant
{{.Prefix}}approve: Get your lab approved by a teaching assistant
{{.Prefix}}cancel: Cancels your help request and removes you from the queue
{{.Prefix}}status: Show your position in the queue
`+"```"+`
After requesting help, you can check the response message you got to see your position in the queue.
You will receive a message when you are next in queue.
`+privacy)
var assistant = createTemplate("assistantHelp", `Teaching Assistant commands:
`+"```"+`
{{.Prefix}}help: Shows this help text
{{.Prefix}}length: Returns the number of students waiting for help.
{{.Prefix}}list <num>: Lists the next <num> students in the queue.
{{.Prefix}}next: Removes and returns the first student from the queue.
{{.Prefix}}clear: Clears the queue!
{{.Prefix}}unregister @mention Unregisters the mentioned user.
{{.Prefix}}cancel Cancels your 'waiting' status.
`+"```"+privacy)
func (bot *HelpBot) helpCommand(m *disgord.MessageCreate, helpTmpl *template.Template) error {
buf := new(strings.Builder)
err := helpTmpl.Execute(buf, bot.cfg)
if err != nil {
return fmt.Errorf("helpCommand: failed to execute template: %w", err)
}
replyMsg(bot.client, m, buf.String())
return nil
}
func (bot *HelpBot) baseHelpCommand(m *disgord.MessageCreate) {
err := bot.helpCommand(m, baseHelp)
if err != nil {
bot.log.Error(err)
}
}
func (bot *HelpBot) studentHelpCommand(m *disgord.MessageCreate) {
err := bot.helpCommand(m, studentHelp)
if err != nil {
bot.log.Error(err)
}
}
func (bot *HelpBot) assistantHelpCommand(m *disgord.MessageCreate) {
err := bot.helpCommand(m, assistant)
if err != nil {
bot.log.Error(err)
}
}
func (bot *HelpBot) helpRequestCommand(m *disgord.MessageCreate, requestType string) {
// create a transaction such that getPos... and Create... are performed atomically
tx := bot.db.Begin()
defer tx.RollbackUnlessCommitted()
// check if an open request already exists
pos, err := getPosInQueue(tx, m.Message.Author.ID)
if err != nil {
bot.log.Errorln("helpRequest: failed to get user pos in queue")
replyMsg(bot.client, m, "An error occurred while creating your request.")
return
}
// already in the queue, no need to do anything.
if pos > 0 {
replyMsg(bot.client, m, fmt.Sprintf("You are already at postition %d in the queue", pos))
return
}
req := HelpRequest{
StudentUserID: m.Message.Author.ID,
Type: requestType,
Done: false,
}
err = tx.Create(&req).Error
if err != nil {
bot.log.Errorln("helpRequest: failed to create new request:", err)
replyMsg(bot.client, m, "An error occurred while creating your request.")
return
}
if bot.assignToIdleAssistant(m.Ctx, tx, req) {
tx.Commit()
return
}
pos, err = getPosInQueue(tx, m.Message.Author.ID)
if err != nil {
bot.log.Errorln("helpRequest: failed to get pos in queue after creating request:", err)
replyMsg(bot.client, m, "An error occurred while creating your request.")
return
}
tx.Commit()
replyMsg(bot.client, m, fmt.Sprintf("A help request has been created, and you are at position %d in the queue.", pos))
}
func (bot *HelpBot) studentStatusCommand(m *disgord.MessageCreate) {
pos, err := getPosInQueue(bot.db, m.Message.Author.ID)
if err != nil {
bot.log.Errorln("studentStatus: failed to get position in queue:", err)
replyMsg(bot.client, m, "An error occurred.")
return
}
if pos <= 0 {
replyMsg(bot.client, m, "You are not in the queue.")
return
}
replyMsg(bot.client, m, fmt.Sprintf("You are at position %d in the queue.", pos))
}
// assignToIdleAssistant will check if any assistants are waiting for a request and pick one of them to handle req.
// db must be a transaction.
func (bot *HelpBot) assignToIdleAssistant(ctx context.Context, db *gorm.DB, req HelpRequest) bool {
err := db.Where("waiting = ?", true).Order("last_request ASC").First(&req.Assistant).Error
if gorm.IsRecordNotFoundError(err) {
return false
} else if err != nil {
bot.log.Errorln("Failed to query for idle assistants:", err)
return false
}
studUser, err := bot.client.GetMember(ctx, bot.cfg.Guild, req.StudentUserID)
if err != nil {
bot.log.Errorln("Failed to retrieve user info for student:", err)
return false
}
assistantUser, err := bot.client.GetMember(ctx, bot.cfg.Guild, req.Assistant.UserID)
if err != nil {
bot.log.Errorln("Failed to retrieve user info for assistant:", err)
return false
}
req.Assistant.LastRequest = time.Now()
req.Assistant.Waiting = false
req.Done = true
req.DoneAt = req.Assistant.LastRequest
req.Reason = "assistantNext"
// need to do this update assistant manually, as zero-valued struct fields are ignored
err = db.Model(&Assistant{}).Where("user_id = ?", req.Assistant.UserID).UpdateColumns(map[string]interface{}{
"waiting": req.Assistant.Waiting,
"last_request": req.Assistant.LastRequest,
}).Error
if err != nil {
bot.log.Errorln("Failed to update assistant status:", err)
return false
}
err = db.Model(&HelpRequest{}).Update(&req).Error
if err != nil {
bot.log.Errorln("Failed to update request:", err)
return false
}
if !sendMsg(ctx, bot.client, assistantUser.User, fmt.Sprintf("Next '%s' request is by %s", req.Type,
getMentionAndNick(studUser))) {
return false
}
if !sendMsg(ctx, bot.client, studUser.User, fmt.Sprintf("You will now receive help from %s.",
getMentionAndNick(assistantUser))) {
return false
}
return true
}
func getPosInQueue(db *gorm.DB, userID disgord.Snowflake) (rowNumber int, err error) {
err = db.Raw(`
select row_number from (
select
row_number () over (
order by created_at asc
) row_number,
student_user_id
from
help_requests
where
done = false
) t
where
student_user_id = ?`,
userID,
).Row().Scan(&rowNumber)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return -1, fmt.Errorf("getPosInQueue error: %w", err)
}
return rowNumber, nil
}
func (bot *HelpBot) cancelRequestCommand(m *disgord.MessageCreate) {
err := bot.db.Model(&HelpRequest{}).Where("student_user_id = ?", m.Message.Author.ID).Updates(map[string]interface{}{
"done": true,
"reason": "userCancel",
"done_at": time.Now(),
}).Error
if gorm.IsRecordNotFoundError(err) {
replyMsg(bot.client, m, "You do not have an active help request.")
return
} else if err != nil {
replyMsg(bot.client, m, "An unknown error occurred.")
return
}
replyMsg(bot.client, m, "Your request was cancelled.")
}
func (bot *HelpBot) nextRequestCommand(m *disgord.MessageCreate) {
// register assistant in DB
var assistant Assistant
err := bot.db.Where("user_id = ?", m.Message.Author.ID).First(&assistant).Error
if gorm.IsRecordNotFoundError(err) {
err := bot.db.Create(&Assistant{
UserID: m.Message.Author.ID,
Waiting: false,
}).Error
if err != nil {
bot.log.Errorln("Failed to create assistant record:", err)
}
} else if err != nil {
bot.log.Errorln("Failed to store assistant in DB:", err)
}
tx := bot.db.Begin()
defer tx.RollbackUnlessCommitted()
var req HelpRequest
err = tx.Where("done = ?", false).Order("created_at asc").First(&req).Error
if gorm.IsRecordNotFoundError(err) {
assistant.UserID = m.Message.Author.ID
assistant.Waiting = true
err := tx.Model(&Assistant{}).Update(&assistant).Error
if err != nil {
bot.log.Errorln("Failed to update waiting state for assistant:", err)
replyMsg(bot.client, m, "There are no more requests in the queue, but due to an error, you won't receive a notification when the next one arrives.")
return
}
if !replyMsg(bot.client, m, "There are no more requests in the queue. You will receive a message when the next request arrives.") {
return
}
tx.Commit()
return
} else if err != nil {
bot.log.Errorln("Failed to get next user:", err)
replyMsg(bot.client, m, "An unknown error occurred.")
return
}
req.Assistant = assistant
req.Assistant.LastRequest = time.Now()
req.Done = true
req.DoneAt = req.Assistant.LastRequest
req.Reason = "assistantNext"
err = tx.Model(&HelpRequest{}).Update(&req).Error
if err != nil {
bot.log.Errorln("Failed to update request:", err)
replyMsg(bot.client, m, "An error occurred while updating request.")
return
}
err = tx.Model(&Assistant{}).Update(&req.Assistant).Error
if err != nil {
bot.log.Errorln("Failed to update assistant:", err)
replyMsg(bot.client, m, "An error occurred while updating assistant status.")
return
}
student, err := bot.client.GetMember(m.Ctx, bot.cfg.Guild, req.StudentUserID)
if err != nil {
bot.log.Errorln("Failed to fetch user:", err)
replyMsg(bot.client, m, "An unknown error occurred.")
return
}
assistantMember, err := bot.client.GetMember(m.Ctx, bot.cfg.Guild, m.Message.Author.ID)
if err != nil {
bot.log.Errorln("Failed to fetch user:", err)
replyMsg(bot.client, m, "An unknown error occurred.")
return
}
if !replyMsg(bot.client, m, fmt.Sprintf("Next '%s' request is by %s.", req.Type, getMentionAndNick(student))) {
return
}
sendMsg(m.Ctx, bot.client, student.User, fmt.Sprintf("You will now receive help from %s", getMentionAndNick(assistantMember)))
tx.Commit()
}
func (bot *HelpBot) lengthCommand(m *disgord.MessageCreate) {
var length int
err := bot.db.Model(&HelpRequest{}).Where("done = ?", false).Count(&length).Error
if err != nil {
bot.log.Errorln("Failed to count number of open requests:", err)
replyMsg(bot.client, m, "An error occurred.")
return
}
var msg string
if length == 1 {
msg = "There is 1 student waiting for help."
} else {
msg = fmt.Sprintf("There are %d students waiting for help.", length)
}
replyMsg(bot.client, m, msg)
}
func (bot *HelpBot) listCommand(m *disgord.MessageCreate) {
words := strings.Fields(m.Message.Content)
num := 10
var err error
if len(words) >= 2 {
num, err = strconv.Atoi(words[1])
if err != nil {
replyMsg(bot.client, m, fmt.Sprintf("'%s' is not a vaild number.", words[1]))
return
}
}
var sb strings.Builder
var requests []HelpRequest
err = bot.db.Where("done = ?", false).Order("created_at asc").Limit(num).Find(&requests).Error
if err != nil {
bot.log.Errorln("Failed to get requests:", err)
replyMsg(bot.client, m, "Failed to get list of requests.")
return
}
if len(requests) == 0 {
replyMsg(bot.client, m, "There are no open requests.")
return
}
fmt.Fprintf(&sb, "Showing the next %d requests:\n\n", len(requests))
for i, req := range requests {
user, err := bot.client.GetMember(m.Ctx, bot.cfg.Guild, req.StudentUserID)
if err != nil {
bot.log.Errorln("Failed to obtain user info:", err)
replyMsg(bot.client, m, "An error occurred while sending the message")
return
}
fmt.Fprintf(&sb, "%d. User: %s, Type: %s\n", i+1, getMentionAndNick(user), req.Type)
}
replyMsg(bot.client, m, sb.String())
}
func (bot *HelpBot) clearCommand(m *disgord.MessageCreate) {
words := strings.Fields(m.Message.Content)
if len(words) < 2 || words[1] != "YES" {
replyMsg(bot.client, m, fmt.Sprintf(
"This command will cancel all the requests in the queue. If you really want to do this, type `%sclear YES`",
bot.cfg.Prefix,
))
return
}
// TODO: send a message to each student whose request was cleared.
err := bot.db.Model(&HelpRequest{}).Where("done = ? ", false).Updates(map[string]interface{}{
"done": true,
"done_at": time.Now(),
"assistant_user_id": m.Message.Author.ID,
"reason": "assistantClear",
}).Error
if err != nil {
bot.log.Errorln("Failed to clear queue:", err)
replyMsg(bot.client, m, "Clear failed due to an error.")
return
}
replyMsg(bot.client, m, "The queue was cleared.")
}
func (bot *HelpBot) registerCommand(m *disgord.MessageCreate) {
words := strings.Fields(m.Message.Content)
if len(words) < 2 {
replyMsg(bot.client, m, "You must include your github username in the command.")
return
}
githubLogin := words[1]
// only allow one user per github login
var count int
err := bot.db.Model(&Student{}).Where("github_login = ?", githubLogin).Count(&count).Error
if err != nil {
bot.log.Errorln("Failed to check for existing user:", err)
replyMsg(bot.client, m, "An unknown error occurred.")
return
}
if count > 0 {
replyMsg(bot.client, m, "That github login has already been used to register! If you believe that this is a mistake, please contact a teaching assistant.")
return
}
membership, _, err := bot.gh.Organizations.GetOrgMembership(m.Ctx, githubLogin, bot.cfg.GitHubOrg)
if err != nil {
bot.log.Infof("Failed to get org membership for user '%s': %v\n", githubLogin, err)
replyMsg(bot.client, m,
"We were unable to verify that you are a member of the course's GitHub organization")
return
}
if membership.GetState() != "active" {
replyMsg(bot.client, m, fmt.Sprintf(
"Please make sure that you have accepted the invitation to join the '%s' organization on GitHub",
bot.cfg.GitHubOrg))
return
}
student := Student{
UserID: m.Message.Author.ID,
GithubLogin: githubLogin,
}
// TODO: query autograder for real name, using github login for now
if bot.cfg.Autograder {
ctx, cancel := context.WithTimeout(m.Ctx, 1*time.Second)
defer cancel()
ctx = metadata.NewOutgoingContext(ctx, bot.ag.md)
req := &agpb.CourseUserRequest{
CourseCode: bot.cfg.CourseCode,
CourseYear: bot.cfg.CourseYear,
UserLogin: githubLogin,
}
userInfo, err := bot.ag.GetUserByCourse(ctx, req)
if err != nil {
bot.log.Errorln("Failed to get info from autograder:", err)
replyMsg(bot.client, m, "Failed to communicate with autograder")
return
}
student.Name = userInfo.GetName()
student.StudentID = userInfo.GetStudentID()
}
// assign roles to student
gm, err := bot.client.GetMember(m.Ctx, bot.cfg.Guild, m.Message.Author.ID)
if err != nil {
bot.log.Errorln("Failed to get member info:", err)
replyMsg(bot.client, m, "An unknown error occurred")
return
}
err = bot.db.Create(&student).Error
if err != nil {
bot.log.Errorln("Failed to store student in database:", err)
replyMsg(bot.client, m, "An uknown error occurred.")
return
}
err = gm.UpdateNick(m.Ctx, bot.client, student.Name)
if err != nil {
bot.log.Errorln("Failed to set nick:", err)
replyMsg(bot.client, m, "An uknown error occurred")
return
}
err = bot.client.AddGuildMemberRole(m.Ctx, bot.cfg.Guild, m.Message.Author.ID, bot.cfg.StudentRole)
if err != nil {
bot.log.Errorln("Failed to add student role:", err)
replyMsg(bot.client, m, "An uknown error occurred")
return
}
replyMsg(bot.client, m, fmt.Sprintf(
"Authentication was successful! You should now have more access to the server. Type %shelp to see available commands",
bot.cfg.Prefix))
}
func (bot *HelpBot) unregisterCommand(m *disgord.MessageCreate) {
if len(m.Message.Mentions) < 1 {
replyMsg(bot.client, m, "You must `@mention` a user to unregister.")
return
}
user := m.Message.Mentions[0]
// permanent deletion from db
err := bot.db.Unscoped().Delete(&Student{}, "user_id = ?", user.ID).Error
if err != nil {
replyMsg(bot.client, m, "Failed to delete user info.")
bot.log.Errorln("Failed to delete student info:", err)
return
}
gm, err := bot.client.GetMember(m.Ctx, bot.cfg.Guild, user.ID)
if err != nil {
replyMsg(bot.client, m, "Failed to get member info. You may have to remove role/nickname manually.")
bot.log.Errorln("Failed to get member info:", err)
return
}
// clear nick
err = gm.UpdateNick(m.Ctx, bot.client, "")
if err != nil {
replyMsg(bot.client, m, "Failed to remove user nick. You may have to remove role/nickname manually.")
bot.log.Errorln("Failed to remove user nick:", err)
return
}
// unassign role
err = bot.client.RemoveGuildMemberRole(m.Ctx, bot.cfg.Guild, user.ID, bot.cfg.StudentRole)
if err != nil {
replyMsg(bot.client, m, "Failed to remove user roles. You may have to remove role/nickname manually.")
bot.log.Errorln("Failed to remove user roles:", err)
return
}
replyMsg(bot.client, m, "User was unregistered.")
}
func (bot *HelpBot) assistantCancelCommand(m *disgord.MessageCreate) {
tx := bot.db.Begin()
defer tx.RollbackUnlessCommitted()
var assistant Assistant
err := tx.Where("user_id = ?", m.Message.Author.ID).First(&assistant).Error
if err != nil {
bot.log.Errorln("Failed to get assistant from DB:", err)
replyMsg(bot.client, m, "An unknown error occurred")
return
}
if !assistant.Waiting {
replyMsg(bot.client, m, "You were not marked as waiting, so no action was taken.")
return
}
err = tx.Model(&Assistant{}).Where("user_id = ?", m.Message.Author.ID).UpdateColumn("waiting", false).Error
if err != nil {
bot.log.Errorln("Failed to update status in DB:", err)
replyMsg(bot.client, m, "An unknown error occurred when attempting to update waiting status.")
return
}
tx.Commit()
replyMsg(bot.client, m, fmt.Sprintf("Your waiting status was removed (you will have to use %snext again to get the next student)", bot.cfg.Prefix))
}