/
main.go
450 lines (432 loc) Β· 14 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
// Bot parameters
var (
GuildID = flag.String("guild", "", "Test guild ID")
BotToken = flag.String("token", "", "Bot access token")
AppID = flag.String("app", "", "Application ID")
)
var s *discordgo.Session
func init() { flag.Parse() }
func init() {
var err error
s, err = discordgo.New("Bot " + *BotToken)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
}
// Important note: call every command in order it's placed in the example.
var (
componentsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"fd_no": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Huh. I see, maybe some of these resources might help you?",
Flags: 1 << 6,
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Emoji: discordgo.ComponentEmoji{
Name: "π",
},
Label: "Documentation",
Style: discordgo.LinkButton,
URL: "https://discord.com/developers/docs/interactions/message-components#buttons",
},
discordgo.Button{
Emoji: discordgo.ComponentEmoji{
Name: "π§",
},
Label: "Discord developers",
Style: discordgo.LinkButton,
URL: "https://discord.gg/discord-developers",
},
discordgo.Button{
Emoji: discordgo.ComponentEmoji{
Name: "π¦«",
},
Label: "Discord Gophers",
Style: discordgo.LinkButton,
URL: "https://discord.gg/7RuRrVHyXF",
},
},
},
},
},
})
if err != nil {
panic(err)
}
},
"fd_yes": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Great! If you wanna know more or just have questions, feel free to visit Discord Devs and Discord Gophers server. " +
"But now, when you know how buttons work, let's move onto select menus (execute `/selects single`)",
Flags: 1 << 6,
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Emoji: discordgo.ComponentEmoji{
Name: "π§",
},
Label: "Discord developers",
Style: discordgo.LinkButton,
URL: "https://discord.gg/discord-developers",
},
discordgo.Button{
Emoji: discordgo.ComponentEmoji{
Name: "π¦«",
},
Label: "Discord Gophers",
Style: discordgo.LinkButton,
URL: "https://discord.gg/7RuRrVHyXF",
},
},
},
},
},
})
if err != nil {
panic(err)
}
},
"select": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var response *discordgo.InteractionResponse
data := i.MessageComponentData()
switch data.Values[0] {
case "go":
response = &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "This is the way.",
Flags: 1 << 6,
},
}
default:
response = &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "It is not the way to go.",
Flags: 1 << 6,
},
}
}
err := s.InteractionRespond(i.Interaction, response)
if err != nil {
panic(err)
}
time.Sleep(time.Second) // Doing that so user won't see instant response.
_, err = s.FollowupMessageCreate(*AppID, i.Interaction, true, &discordgo.WebhookParams{
Content: "Anyways, now when you know how to use single select menus, let's see how multi select menus work. " +
"Try calling `/selects multi` command.",
Flags: 1 << 6,
})
if err != nil {
panic(err)
}
},
"stackoverflow_tags": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
data := i.MessageComponentData()
const stackoverflowFormat = `https://stackoverflow.com/questions/tagged/%s`
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Here is your stackoverflow link: " + fmt.Sprintf(stackoverflowFormat, strings.Join(data.Values, "+")),
Flags: 1 << 6,
},
})
if err != nil {
panic(err)
}
time.Sleep(time.Second) // Doing that so user won't see instant response.
_, err = s.FollowupMessageCreate(*AppID, i.Interaction, true, &discordgo.WebhookParams{
Content: "Now you know everything about select component. If you want to know more or ask a question - feel free to.",
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Emoji: discordgo.ComponentEmoji{
Name: "π",
},
Label: "Documentation",
Style: discordgo.LinkButton,
URL: "https://discord.com/developers/docs/interactions/message-components#select-menus",
},
discordgo.Button{
Emoji: discordgo.ComponentEmoji{
Name: "π§",
},
Label: "Discord developers",
Style: discordgo.LinkButton,
URL: "https://discord.gg/discord-developers",
},
discordgo.Button{
Emoji: discordgo.ComponentEmoji{
Name: "π¦«",
},
Label: "Discord Gophers",
Style: discordgo.LinkButton,
URL: "https://discord.gg/7RuRrVHyXF",
},
},
},
},
Flags: 1 << 6,
})
if err != nil {
panic(err)
}
},
}
commandsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"buttons": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Are you comfortable with buttons and other message components?",
Flags: 1 << 6,
// Buttons and other components are specified in Components field.
Components: []discordgo.MessageComponent{
// ActionRow is a container of all buttons within the same row.
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
// Label is what the user will see on the button.
Label: "Yes",
// Style provides coloring of the button. There are not so many styles tho.
Style: discordgo.SuccessButton,
// Disabled allows bot to disable some buttons for users.
Disabled: false,
// CustomID is a thing telling Discord which data to send when this button will be pressed.
CustomID: "fd_yes",
},
discordgo.Button{
Label: "No",
Style: discordgo.DangerButton,
Disabled: false,
CustomID: "fd_no",
},
discordgo.Button{
Label: "I don't know",
Style: discordgo.LinkButton,
Disabled: false,
// Link buttons don't require CustomID and do not trigger the gateway/HTTP event
URL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
Emoji: discordgo.ComponentEmoji{
Name: "π€·",
},
},
},
},
// The message may have multiple actions rows.
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Label: "Discord Developers server",
Style: discordgo.LinkButton,
Disabled: false,
URL: "https://discord.gg/discord-developers",
},
},
},
},
},
})
if err != nil {
panic(err)
}
},
"selects": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var response *discordgo.InteractionResponse
switch i.ApplicationCommandData().Options[0].Name {
case "single":
response = &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Now let's take a look on selects. This is single item select menu.",
Flags: 1 << 6,
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.SelectMenu{
// Select menu, as other components, must have a customID, so we set it to this value.
CustomID: "select",
Placeholder: "Choose your favorite programming language π",
Options: []discordgo.SelectMenuOption{
{
Label: "Go",
// As with components, this things must have their own unique "id" to identify which is which.
// In this case such id is Value field.
Value: "go",
Emoji: discordgo.ComponentEmoji{
Name: "π¦¦",
},
// You can also make it a default option, but in this case we won't.
Default: false,
Description: "Go programming language",
},
{
Label: "JS",
Value: "js",
Emoji: discordgo.ComponentEmoji{
Name: "π¨",
},
Description: "JavaScript programming language",
},
{
Label: "Python",
Value: "py",
Emoji: discordgo.ComponentEmoji{
Name: "π",
},
Description: "Python programming language",
},
},
},
},
},
},
},
}
case "multi":
response = &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "The tastiest things are left for the end. Let's see how the multi-item select menu works: " +
"try generating your own stackoverflow search link",
Flags: 1 << 6,
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.SelectMenu{
CustomID: "stackoverflow_tags",
Placeholder: "Select tags to search on StackOverflow",
// This is where confusion comes from. If you don't specify these things you will get single item select.
// These fields control the minimum and maximum amount of selected items.
MinValues: 1,
MaxValues: 3,
Options: []discordgo.SelectMenuOption{
{
Label: "Go",
Description: "Simple yet powerful programming language",
Value: "go",
// Default works the same for multi-select menus.
Default: false,
Emoji: discordgo.ComponentEmoji{
Name: "π¦¦",
},
},
{
Label: "JS",
Description: "Multiparadigm OOP language",
Value: "javascript",
Emoji: discordgo.ComponentEmoji{
Name: "π¨",
},
},
{
Label: "Python",
Description: "OOP prototyping programming language",
Value: "python",
Emoji: discordgo.ComponentEmoji{
Name: "π",
},
},
{
Label: "Web",
Description: "Web related technologies",
Value: "web",
Emoji: discordgo.ComponentEmoji{
Name: "π",
},
},
{
Label: "Desktop",
Description: "Desktop applications",
Value: "desktop",
Emoji: discordgo.ComponentEmoji{
Name: "π»",
},
},
},
},
},
},
},
},
}
}
err := s.InteractionRespond(i.Interaction, response)
if err != nil {
panic(err)
}
},
}
)
func main() {
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is up!")
})
// Components are part of interactions, so we register InteractionCreate handler
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
case discordgo.InteractionMessageComponent:
if h, ok := componentsHandlers[i.MessageComponentData().CustomID]; ok {
h(s, i)
}
}
})
_, err := s.ApplicationCommandCreate(*AppID, *GuildID, &discordgo.ApplicationCommand{
Name: "buttons",
Description: "Test the buttons if you got courage",
})
if err != nil {
log.Fatalf("Cannot create slash command: %v", err)
}
_, err = s.ApplicationCommandCreate(*AppID, *GuildID, &discordgo.ApplicationCommand{
Name: "selects",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "multi",
Description: "Multi-item select menu",
},
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "single",
Description: "Single-item select menu",
},
},
Description: "Lo and behold: dropdowns are coming",
})
if err != nil {
log.Fatalf("Cannot create slash command: %v", err)
}
err = s.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
defer s.Close()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Graceful shutdown")
}