-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.go
853 lines (764 loc) · 26.4 KB
/
commands.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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
package main
import (
"bytes"
"database/sql"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/lit"
"github.com/goccy/go-json"
"github.com/mb-14/gomarkov"
"github.com/psykhi/wordclouds"
"image/color"
"image/png"
"regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
)
var (
// Commands
commands = []*discordgo.ApplicationCommand{
{
Name: "characters",
Description: "Prints the number of characters sent for a channel, or the entire server if omitted",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Optional channel to get stats for",
Required: false,
},
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "includebot",
Description: "Whether to include or not message from bots",
Required: false,
},
},
},
{
Name: "words",
Description: "Prints the number of words sent for a channel, or the entire server if omitted",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Optional channel to get stats for",
Required: false,
},
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "includebot",
Description: "Whether to include or not message from bots",
Required: false,
},
},
},
{
Name: "messages",
Description: "Prints the number of messages sent for a channel, or the entire server if omitted",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Optional channel to get stats for",
Required: false,
},
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "includebot",
Description: "Whether to include or not message from bots",
Required: false,
},
},
},
{
Name: "charspermex",
Description: "Prints the number of characters per message sent for a channel, or the entire server if omitted",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Optional channel to get stats for",
Required: false,
},
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "includebot",
Description: "Whether to include or not message from bots",
Required: false,
},
},
},
{
Name: "wordcloud",
Description: "Generates a word cloud for a channel, or the entire server if omitted",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Optional channel to get stats for",
Required: false,
},
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "includebot",
Description: "Whether to include or not message from bots",
Required: false,
},
},
},
{
Name: "undelete",
Description: "Recovers the last n delete messages from the current channel",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "number",
Description: "How many messages to recover",
Required: false,
},
},
},
{
Name: "markov",
Description: "Generates a message from the current markov chain",
},
{
Name: "longest",
Description: "Links the longest messages",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Optional channel for the stat",
Required: false,
},
},
},
{
Name: "poll",
Description: "Creates a poll",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "question",
Description: "The question to ask",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "group",
Description: "A group of people to poll",
Required: true,
},
},
},
{
Name: "creategroup",
Description: "Creates a group of people to poll",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "name",
Description: "The name of the group",
Required: true,
},
},
},
{
Name: "deletegroup",
Description: "Deletes a group of people to poll",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "name",
Description: "The name of the group",
Required: true,
},
},
},
{
Name: "addmember",
Description: "Adds a member to a group",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "group",
Description: "The selected group",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionUser,
Name: "member",
Description: "The member to add",
Required: true,
},
},
},
{
Name: "removemember",
Description: "Removes a member to a group",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "group",
Description: "The selected group",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionUser,
Name: "member",
Description: "The member to remove",
Required: true,
},
},
},
}
// Handler
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}){
// Prints the number of characters sent for a given channel, or if not specified for the entire server
"characters": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var (
mex *sql.Rows
err error
messageJSON []byte
toSend string
m LightMessage
cont int
characters = make(map[string]int)
people = make(map[string]string)
channel string
bot bool
)
for _, o := range i.ApplicationCommandData().Options {
switch o.Type {
case discordgo.ApplicationCommandOptionChannel:
channel = o.ChannelValue(nil).ID
case discordgo.ApplicationCommandOptionBoolean:
bot = o.BoolValue()
}
}
// If there's a specified channel, use it in the query
if channel != "" {
mex, err = db.Query("SELECT message FROM messages WHERE channelID=?", channel)
} else {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=?", i.GuildID)
}
if err != nil {
lit.Error("Can't query database, %s", err)
return
}
for mex.Next() {
err = mex.Scan(&messageJSON)
if err != nil {
lit.Error("Can't scan m, %s", err)
return
}
err = json.Unmarshal(messageJSON, &m)
if err != nil {
lit.Error("Can't unmarshal JSON, %s", err)
continue
}
if m.Author.ID != "" {
if bot {
characters[m.Author.ID] += len(m.Content)
people[m.Author.ID] = m.Author.Username
} else {
if !m.Author.Bot {
characters[m.Author.ID] += len(m.Content)
people[m.Author.ID] = m.Author.Username
}
}
}
}
// Characters
for i, kv := range sorting(characters) {
cont += kv.Value
toSend += fmt.Sprintf("%d) %s: %d\n", i+1, people[kv.Key], kv.Value)
}
toSend = fmt.Sprintf("Number of characters sent: %d\n\n", cont) + toSend
sendEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Characters", toSend).
SetColor(0x7289DA).MessageEmbed, i.Interaction, c)
},
// Prints the number of words sent for a given channel, or if not specified for the entire server
"words": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var (
mex *sql.Rows
err error
messageJSON []byte
toSend string
m LightMessage
cont int
words = make(map[string]int)
people = make(map[string]string)
// Match non-space character sequences.
re = regexp.MustCompile(`\S+`)
channel string
bot bool
)
for _, o := range i.ApplicationCommandData().Options {
switch o.Type {
case discordgo.ApplicationCommandOptionChannel:
channel = o.ChannelValue(nil).ID
case discordgo.ApplicationCommandOptionBoolean:
bot = o.BoolValue()
}
}
// If there's a specified channel, use it in the query
if channel != "" {
mex, err = db.Query("SELECT message FROM messages WHERE channelID=?", channel)
} else {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=?", i.GuildID)
}
if err != nil {
lit.Error("Can't query database, %s", err)
return
}
for mex.Next() {
err = mex.Scan(&messageJSON)
if err != nil {
lit.Error("Can't scan m, %s", err)
return
}
err = json.Unmarshal(messageJSON, &m)
if err != nil {
lit.Error("Can't unmarshal JSON, %s", err)
continue
}
if m.Author.ID != "" {
if bot {
people[m.Author.ID] = m.Author.Username
words[m.Author.ID] += len(re.FindAllString(m.Content, -1))
} else {
if !m.Author.Bot {
people[m.Author.ID] = m.Author.Username
words[m.Author.ID] += len(re.FindAllString(m.Content, -1))
}
}
}
}
// Words
for i, kv := range sorting(words) {
cont += kv.Value
toSend += fmt.Sprintf("%d) %s: %d\n", i+1, people[kv.Key], kv.Value)
}
toSend = fmt.Sprintf("Number of words: %d\n\n", cont) + toSend
sendEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Words", toSend).
SetColor(0x7289DA).MessageEmbed, i.Interaction, c)
},
// Prints the number of messages sent for a given channel, or if not specified for the entire server
"messages": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var (
mex *sql.Rows
err error
messageJSON []byte
toSend string
m LightMessage
cont int
people = make(map[string]string)
messages = make(map[string]int)
channel string
bot bool
)
for _, o := range i.ApplicationCommandData().Options {
switch o.Type {
case discordgo.ApplicationCommandOptionChannel:
channel = o.ChannelValue(nil).ID
case discordgo.ApplicationCommandOptionBoolean:
bot = o.BoolValue()
}
}
// If there's a specified channel, use it in the query
if channel != "" {
mex, err = db.Query("SELECT message FROM messages WHERE channelID=?", channel)
} else {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=?", i.GuildID)
}
if err != nil {
lit.Error("Can't query database, %s", err)
return
}
for mex.Next() {
err = mex.Scan(&messageJSON)
if err != nil {
lit.Error("Can't scan m, %s", err)
return
}
err = json.Unmarshal(messageJSON, &m)
if err != nil {
lit.Error("Can't unmarshal JSON, %s", err)
continue
}
if m.Author.ID != "" {
if bot {
people[m.Author.ID] = m.Author.Username
messages[m.Author.ID]++
} else {
if !m.Author.Bot {
people[m.Author.ID] = m.Author.Username
messages[m.Author.ID]++
}
}
}
}
// Messages
for i, kv := range sorting(messages) {
cont += kv.Value
toSend += fmt.Sprintf("%d) %s: %d\n", i+1, people[kv.Key], kv.Value)
}
toSend = fmt.Sprintf("Number of messages: %d\n\n", cont) + toSend
sendEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Messages", toSend).
SetColor(0x7289DA).MessageEmbed, i.Interaction, c)
},
// Prints stats for a given channel, or if not specified for the entire server.
"charspermex": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var (
mex *sql.Rows
err error
messageJSON []byte
toSend string
m LightMessage
cont int
characters = make(map[string]int)
people = make(map[string]string)
messages = make(map[string]int)
charPerMex = make(map[string]int)
channel string
bot bool
)
for _, o := range i.ApplicationCommandData().Options {
switch o.Type {
case discordgo.ApplicationCommandOptionChannel:
channel = o.ChannelValue(nil).ID
case discordgo.ApplicationCommandOptionBoolean:
bot = o.BoolValue()
}
}
// If there's a specified channel, use it in the query
if channel != "" {
mex, err = db.Query("SELECT message FROM messages WHERE channelID=?", channel)
} else {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=?", i.GuildID)
}
if err != nil {
lit.Error("Can't query database, %s", err)
return
}
for mex.Next() {
err = mex.Scan(&messageJSON)
if err != nil {
lit.Error("Can't scan m, %s", err)
return
}
err = json.Unmarshal(messageJSON, &m)
if err != nil {
lit.Error("Can't unmarshal JSON, %s", err)
continue
}
if m.Author.ID != "" {
if bot {
characters[m.Author.ID] += len(m.Content)
people[m.Author.ID] = m.Author.Username
messages[m.Author.ID]++
} else {
if !m.Author.Bot {
characters[m.Author.ID] += len(m.Content)
people[m.Author.ID] = m.Author.Username
messages[m.Author.ID]++
}
}
}
}
// Characters
for k, v := range characters {
charPerMex[k] = v / messages[k]
}
for i, kv := range sorting(charPerMex) {
cont += kv.Value
toSend += fmt.Sprintf("%d) %s: %d\n", i+1, people[kv.Key], kv.Value)
}
toSend = fmt.Sprintf("Number of characters per message sent: %d\n\n", cont) + toSend
sendEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Characters per message", toSend).
SetColor(0x7289DA).MessageEmbed, i.Interaction, c)
},
// Generates a word cloud for a channel, or the entire server if omitted
"wordcloud": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var (
mex *sql.Rows
err error
messageJSON []byte
m LightMessage
words = make(map[string]int)
channel string
bot bool
)
for _, o := range i.ApplicationCommandData().Options {
switch o.Type {
case discordgo.ApplicationCommandOptionChannel:
channel = o.ChannelValue(nil).ID
case discordgo.ApplicationCommandOptionBoolean:
bot = o.BoolValue()
}
}
// If there's a specified channel, use it in the query
if channel != "" {
mex, err = db.Query("SELECT message FROM messages WHERE channelID=?", channel)
} else {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=?", i.GuildID)
}
if err != nil {
lit.Error("Can't query database, %s", err)
return
}
for mex.Next() {
err = mex.Scan(&messageJSON)
if err != nil {
lit.Error("Can't scan m, %s", err)
continue
}
err = json.Unmarshal(messageJSON, &m)
if err != nil {
lit.Error("Can't unmarshal JSON, %s", err)
continue
}
if bot {
mSplitted := strings.Fields(strings.ToLower(m.Content))
for _, word := range mSplitted {
if utf8.RuneCountInString(word) > 3 {
words[word]++
}
}
} else {
if m.Author.ID != "" && !m.Author.Bot {
mSplitted := strings.Fields(strings.ToLower(m.Content))
for _, word := range mSplitted {
if utf8.RuneCountInString(word) > 3 {
words[word]++
}
}
}
}
}
w := wordclouds.NewWordcloud(
words,
wordclouds.FontFile("./fonts/Roboto-Regular.ttf"),
wordclouds.Height(2048),
wordclouds.Width(2048),
wordclouds.Colors([]color.Color{color.RGBA{R: 247, G: 144, B: 30, A: 255}, color.RGBA{R: 194, G: 69, B: 39, A: 255}, color.RGBA{R: 38, G: 103, B: 118, A: 255}, color.RGBA{R: 173, G: 210, B: 224, A: 255}}),
)
var imgPng bytes.Buffer
// Draws image
img := w.Draw()
// Encodes it
_ = png.Encode(&imgPng, img)
// Sends it
<-c
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Files: []*discordgo.File{
{
Name: "wordcloud.png",
ContentType: "image/png",
Reader: &imgPng,
},
},
})
},
"undelete": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var (
number uint
m discordgo.Message
messageJSON []byte
toSend string
toAdd string
)
if len(i.ApplicationCommandData().Options) > 0 {
number = uint(i.ApplicationCommandData().Options[0].UintValue())
} else {
// Default value
number = 3
}
toSend = "Last " + strconv.Itoa(int(number)) + " deleted messages:\n```"
rows, err := db.Query("SELECT message FROM messages WHERE deleted = 1 AND channelID=? ORDER BY JSON_VALUE(message, '$.timestamp') DESC LIMIT ?", i.ChannelID, number)
if err != nil {
lit.Error("Can't query database, %s", err)
return
}
for rows.Next() {
toAdd = ""
err = rows.Scan(&messageJSON)
if err != nil {
lit.Error("Can't scan m, %s", err)
continue
}
err = json.Unmarshal(messageJSON, &m)
if err != nil {
lit.Error("Can't unmarshal JSON, %s", err)
continue
}
for _, a := range m.Attachments {
toAdd += a.ID + "\n"
}
for _, e := range m.Embeds {
for _, f := range e.Fields {
toAdd += f.Name + ": " + f.Value + "\n"
}
}
if m.Author != nil {
toSend += m.Author.Username
}
toSend += ": " + toAdd + m.Content + "\n"
}
sendEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Undelete", strings.TrimSuffix(toSend, "\n")+"```").
SetColor(0x7289DA).MessageEmbed, i.Interaction, c)
},
"markov": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
tokens := []string{gomarkov.StartToken}
for tokens[len(tokens)-1] != gomarkov.EndToken {
next, _ := server[i.GuildID].model.Generate(tokens[(len(tokens) - 1):])
tokens = append(tokens, next)
}
sendEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Markov", strings.Join(tokens[1:len(tokens)-1], " ")).
SetColor(0x7289DA).MessageEmbed, i.Interaction, c)
},
"longest": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var (
rows *sql.Rows
data []byte
channelID, messageID string
m LightMessage
cont = 1
highest int
err error
embed = NewEmbed().SetTitle(s.State.User.Username).SetColor(0x7289DA)
)
if len(i.ApplicationCommandData().Options) > 0 {
rows, err = db.Query("SELECT message, channelID, messageID FROM messages WHERE channelID=? AND deleted=0 ORDER BY length(JSON_VALUE(message, '$.content')) DESC LIMIT 10", i.ApplicationCommandData().Options[0].ChannelValue(nil).ID)
} else {
rows, err = db.Query("SELECT message, channelID, messageID FROM messages WHERE guildID=? AND deleted=0 ORDER BY length(JSON_VALUE(message, '$.content')) DESC LIMIT 10", i.GuildID)
}
if err != nil {
lit.Error("Error querying database: %s", err.Error())
return
}
for rows.Next() {
err = rows.Scan(&data, &channelID, &messageID)
if err == nil {
_ = json.Unmarshal(data, &m)
if len(m.Content) < 100 {
highest = len(m.Content)
} else {
highest = 100
}
embed.AddField(strconv.Itoa(cont), fmt.Sprintf("[%s](https://discord.com/channels/%s/%s/%s) - by %s\n", m.Content[0:highest], i.GuildID, channelID, messageID, m.Author.Username))
cont++
}
}
sendEmbedInteraction(s, embed.MessageEmbed, i.Interaction, c)
},
"poll": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var n int
question := i.ApplicationCommandData().Options[0].StringValue()
group := i.ApplicationCommandData().Options[1].StringValue()
// If the group doesn't exist, just quit
_ = db.QueryRow("SELECT COUNT(*) FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, group).Scan(&n)
if n == 0 {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group not found!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
return
}
// Create the poll message
msg, _ := s.ChannelMessageSendEmbed(i.ChannelID, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", question).MessageEmbed)
// Adds the message to the db
_, _ = db.Exec("INSERT INTO pollState (messageID, question, groupName, guildID) VALUES (?, ?, ?, ?)", msg.ID, question, group, i.GuildID)
// And the map
server[i.GuildID].polls[msg.ID] = true
// Add the reactions
_ = s.MessageReactionAdd(msg.ChannelID, msg.ID, "👍")
_ = s.MessageReactionAdd(msg.ChannelID, msg.ID, "👎")
},
"creategroup": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var n int
_ = db.QueryRow("SELECT COUNT(*) FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, i.ApplicationCommandData().Options[0].StringValue()).Scan(&n)
if n == 0 {
_, _ = db.Exec("INSERT INTO pollsGroup (serverID, name, createdBy) VALUES (?, ?, ?)", i.GuildID, i.ApplicationCommandData().Options[0].StringValue(), i.Member.User.ID)
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group created!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
} else {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group already exists!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
}
},
"deletegroup": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var userID string
_ = db.QueryRow("SELECT createdBy FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, i.ApplicationCommandData().Options[0].StringValue()).Scan(&userID)
if userID == "" {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group not found!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
}
if userID == i.Member.User.ID {
_, _ = db.Exec("DELETE FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, i.ApplicationCommandData().Options[0].StringValue())
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group deleted!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
} else {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "You are not the owner of this group!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
}
},
// Adds a member to a group. Only the creator of the group can add people
"addmember": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var createdBy, userIDs string
_ = db.QueryRow("SELECT createdBy, userIDs FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, i.ApplicationCommandData().Options[0].StringValue()).Scan(&createdBy, &userIDs)
if createdBy == "" {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group not found!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
}
if createdBy == i.Member.User.ID {
user := i.ApplicationCommandData().Options[1].UserValue(s)
// If the user is already in the group, just return
if strings.Contains(userIDs, user.ID) {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "User already in the group!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
return
}
// Gets the old members, and adds the new one
if userIDs == "" {
userIDs = user.ID
} else {
userIDs += "," + user.ID
}
_, _ = db.Exec("UPDATE pollsGroup SET userIDs=? WHERE serverID=? AND name=?", userIDs, i.GuildID, i.ApplicationCommandData().Options[0].StringValue())
// Adds the nickname to the database
_, _ = db.Exec("INSERT IGNORE INTO users (id, nickname) VALUES (?, ?)", user.ID, user.Username)
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Member added!").MessageEmbed, i.Interaction, time.Second*3, c)
} else {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "You are not the owner of this group!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
}
},
// Removes a member from a group. Only the creator of the group can remove people
"removemember": func(s *discordgo.Session, i *discordgo.InteractionCreate, c chan struct{}) {
var createdBy, userIDs string
_ = db.QueryRow("SELECT createdBy, userIDs FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, i.ApplicationCommandData().Options[0].StringValue()).Scan(&createdBy, &userIDs)
if createdBy == "" {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group not found!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
}
if createdBy == i.Member.User.ID {
// If the user is not in the group, just return
if !strings.Contains(userIDs, i.ApplicationCommandData().Options[1].UserValue(s).ID) {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "User not in the group!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
return
}
// Gets the old members, and adds the new one
userIDs = strings.Replace(userIDs, ","+i.ApplicationCommandData().Options[1].UserValue(nil).ID, "", -1)
_, _ = db.Exec("UPDATE pollsGroup SET userIDs=? WHERE serverID=? AND name=?", userIDs, i.GuildID, i.ApplicationCommandData().Options[0].StringValue())
} else {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "You are not the owner of this group!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3, c)
}
},
}
)