-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
812 lines (715 loc) · 27.9 KB
/
handlers.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
package main
import (
"bytes"
"database/sql"
"errors"
"fmt"
"github.com/Necroforger/dgwidgets"
"github.com/bwmarrin/discordgo"
"log"
"os"
"sort"
"strings"
"text/tabwriter"
"time"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
// A map of command handlers for interactions.
var commandHandlers = map[string]func(session *discordgo.Session, interaction *discordgo.InteractionCreate){
"add_cards": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
// Getting all the files in the directory.
filesList, err := os.ReadDir("./cards")
if err != nil {
log.Printf("%vERROR%v - COULD NOT LIST CARDS: %v", Red, Reset, err)
return
}
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Now registering %d cards...", len(filesList)),
},
})
if err != nil {
return
}
for _, file := range filesList {
// Grabbing the image file path.
filePath := fmt.Sprintf("./cards/%v", file.Name())
// Reading the file into memory.
imageBytes, err := os.Open(filePath)
if err != nil {
log.Printf("%vERROR%v - COULD NOT READ IMAGE: %v", Red, Reset, err)
return
}
// Uploading that image to discord for saving.
msg, err := session.ChannelFileSend(interaction.ChannelID, file.Name(), imageBytes)
if err != nil {
log.Printf("%vERROR%v - COULD NOT UPLOAD IMAGE: %v", Red, Reset, err)
return
}
// Getting all the variables for the cards.
name := strings.ReplaceAll(file.Name(), ".png", "")
nameParts := strings.Split(name, " ")
log.Println(nameParts)
var character string
switch nameParts[0] {
case "SG01":
character = "Hibiki"
case "SG02":
character = "Tsubasa"
case "SG03":
character = "Chris"
case "SG04":
character = "Maria"
case "SG05":
character = "Shirabe"
case "SG06":
character = "Kirika"
case "SG07":
character = "Kanade"
case "SG08":
character = "Miku"
case "SG09":
character = "Serena"
case "SG10":
character = "Fine"
case "SG11":
character = "Dr. Ver"
case "SG12":
character = "Genjuro"
case "SG13":
character = "Ogawa"
case "SG14":
character = "St. Germain"
case "SG15":
character = "Cagliostro"
case "SG16":
character = "Prelati"
case "SG18":
character = "Adam"
case "SG19":
character = "Carol"
case "SG21":
character = "Phara"
case "SG22":
character = "Leiur"
case "SG23":
character = "Garie"
case "SG24":
character = "Micha"
case "SG25":
character = "Andou"
case "SG26":
character = "Shiori"
case "SG27":
character = "Yumi"
case "SG28":
character = "Vanessa"
case "SG29":
character = "Millaarc"
case "SG30":
character = "Elsa"
case "SG31":
character = "Shem-Ha"
case "SG32":
character = "Elfnein"
}
cardID := fmt.Sprintf("%v_%v", nameParts[0], nameParts[1])
evolution := nameParts[2]
cardImage := msg.Attachments[0].URL
// Creating a query to insert the cards into the card database.
query := fmt.Sprintf(`INSERT INTO cards(character, id, evolution, image) VALUES("%v", "%v", %v, "%v");`,
character, cardID, evolution, cardImage)
result, err := db.Exec(query)
if err != nil {
log.Printf("%vERROR%v - COULD NOT REGISTER CARD IN DATABASE: %v", Red, Reset, err)
return
}
log.Printf("%vSUCCESS%v - REGISTERED CARD IN CARD DATABASE: %v", Green, Reset, result)
time.Sleep(time.Millisecond * 10)
}
},
"register": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
authorID := interaction.Member.User.ID
if userIsRegistered(session, interaction) {
// Notify the user that they are already registerd.
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You're already registered to play!",
},
})
if err != nil {
return
}
} else {
// Creating a query to insert the user into the database with a phony unix timestamp and no credits.
query := fmt.Sprintf(`INSERT INTO users(id, timestamp, credits) VALUES("%s", 0, 10000);`,
authorID)
// Executing that query.
result, err := db.Exec(query)
if err != nil {
log.Printf("%vERROR%v - COULD NOT PLACE USER IN REGISTRATION DATABASE: %v", Red, Reset, err)
return
}
log.Printf("%vSUCCESS%v - PLACED USER INTO REGISTRATION DATABASE: %v", Green, Reset, result)
// Notify the user that they are now registered.
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You are now registered to play. Here's 10,000 credits to get you started!",
},
})
if err != nil {
return
}
}
},
"daily": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
authorID := interaction.Member.User.ID
currentTimestamp := time.Now().Unix()
var databaseTimestamp int64
var credits int64
if !userIsRegistered(session, interaction) {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Hey! You aren't registered to play yet! Remember to use the command `/register` before trying to play!",
},
})
if err != nil {
return
}
return
}
// Perform a single row query in the database to retrieve the timestamp.
query := fmt.Sprintf(`SELECT timestamp FROM users WHERE id = %s;`, authorID)
err := db.QueryRow(query).Scan(&databaseTimestamp)
if err != nil {
log.Printf("%vERROR%v - COULD NOT RETRIEVE USER'S TIMESTAMP FROM DATABASE:\n\t%v", Red, Reset, err)
return
}
// Checking to see if the user is on cooldown or if it is just an outdated entry.
if currentTimestamp >= databaseTimestamp+int64(86400) {
// It was an outdated entry, so we should give the user their reward and place them on cooldown again.
// Updating the timestamp in the database so that the user can't use the command again for a certain amount of time.
query = fmt.Sprintf(`UPDATE users SET timestamp = %v WHERE id = %v;`, currentTimestamp, authorID)
result, err := db.Exec(query)
if err != nil {
log.Printf("%vERROR%v - COULD NOT UPDATE UNIX TIMESTAMP IN DATABASE: %v", Red, Reset, err)
return
}
log.Printf("%vSUCCESS%v - UPDATED USER COOLDOWN: %v", Green, Reset, result)
// Snagging the amount of credits so that they can be updated.
query := fmt.Sprintf(`SELECT credits FROM users WHERE id = %v;`, authorID)
err = db.QueryRow(query).Scan(&credits)
if err != nil {
log.Printf("%vERROR%v - COULD NOT GET CREDITS OF USER IN DATABASE: %v", Red, Reset, err)
return
}
// Updating the amount of credits in the database for the user.
query = fmt.Sprintf(`UPDATE users SET credits = %v WHERE id = %v;`, credits+int64(100), authorID)
result, err = db.Exec(query)
if err != nil {
log.Printf("%vERROR%v - COULD NOT UPDATE CREDITS IN DATABASE: %v", Red, Reset, err)
return
}
log.Printf("%vSUCCESS%v - UPDATED USER CREDITS: %v", Green, Reset, result)
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Here's your daily reward of 100 credits!",
},
})
if err != nil {
return
}
} else {
// The user is actually on cooldown so we should let them know to comeback later.
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Come back on <t:%v:D> at <t:%v:T> to claim your daily reward!",
databaseTimestamp+int64(86400), databaseTimestamp+int64(86400)),
},
})
if err != nil {
return
}
}
},
"credits": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
var credits int64
authorID := interaction.Member.User.ID
printer := message.NewPrinter(language.English)
if !userIsRegistered(session, interaction) {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Hey! You aren't registered to play yet! Remember to use the command `/register` before trying to play!",
},
})
if err != nil {
return
}
return
}
// Perform a single row query to get the amount of credits a user has.
query := fmt.Sprintf(`SELECT credits FROM users WHERE id = %s;`, authorID)
err := db.QueryRow(query).Scan(&credits)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
log.Printf("%vERROR%v - COULD NOT RETRIEVE CREDITs FROM DATABASE:\n\t%v", Red, Reset, err)
return
}
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: printer.Sprintf("You currently have %d credits!", credits),
},
})
if err != nil {
return
}
},
"characters": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: strings.Join(charactersList(), ", "),
},
})
if err != nil {
return
}
},
"collection_list": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
authorID := interaction.Member.User.ID
var id string
var characterName string
var customName string
var evolution int8
var query string
// var webhookParams []discordgo.WebhookParams
var embeds []*discordgo.MessageEmbed
// Checking to make sure the user is registered.
if !userIsRegistered(session, interaction) {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Hey! You aren't registered to play yet! Remember to use the command `/register` before trying to play!",
},
})
if err != nil {
return
}
return
}
// I don't even know at this point. Check whether a character is specified or something.
if len(interaction.ApplicationCommandData().Options) == 0 {
query = fmt.Sprintf(`SELECT card_id, character, custom_name, evolution FROM collections WHERE user_id = %v;`, authorID)
} else {
if inArray(strings.Title(interaction.ApplicationCommandData().Options[0].StringValue()), charactersList()) {
query = fmt.Sprintf(`SELECT card_id, character, custom_name, evolution FROM collections WHERE user_id = %v AND character_name = "%v";`,
authorID, interaction.ApplicationCommandData().Options[0].StringValue())
} else {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "I couldn't find a character with that name.",
},
})
if err != nil {
return
}
}
}
// Executing the query.
rows, err := db.Query(query)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
log.Printf("%vERROR%v - COULD NOT RETRIEVE CARDS FROM DATABASE:\n\t%v", Red, Reset, err)
return
}
// Creating a struct to hold query results.
type Card struct {
id string
characterName string
customName string
evolution int8
}
// Making an slice of card structs to hold results.
var cards []Card
// Iterating over the results and appending to an array of cards.
for rows.Next() {
err := rows.Scan(&id, &characterName, &customName, &evolution)
if err != nil {
log.Printf("%vERROR%v - COULD NOT RETRIEVE CHARACTER FROM ROW:\n\t%v", Red, Reset, err)
return
}
var card Card
card.id = id
card.characterName = characterName
card.customName = customName
card.evolution = evolution
cards = append(cards, card)
}
if len(cards) == 0 {
// If there were no rows returned, let the user know that they don't have any cards.
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "It looks like you don't have any cards that match that criteria!",
},
})
if err != nil {
return
}
return
}
// Sorting the slice.
sort.SliceStable(cards[:], func(i, j int) bool {
return cards[i].characterName < cards[j].characterName
})
// SUPER funky shit to chop up array.
var chunkedCards [][]Card
chunkSize := 25
for i := 0; i < len(cards); i += chunkSize {
end := i + chunkSize
if end > len(cards) {
end = len(cards)
}
chunkedCards = append(chunkedCards, cards[i:end])
}
// Printing the results to the user. Need to clean it up...
for _, values := range chunkedCards {
buffer := new(bytes.Buffer)
writer := tabwriter.NewWriter(buffer, 0, 0, 4, ' ', 0)
fmt.Fprintln(writer, "Character:\tCard Name:\tEvolution:")
for _, value := range values {
// content += fmt.Sprintf("%-10s\t%12s\n", value.characterName, value.customName)
_, err := fmt.Fprintf(writer, "%v\t%v\t%v\n", value.characterName, value.customName, value.evolution)
if err != nil {
log.Println(err)
}
}
writer.Flush()
content := "```" + buffer.String() + "```"
embeds = append(embeds, &discordgo.MessageEmbed{
Description: content,
})
}
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Now listing cards...",
},
})
if err != nil {
return
}
paginator := dgwidgets.NewPaginator(session, interaction.ChannelID)
for _, embed := range embeds {
paginator.Add(embed)
}
paginator.SetPageFooters()
paginator.Widget.Timeout = time.Minute * 5
paginator.Widget.UserWhitelist = append(paginator.Widget.UserWhitelist, authorID)
paginator.Spawn()
},
"collection_amount": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
var query string
var userAmount int64
var totalAmount int64
authorID := interaction.Member.User.ID
printer := message.NewPrinter(language.English)
// Checking to make sure the user is registered.
if !userIsRegistered(session, interaction) {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Hey! You aren't registered to play yet! Remember to use the command `/register` before trying to play!",
},
})
if err != nil {
return
}
return
}
// I don't even know at this point. Check whether a character is specified or something.
if len(interaction.ApplicationCommandData().Options) == 0 {
query = fmt.Sprintf(`SELECT COUNT(DISTINCT card_id) FROM collections WHERE user_id = %v;`, authorID)
} else {
if inArray(strings.Title(interaction.ApplicationCommandData().Options[0].StringValue()), charactersList()) {
query = fmt.Sprintf(`SELECT COUNT(DISTINCT card_id) FROM collections WHERE user_id = %v AND character = "%v";`,
authorID, strings.Title(interaction.ApplicationCommandData().Options[0].StringValue()))
} else {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "I couldn't find a character with that name.",
},
})
if err != nil {
return
}
}
}
err := db.QueryRow(query).Scan(&userAmount)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
log.Printf("%vERROR%v - COULD NOT RETRIEVE AMOUNT OF CARDS FROM DATABASE:\n\t%v", Red, Reset, err)
return
}
// Grabbing the amount of those total cards in the cards table.
if len(interaction.ApplicationCommandData().Options) == 0 {
query = `SELECT COUNT(DISTINCT card_id) FROM cards;`
} else {
query = fmt.Sprintf(`SELECT COUNT(DISTINCT card_id) FROM cards WHERE character = "%v";`,
interaction.ApplicationCommandData().Options[0].StringValue())
}
err = db.QueryRow(query).Scan(&totalAmount)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
log.Printf("%vERROR%v - COULD NOT RETRIEVE AMOUNT OF CARDS FROM DATABASE:\n\t%v", Red, Reset, err)
return
}
if len(interaction.ApplicationCommandData().Options) == 0 {
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: printer.Sprintf("You have currently collected %d out of %d cards!", userAmount, totalAmount),
},
})
if err != nil {
return
}
} else {
err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: printer.Sprintf("You have currently collected %d out of %d cards of %s!",
userAmount, totalAmount, strings.Title(interaction.ApplicationCommandData().Options[0].StringValue())),
},
})
if err != nil {
return
}
}
},
"single_pull": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
authorID := interaction.Member.User.ID
// Checking to make sure the user is registered.
if !userIsRegistered(session, interaction) {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Hey! You aren't registered to play yet! Remember to use the command `/register` before trying to play!",
},
})
if err != nil {
return
}
return
}
// Snagging the amount of credits so that they can be checked against.
credits := getCredits(authorID)
// Making sure the user has the correct amount of credits.
if credits >= int64(200) {
// Checking to see if the user specified a character to pull for.
if len(interaction.ApplicationCommandData().Options) != 0 {
// They did, so we need to check if that that character is available to pull from.
if inArray(strings.Title(interaction.ApplicationCommandData().Options[0].StringValue()), charactersList()) {
// Updating the amount of credits in the database for the user.
updateCredits(-200, authorID)
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
//Content: fmt.Sprintf("Successfully added %v to your collection. You can rename this card at anytime by using `/rename [original_name] [new_name]", drawnCardID),
Content: "I've deducted 200 credits from your wallet, let's see what you drew!",
},
})
if err != nil {
return
}
time.Sleep(time.Second / 10)
webhook := pullCard(session, interaction)
_, err = session.FollowupMessageCreate(interaction.Interaction, true, &webhook)
if err != nil {
return
}
} else {
// Could not find a character pool with that name.
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
//Content: fmt.Sprintf("Successfully added %v to your collection. You can rename this card at anytime by using `/rename [original_name] [new_name]", drawnCardID),
Content: "I couldn't find a character pool with that name.",
},
})
if err != nil {
return
}
}
} else {
// The player did not specify a character to draw from.
// Updating the amount of credits in the database for the user.
updateCredits(-200, authorID)
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
//Content: fmt.Sprintf("Successfully added %v to your collection. You can rename this card at anytime by using `/rename [original_name] [new_name]", drawnCardID),
Content: "I've deducted 200 credits from your wallet, let's see what you drew!",
},
})
if err != nil {
return
}
//time.Sleep(time.Second / 10)
webhook := pullCard(session, interaction)
_, err = session.FollowupMessageCreate(interaction.Interaction, true, &webhook)
if err != nil {
return
}
}
} else {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You do not have enough credits to draw a card.",
},
})
if err != nil {
return
}
}
},
"ten_pull": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
authorID := interaction.Member.User.ID
// Checking to make sure the user is registered.
if !userIsRegistered(session, interaction) {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Hey! You aren't registered to play yet! Remember to use the command `/register` before trying to play!",
},
})
if err != nil {
return
}
return
}
// Snagging the amount of credits so they can be checked against.
credits := getCredits(authorID)
// Making sure the user has the correct amount of credits.
if credits >= int64(1800) {
// Checking to see if the user specified a character to pull for.
if len(interaction.ApplicationCommandData().Options) != 0 {
// They did, so we need to check if that that character is available to pull from.
if inArray(strings.Title(interaction.ApplicationCommandData().Options[0].StringValue()), charactersList()) {
// Updating the amount of credits in the database for the user.
updateCredits(-1800, authorID)
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
//Content: fmt.Sprintf("Successfully added %v to your collection. You can rename this card at anytime by using `/rename [original_name] [new_name]", drawnCardID),
Content: "I've deducted 1,800 credits from your wallet, let's see what you drew!",
},
})
if err != nil {
return
}
// Conducting the ten pull.
var webhookParams []discordgo.WebhookParams
for i := 0; i < 10; i++ {
webhook := pullCard(session, interaction)
webhookParams = append(webhookParams, webhook)
}
paginator := dgwidgets.NewPaginator(session, interaction.ChannelID)
for _, webhook := range webhookParams {
paginator.Add(webhook.Embeds[0])
}
paginator.SetPageFooters()
paginator.Widget.Timeout = time.Minute * 5
paginator.Widget.UserWhitelist = append(paginator.Widget.UserWhitelist, authorID)
err = paginator.Spawn()
if err != nil {
return
}
} else {
// Could not find a character pool with that name.
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
//Content: fmt.Sprintf("Successfully added %v to your collection. You can rename this card at anytime by using `/rename [original_name] [new_name]", drawnCardID),
Content: "I couldn't find a character pool with that name.",
},
})
if err != nil {
return
}
}
} else {
// The user did not specify a character to pull for.
// Updating the amount of credits in the database for the user.
updateCredits(-1800, authorID)
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
//Content: fmt.Sprintf("Successfully added %v to your collection. You can rename this card at anytime by using `/rename [original_name] [new_name]", drawnCardID),
Content: "I've deducted 1,800 credits from your wallet, let's see what you drew!",
},
})
if err != nil {
return
}
// Conducting the ten pull.
var webhookParams []discordgo.WebhookParams
for i := 0; i < 10; i++ {
webhook := pullCard(session, interaction)
webhookParams = append(webhookParams, webhook)
}
paginator := dgwidgets.NewPaginator(session, interaction.ChannelID)
for _, webhook := range webhookParams {
paginator.Add(webhook.Embeds[0])
}
paginator.SetPageFooters()
paginator.Widget.Timeout = time.Minute * 5
paginator.Widget.UserWhitelist = append(paginator.Widget.UserWhitelist, authorID)
err = paginator.Spawn()
if err != nil {
return
}
}
} else {
// https: //pkg.go.dev/github.com/bwmarrin/discordgo#Session.InteractionRespond
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You do not have enough credits to draw a card.",
},
})
if err != nil {
return
}
}
},
}