-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslashcommands.go
306 lines (260 loc) · 11 KB
/
slashcommands.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
package main
import (
"strings"
"log"
"fmt"
"strconv"
"regexp"
"time"
"errors"
"github.com/bwmarrin/discordgo"
)
var SlashCommands = []*discordgo.ApplicationCommand{
{
Name: "hello",
Type: discordgo.ChatApplicationCommand,
Description: "basic test",
},
{
Name: "init",
Type: discordgo.ChatApplicationCommand,
Description: "Initializes an empty server",
},
{
Name: "purge",
Type: discordgo.ChatApplicationCommand,
Description: "Nukes the server; used for development",
},
{
Name: "register",
Type: discordgo.ChatApplicationCommand,
Description: "Registers user with role into class.",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "student",
Description: "Register into class as a student.",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "instructor",
Description: "Register into class as a instructor; requires a password",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "password",
Description: "Password to register as insturctor",
Required: true,
},
},
},
{
Name: "ta",
Description: "Register into class as a TA; requires a password",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "password",
Description: "Password to register as TA",
Required: true,
},
},
},
},
},
{
Name: "new-assignment",
Type: discordgo.ChatApplicationCommand,
Description: "Creates a new assignment",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "assignment-name",
Description: "name of the new assignment",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "due-date",
Description: "date the assignment is due in YYYY-MM-DD HH:MM format",
Required: true,
},
},
},
}
var SlashCommandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"hello": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "yay slash command",
},
})
},
"init": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if !adminPrivledgeCommand(s, i) { return }
guildID := i.Interaction.GuildID
/* create channels =-=-=-=-=-=-= */
/* general channels */
s.GuildChannelCreate(guildID, "announcements", discordgo.ChannelTypeGuildText)
s.GuildChannelCreate(guildID, "rules", discordgo.ChannelTypeGuildText)
/* admin channels */
adminCategory, _ := s.GuildChannelCreate(guildID, "admin", discordgo.ChannelTypeGuildCategory);
s.GuildChannelCreateComplex(guildID, discordgo.GuildChannelCreateData{
Name: "admin-commands",
Type: discordgo.ChannelTypeGuildText,
ParentID: adminCategory.ID,
})
/* create roles =-=-=-=-=-=-=-= */
role, _ := s.GuildRoleCreate(guildID);
s.GuildRoleEdit(guildID, role.ID, "Instructor", 16718213, true, 8, true);
role, _ = s.GuildRoleCreate(guildID);
s.GuildRoleEdit(guildID, role.ID, "TA", 16760604, true, 8, true);
role, _ = s.GuildRoleCreate(guildID);
s.GuildRoleEdit(guildID, role.ID, "Student", 1889791, true, 174016814656, true);
interactionSuccess("sucessfully initialized server", s, i)
},
"purge": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if !adminPrivledgeCommand(s, i) { return }
guildID := i.Interaction.GuildID
/* delete all channels */
channels, _ := s.GuildChannels(guildID)
for _, c := range channels { s.ChannelDelete(c.ID) }
/* delete all roles */
roles, _ := s.GuildRoles(guildID)
for _, r := range roles { s.GuildRoleDelete(guildID, r.ID) }
s.GuildChannelCreate(guildID, "general", discordgo.ChannelTypeGuildText)
interactionSuccess("purged server", s, i)
},
"register": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
/* dm only is not good idea, since we don't have guildid */
// if !dmCommand(s, i) { return }
/* parse subcommands */
switch i.ApplicationCommandData().Options[0].Name {
case "student":
registerStudent(s, i)
case "instructor":
registerInstructor(s, i)
case "ta":
registerTA(s, i)
}
},
// TODO switch over to validation library
"new-assignment": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
assignmentNameArg := i.ApplicationCommandData().Options[0].StringValue();
dueDateArg := i.ApplicationCommandData().Options[1].StringValue();
guildID := i.Interaction.GuildID
/* do some validation on args */
dueDate, err := parseDateStringCommandArg(dueDateArg)
/* insert assignment into database */
_, err = DBNewAssignment(guildID, assignmentNameArg, dueDate.Unix());
if err != nil {
interactionError("Failed to create new assignment", s, i)
return
}
interactionSuccess(fmt.Sprintf("Successfully created assignment %s", assignmentNameArg), s, i)
},
}
/* sub commands for register */
func registerStudent(s *discordgo.Session, i *discordgo.InteractionCreate) {
guildID := i.Interaction.GuildID
/* create channels for student */
studentChannelPermissions := []*discordgo.PermissionOverwrite{
{
ID: guildID,
Type: discordgo.PermissionOverwriteTypeRole,
Allow: 0,
Deny: 1024, // disable @everyone from viewing channel
},
{
ID: i.Member.User.ID,
Type: discordgo.PermissionOverwriteTypeMember,
Allow: 1024, // allow viewing
Deny: 0,
},
}
studentCategory, err := s.GuildChannelCreate(guildID, fmt.Sprintf("%s's channels", i.Member.User.Username), discordgo.ChannelTypeGuildCategory);
if err != nil { log.Println(err) }
s.GuildChannelCreateComplex(guildID, discordgo.GuildChannelCreateData{
Name: "questions",
Type: discordgo.ChannelTypeGuildText,
ParentID: studentCategory.ID,
Topic: "Use this channel to ask instructors any private questions",
PermissionOverwrites: studentChannelPermissions,
})
studentRole, _ := findGuildRole(s, i.Interaction.GuildID, "Student")
s.GuildMemberRoleAdd(guildID, i.Member.User.ID, studentRole)
interactionSuccess("Sucessfully registered as student!", s, i)
}
func registerInstructor(s *discordgo.Session, i *discordgo.InteractionCreate) {
guildID := i.Interaction.GuildID
instructorRole, _ := findGuildRole(s, guildID, "Instructor")
s.GuildMemberRoleAdd(guildID, i.Member.User.ID, instructorRole)
interactionSuccess("Sucessfully registered as instructor!", s, i)
}
func registerTA(s *discordgo.Session, i *discordgo.InteractionCreate) {
guildID := i.Interaction.GuildID
taRole, _ := findGuildRole(s, guildID, "TA")
s.GuildMemberRoleAdd(guildID, i.Member.User.ID, taRole)
interactionSuccess("Sucessfully registered as TA!", s, i)
}
/* helpers */
/* restricts command to only be used by admins */
func adminPrivledgeCommand(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
if (i.Interaction.Member.Permissions >> 3) & 0x1 == 0x1 { return true }
interactionError("You do not have permission to execute this command.", s, i)
return false
}
/* restricts command to be used only in dm */
func dmCommand(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
channelInfo, err := s.Channel(i.Interaction.ChannelID)
if err != nil { log.Println("Error retrieving channel information") }
if channelInfo.Type == discordgo.ChannelTypeDM { return true }
interactionError("Please run this command in a DM.", s, i)
return false
}
/* finds a role based on name */
func findGuildRole(s *discordgo.Session, guildID string, roleName string) (string, error) {
guildRoles, _ := s.GuildRoles(guildID)
for _, r := range guildRoles {
if strings.Compare(r.Name, roleName) == 0 { return r.ID, nil }
}
return "", errors.New(fmt.Sprintf("Cannot find role of name %s", roleName))
}
/* checks if string is in YYYY-MM-DD HH:MM format */
/* converts datestring option to date */
func parseDateStringCommandArg(dateString string) (*time.Time, error) {
/* issues with the regex - it's crude since its only for user friendliness */
/* doesnt check leap years */
/* doesnt validate number of days in each month */
/* doesnt check for zero month and dates ie 2021-00-00 */
r := regexp.MustCompile(`(\d{4})-(0\d|1[012])-([012]\d|3[01]) ([01]\d|2[0123]):([012345]\d)`)
matches := r.FindStringSubmatch(dateString)
if matches == nil { return nil, errors.New("invalid datestring") }
/* make this less horrendous */
year, _ := strconv.Atoi(matches[1])
month, _ := strconv.Atoi(matches[2])
day, _ := strconv.Atoi(matches[3])
hour, _ := strconv.Atoi(matches[4])
minute, _ := strconv.Atoi(matches[5])
t := time.Date(year, time.Month(month), day, hour, minute, 0, 0, time.UTC)
return &t, nil
}
/* interaction responses */
func interactionSuccess(successMessage string, s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("%s", successMessage),
},
})
}
func interactionError(errorMessage string, s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("**[ERROR]** %s", errorMessage),
},
})
}