-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommands.go
More file actions
executable file
·352 lines (312 loc) · 10.1 KB
/
commands.go
File metadata and controls
executable file
·352 lines (312 loc) · 10.1 KB
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
package main
import (
"log"
"github.com/bwmarrin/discordgo"
)
// ID for the CTF text channel category
const ctfCategoryID = "1259233177994661910"
// Define our ApplicationCommands
var (
// Create an array of ApplicationCommand structs to register the definitions of our commands
commands = []*discordgo.ApplicationCommand{
{
Name: "ping",
Description: "ping-command",
},
{
Name: "ctf",
Type: discordgo.ChatApplicationCommand,
Description: "Parent command for the CTF group",
// Subcommands for 'ctf'
Options: []*discordgo.ApplicationCommandOption{
{
Name: "create",
Type: discordgo.ApplicationCommandOptionSubCommand,
Description: "Create a CTF",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "ctf-name",
Description: "CTF name",
Required: true,
},
},
},
{
Name: "join",
Type: discordgo.ApplicationCommandOptionSubCommand,
Description: "Join a CTF",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "ctf-name",
Description: "CTF name",
Required: true,
},
},
},
{
Name: "leave",
Type: discordgo.ApplicationCommandOptionSubCommand,
Description: "Leave a CTF",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "ctf-name",
Description: "CTF name",
Required: false,
},
},
},
},
},
}
// Create a map of <CommandName>:<HandlerFunction> for each command. Each command will
// correspond to a first-class function that will handle the command's usage upon invocation
// commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
// Ping command
// "ping": pingCommandCallback,
// 'ctf' command group. The function is defined below for cleanliness
// "ctf": ctfCommandCallback,
// }
// Define handlers for message components. That is to say, what will be executed when a
// component is interacted with.
// componentsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
// "ctf_join": joinCTF,
// "ctf_leave": leaveCTF,
// }
)
// Ping command handler
func pingCommandCallback(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Pong!",
},
})
}
// This function handles the response action(s) for the 'ctf' group of ApplicationCommands
func ctfCommandCallback(s *discordgo.Session, i *discordgo.InteractionCreate) {
var respContent string
var ctfName string
var respComponents []discordgo.MessageComponent
var leaveRespComponent []discordgo.MessageComponent
data := i.ApplicationCommandData()
// Check which subcommand was called
switch data.Options[0].Name {
case "create":
ctfName = data.Options[0].Options[0].StringValue()
log.Printf("New CTF Name given: %s\n", ctfName)
// Create the new role for the CTF
roleParams := &discordgo.RoleParams{
Name: ctfName,
}
newRole, err := s.GuildRoleCreate(GlobalConfig.GuildID, roleParams)
if err != nil {
respContent = "Could not create new guild role: " + err.Error()
} else {
respContent = ctfName
}
// Reply with a button to allow quickly joining the CTF
actionRow := discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Label: "Join " + ctfName,
Style: discordgo.SuccessButton,
Disabled: false,
CustomID: "ctf_join",
},
},
}
respComponents = append(respComponents, actionRow)
leaveActionRow := discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Label: "Leave " + ctfName,
Style: discordgo.SuccessButton,
Disabled: false,
CustomID: "ctf_leave",
},
},
}
leaveRespComponent = append(leaveRespComponent, leaveActionRow)
// Create a channel for the CTF that is locked to the created role
targetGuild, _ := s.Guild(i.GuildID)
everyoneID := targetGuild.Roles[0].ID // Per my assumption, @everyone is the first role - Jack
channel, err := s.GuildChannelCreateComplex(i.GuildID, discordgo.GuildChannelCreateData{
Name: ctfName,
Type: discordgo.ChannelTypeGuildText,
Topic: "Channel for " + ctfName,
ParentID: ctfCategoryID,
PermissionOverwrites: []*discordgo.PermissionOverwrite{
{ // deny everyone read perms
ID: everyoneID,
Type: 0,
Deny: discordgo.PermissionViewChannel,
Allow: 0,
},
{ // allow the new role to view and send messages
ID: newRole.ID,
Type: 0,
Deny: 0,
Allow: discordgo.PermissionViewChannel | discordgo.PermissionSendMessages,
},
},
})
log.Println(channel)
if err != nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: 1 << 6,
Content: "Could not create channel",
},
})
} else {
log.Println(err)
}
s.ChannelMessageSendComplex(channel.ID, &discordgo.MessageSend{
Content: ctfName,
Components: leaveRespComponent,
})
case "join":
if len(data.Options) > 0 && len(data.Options[0].Options) > 0 {
ctfName := data.Options[0].Options[0].StringValue()
joinCTFCallback(s, i, ctfName)
// We respond in the function call, no need to attempt to respond twice.
return
}
case "leave":
if len(data.Options) > 0 && len(data.Options[0].Options) > 0 {
ctfName := data.Options[0].Options[0].StringValue()
leaveCTFCallback(s, i, ctfName)
// We respond in the function call, no need to attempt to respond twice.
return
}
}
// Send back the status reply
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: respContent,
Components: respComponents,
},
})
if err != nil {
log.Print(err)
}
}
func joinCTF(s *discordgo.Session, i *discordgo.InteractionCreate) {
ctfName := i.Message.Content
joinCTFCallback(s, i, ctfName)
}
func leaveCTF(s *discordgo.Session, i *discordgo.InteractionCreate) {
ctfName := i.Message.Content
leaveCTFCallback(s, i, ctfName)
}
func joinCTFCallback(s *discordgo.Session, i *discordgo.InteractionCreate, ctfName string) {
guild, err := s.Guild(i.GuildID)
if err != nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: 1 << 6,
Content: "Couldn't find the calling Guild",
},
})
log.Println(err)
return
}
callingUser := i.Member.User
log.Printf("Adding user %s to CTF %s", callingUser.Username, ctfName)
var targetRole *discordgo.Role
for _, role := range guild.Roles {
if role.Name == ctfName {
targetRole = role
break
}
}
if targetRole == nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: 1 << 6,
Content: "Could not find role: " + ctfName,
},
})
log.Println(err)
return
}
// Add the user to the role
err = s.GuildMemberRoleAdd(i.GuildID, callingUser.ID, targetRole.ID)
if err != nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: 1 << 6,
Content: "Could not add role to caller",
},
})
log.Println(err)
return
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Added you to " + targetRole.Name,
},
})
}
func leaveCTFCallback(s *discordgo.Session, i *discordgo.InteractionCreate, ctfName string) {
guild, err := s.Guild(i.GuildID)
if err != nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: 1 << 6,
Content: "Couldn't find the calling Guild",
},
})
log.Println(err)
return
}
callingUser := i.Member.User
log.Printf("Removing user %s from CTF %s", callingUser.Username, ctfName)
var targetRole *discordgo.Role
for _, role := range guild.Roles {
if role.Name == ctfName {
targetRole = role
break
}
}
if targetRole == nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: 1 << 6,
Content: "Could not find role: " + ctfName,
},
})
log.Println(err)
return
}
// Remove the user from the role
err = s.GuildMemberRoleRemove(i.GuildID, callingUser.ID, targetRole.ID)
if err != nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: 1 << 6,
Content: "Could not remove role from caller",
},
})
log.Println(err)
return
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Removed you from " + targetRole.Name,
},
})
}