forked from recursecenter/pairing-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdispatch.go
176 lines (150 loc) · 5.36 KB
/
dispatch.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
package main
import (
"context"
"fmt"
"strings"
)
const helpMessage string = "**How to use Pairing Bot:**\n* `subscribe` to start getting matched with other Pairing Bot users for pair programming\n* `schedule monday wednesday friday` to set your weekly pairing schedule\n * In this example, I've been set to find pairing partners for you on every Monday, Wednesday, and Friday\n * You can schedule pairing for any combination of days in the week\n* `skip tomorrow` to skip pairing tomorrow\n * This is valid until matches go out at 04:00 UTC\n* `unskip tomorrow` to undo skipping tomorrow\n* `status` to show your current schedule, skip status, and name\n* `unsubscribe` to stop getting matched entirely\n\nIf you've found a bug, please [submit an issue on github](https://github.com/thwidge/pairing-bot/issues)!"
const subscribeMessage string = "Yay! You're now subscribed to Pairing Bot!\nCurrently, I'm set to find pair programming partners for you on **Mondays**, **Tuesdays**, **Wednesdays**, **Thursdays**, and **Fridays**.\nYou can customize your schedule any time with `schedule` :)"
const unsubscribeMessage string = "You're unsubscribed!\nI won't find pairing partners for you unless you `subscribe`.\n\nBe well :)"
const notSubscribedMessage string = "You're not subscribed to Pairing Bot <3"
var writeErrorMessage = fmt.Sprintf("Something went sideways while writing to the database. You should probably ping %v", owner)
var readErrorMessage = fmt.Sprintf("Something went sideways while reading from the database. You should probably ping %v", owner)
func dispatch(ctx context.Context, pl *PairingLogic, cmd string, cmdArgs []string, userID string, userEmail string, userName string) (string, error) {
var response string
var err error
rec, err := pl.rdb.GetByUserID(ctx, userID, userEmail, userName)
if err != nil {
response = readErrorMessage
return response, err
}
isSubscribed := rec.isSubscribed
// here's the actual actions. command input from
// the user input has already been sanitized, so we can
// trust that cmd and cmdArgs only have valid stuff in them
switch cmd {
case "schedule":
if !isSubscribed {
response = notSubscribedMessage
break
}
// create a new blank schedule
var newSchedule = map[string]interface{}{
"monday": false,
"tuesday": false,
"wednesday": false,
"thursday": false,
"friday": false,
"saturday": false,
"sunday": false,
}
// populate it with the new days they want to pair on
for _, day := range cmdArgs {
newSchedule[day] = true
}
// put it in the database
rec.schedule = newSchedule
err = pl.rdb.Set(ctx, userID, rec)
if err != nil {
response = writeErrorMessage
break
}
response = "Awesome, your new schedule's been set! You can check it with `status`."
case "subscribe":
if isSubscribed {
response = "You're already subscribed! Use `schedule` to set your schedule."
break
}
err = pl.rdb.Set(ctx, userID, rec)
if err != nil {
response = writeErrorMessage
break
}
response = subscribeMessage
case "unsubscribe":
if !isSubscribed {
response = notSubscribedMessage
break
}
err := pl.rdb.Delete(ctx, userID)
if err != nil {
response = writeErrorMessage
break
}
response = unsubscribeMessage
case "skip":
if !isSubscribed {
response = notSubscribedMessage
break
}
rec.isSkippingTomorrow = true
err := pl.rdb.Set(ctx, userID, rec)
if err != nil {
response = writeErrorMessage
break
}
response = `Tomorrow: cancelled. I feel you. **I will not match you** for pairing tomorrow <3`
case "unskip":
if !isSubscribed {
response = notSubscribedMessage
break
}
rec.isSkippingTomorrow = false
err := pl.rdb.Set(ctx, userID, rec)
if err != nil {
response = writeErrorMessage
break
}
response = "Tomorrow: uncancelled! Heckin *yes*! **I will match you** for pairing tomorrow :)"
case "status":
if !isSubscribed {
response = notSubscribedMessage
break
}
// this particular days list is for sorting and printing the
// schedule correctly, since it's stored in a map in all lowercase
var daysList = []string{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"}
// get their current name
whoami := rec.name
// get skip status and prepare to write a sentence with it
var skipStr string
if rec.isSkippingTomorrow {
skipStr = " "
} else {
skipStr = " not "
}
// make a sorted list of their schedule
var schedule []string
for _, day := range daysList {
// this line is a little wild, sorry. it looks so weird because we
// have to do type assertion on both interface types
if rec.schedule[strings.ToLower(day)].(bool) {
schedule = append(schedule, day)
}
}
// make a lil nice-lookin schedule string
var scheduleStr string
for i := range schedule[:len(schedule)-1] {
scheduleStr += schedule[i] + "s, "
}
if len(schedule) > 1 {
scheduleStr += "and " + schedule[len(schedule)-1] + "s"
} else if len(schedule) == 1 {
scheduleStr += schedule[0] + "s"
}
response = fmt.Sprintf("* You're %v\n* You're scheduled for pairing on **%v**\n* **You're%vset to skip** pairing tomorrow", whoami, scheduleStr, skipStr)
case "help":
response = helpMessage
default:
// this won't execute because all input has been sanitized
// by parseCmd() and all cases are handled explicitly above
}
return response, err
}