-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_handler.go
780 lines (656 loc) · 19.1 KB
/
command_handler.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
package main
/*
Our interface to our command registry
*/
import (
"fmt"
"github.com/bwmarrin/discordgo"
"os"
"strconv"
)
// CommandHandler struct
type CommandHandler struct {
callback *CallbackHandler
conf *Config
db *DBHandler
perm *PermissionsHandler
registry *CommandRegistry
dg *discordgo.Session
user *UserHandler
ch *ChannelHandler
logchan chan string
}
// Init function
func (h *CommandHandler) Init(channelhandler *ChannelHandler) {
// Setup our Channel Handler
h.ch = channelhandler
// Setup our command registry interface
h.registry = new(CommandRegistry)
h.registry.conf = h.conf
pagecount := h.conf.MainConfig.PerPageCount
if pagecount < 2 {
count := strconv.Itoa(pagecount)
fmt.Println("Invalid Config Parameter Setting: [main]:per_page_count must be 2 or higher - Found " + count)
os.Exit(0)
}
h.registry.db = h.db
h.registry.user = h.user
}
// Read function
func (h *CommandHandler) Read(s *discordgo.Session, m *discordgo.MessageCreate) {
// Check for safety
if !SafeInput(s, m, h.conf) {
return
}
user, err := h.user.GetUser(m.Author.ID, s, m.ChannelID)
if err != nil {
return
}
if !user.CheckRole("admin") {
return
}
command, message := CleanCommand(m.Content, h.conf)
if command == "command" {
h.ReadCommand(message, s, m)
}
}
// ReadCommand function
func (h *CommandHandler) ReadCommand(message []string, s *discordgo.Session, m *discordgo.MessageCreate) {
if len(message) < 1 {
s.ChannelMessageSend(m.ChannelID, "<command> requires an argument")
return
}
command := message[0]
payload := RemoveStringFromSlice(message, command)
if command == "enable" {
if len(payload) < 1 {
s.ChannelMessageSend(m.ChannelID, "<enable> requires at least one argument")
return
}
h.EnableCommand(payload, s, m)
return
}
if command == "disable" {
if len(payload) < 1 {
s.ChannelMessageSend(m.ChannelID, "<disable> requires at least one argument")
return
}
h.DisableCommand(payload, s, m)
return
}
//TODO
if command == "groups" {
if len(payload) < 1 {
s.ChannelMessageSend(m.ChannelID, "<group> requires at least one argument")
return
}
h.ReadGroups(payload, s, m)
return
}
if command == "roles" {
if len(payload) < 1 {
s.ChannelMessageSend(m.ChannelID, "<roles> requires at least one argument")
return
}
h.ReadRoles(payload, s, m)
return
}
if command == "users" {
if len(payload) < 1 {
s.ChannelMessageSend(m.ChannelID, "<usermanager> requires at least one argument")
return
}
h.ReadUsers(payload, s, m)
return
}
if command == "channels" {
if len(payload) < 1 {
s.ChannelMessageSend(m.ChannelID, "<channels> requires at least one argument")
return
}
h.ReadChannels(payload, s, m)
return
}
if command == "usage" {
if len(message) < 2 {
s.ChannelMessageSend(m.ChannelID, "<usage> requires at least one argument")
return
}
h.DisplayUsage(payload, s, m)
return
}
if command == "description" {
if len(message) < 2 {
s.ChannelMessageSend(m.ChannelID, "<description> requires at least one argument")
return
}
h.DisplayDescription(payload, s, m)
return
}
if command == "list" {
h.ReadList(message, s, m)
return
}
}
// ReadList function
func (h *CommandHandler) ReadList(message []string, s *discordgo.Session, m *discordgo.MessageCreate) {
// list
if len(message) < 2 {
h.ListCommands(m.ChannelID, 0, s, m)
return
}
// list <number> || list <channel>
if len(message) > 1 {
// if number by itself
page, err := strconv.Atoi(message[0])
if err == nil {
h.ListCommands(m.ChannelID, page, s, m)
return
}
// Check for channel as argument
channelid := CleanChannel(message[0])
_, err = s.Channel(channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Invalid inputs provided")
return
}
h.ch.channeldb.CreateIfNotExists(channelid)
if len(message) == 2 {
// If channel argument and no page number supplied
h.ListCommands(channelid, 0, s, m)
}
if len(message) > 2 {
// Check to see if second argument is a number
page, err := strconv.Atoi(message[2])
if err == nil {
h.ListCommands(channelid, page, s, m)
return
}
h.ListCommands(channelid, page, s, m)
}
return
}
}
// ReadGroups function
func (h *CommandHandler) ReadGroups(message []string, s *discordgo.Session, m *discordgo.MessageCreate) {
command := message[0]
payload := RemoveStringFromSlice(message, command)
if len(payload) < 1 {
s.ChannelMessageSend(m.ChannelID, command+" requires an argument")
return
}
// list
if command == "list" {
groups, err := h.registry.GetGroups(payload[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
if len(groups) < 1 {
s.ChannelMessageSend(m.ChannelID, ":\nNo Group assignments found for "+payload[0])
return
}
var formatted string
for i, group := range groups {
if i == len(groups)-1 {
formatted = formatted + group
} else {
formatted = formatted + group + ", "
}
}
s.ChannelMessageSend(m.ChannelID, ":\nGroups for "+payload[0]+" : "+formatted)
return
}
// add
if command == "add" {
if len(payload) < 2 {
s.ChannelMessageSend(m.ChannelID, command+" requires two arguments <group> <command>")
return
}
err := h.registry.AddGroup(payload[1], payload[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, payload[1]+" has been added to the "+payload[0]+" group.")
return
}
// remove
if command == "remove" {
if len(payload) < 2 {
s.ChannelMessageSend(m.ChannelID, command+" requires two arguments <group> <command>")
return
}
err := h.registry.RemoveGroup(payload[1], payload[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, payload[1]+" has been removed from the "+payload[0]+" group.")
return
}
}
// ReadRoles function
func (h *CommandHandler) ReadRoles(message []string, s *discordgo.Session, m *discordgo.MessageCreate) {
command := message[0]
payload := RemoveStringFromSlice(message, command)
if len(payload) < 1 {
s.ChannelMessageSend(m.ChannelID, command+" requires an argument")
return
}
// list
if command == "list" {
roles, err := h.registry.GetRoles(payload[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
if len(roles) < 1 {
s.ChannelMessageSend(m.ChannelID, ":\nNo Role assignments found for "+payload[0])
return
}
var formatted string
for i, role := range roles {
if i == len(roles)-1 {
formatted = formatted + role
} else {
formatted = formatted + role + ", "
}
}
s.ChannelMessageSend(m.ChannelID, ":\nRoles for "+payload[0]+" : "+formatted)
return
}
// add
if command == "add" {
if len(payload) < 2 {
s.ChannelMessageSend(m.ChannelID, command+" requires two arguments <role> <command>")
return
}
guildID, err := getGuildID(s, m.ChannelID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error retrieving GuildID: "+err.Error())
return
}
// Checks if Role Exists in Guild
_, err = getRoleIDByName(s, guildID, payload[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error retrieving roleID: "+err.Error())
return
}
err = h.registry.AddRole(payload[1], payload[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, payload[1]+" has been added to the "+payload[0]+" role.")
return
}
// remove
if command == "remove" {
if len(payload) < 2 {
s.ChannelMessageSend(m.ChannelID, command+" requires two arguments <role> <command>")
return
}
guildID, err := getGuildID(s, m.ChannelID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error retrieving GuildID: "+err.Error())
return
}
// Checks if Role Exists in Guild
_, err = getRoleIDByName(s, guildID, payload[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error retrieving roleID: "+err.Error())
return
}
err = h.registry.RemoveRole(payload[1], payload[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, payload[1]+" has been removed from the "+payload[0]+" role.")
return
}
}
// ReadUsers function
func (h *CommandHandler) ReadUsers(message []string, s *discordgo.Session, m *discordgo.MessageCreate) {
command := message[0]
payload := RemoveStringFromSlice(message, command)
if len(payload) < 1 {
s.ChannelMessageSend(m.ChannelID, command+" requires an argument")
return
}
// list
if command == "list" {
users, err := h.registry.GetUsers(payload[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
if len(users) < 1 {
s.ChannelMessageSend(m.ChannelID, ":\nNo User assignments found for "+payload[0])
return
}
var formatted string
for i, user := range users {
dguser, err := s.User(user)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
if i == len(users)-1 {
formatted = formatted + dguser.Mention()
} else {
formatted = formatted + dguser.Mention() + ", "
}
}
s.ChannelMessageSend(m.ChannelID, ":\nUsers for "+payload[0]+" : "+formatted)
return
}
// add
if command == "add" {
if len(payload) < 2 {
s.ChannelMessageSend(m.ChannelID, command+" requires two arguments <usermanager> <command>")
return
}
mentions := m.Mentions
if len(mentions) < 1 {
s.ChannelMessageSend(m.ChannelID, "Invalid Usage: User must be mentioned")
return
}
err := h.registry.AddUser(payload[1], mentions[0].ID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, mentions[0].Mention()+" has been added to "+payload[1])
return
}
// remove
if command == "remove" {
if len(payload) < 2 {
s.ChannelMessageSend(m.ChannelID, command+" requires two arguments <usermanager> <command>")
return
}
mentions := m.Mentions
if len(mentions) < 1 {
s.ChannelMessageSend(m.ChannelID, "Invalid Usage: User must be mentioned")
return
}
err := h.registry.RemoveUser(payload[1], mentions[0].ID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, mentions[0].Mention()+" has been removed from "+payload[0])
return
}
}
// ReadChannels function
func (h *CommandHandler) ReadChannels(message []string, s *discordgo.Session, m *discordgo.MessageCreate) {
command := message[0]
payload := RemoveStringFromSlice(message, command)
if len(payload) < 1 {
s.ChannelMessageSend(m.ChannelID, command+" requires an argument")
return
}
// list
if command == "list" {
channels, err := h.registry.ChannelList(payload[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
if len(channels) < 1 {
s.ChannelMessageSend(m.ChannelID, ":\nNo Channel assignments found for "+payload[0])
return
}
var formatted string
for i, channel := range channels {
dgchannel, err := s.Channel(channel)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
if i == len(channels)-1 {
formatted = formatted + "<#" + dgchannel.ID + ">"
} else {
formatted = formatted + "<#" + dgchannel.ID + ">, "
}
}
s.ChannelMessageSend(m.ChannelID, ":\nChannels for "+payload[0]+" : "+formatted)
return
}
// add
if command == "add" {
if len(payload) < 2 {
s.ChannelMessageSend(m.ChannelID, command+" requires two arguments <channel> <command>")
return
}
channelid := CleanChannel(payload[0])
_, err := strconv.Atoi(channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
}
channelmention, err := s.Channel(channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
if channelmention.Name == "" {
s.ChannelMessageSend(m.ChannelID, "Error during channel validation")
return
}
err = h.registry.AddChannel(payload[1], channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, "<#"+channelmention.ID+">"+" has been added to "+payload[1])
return
}
// remove
if command == "remove" {
if len(payload) < 2 {
s.ChannelMessageSend(m.ChannelID, command+" requires two arguments <channel> <command>")
return
}
channelid := CleanChannel(payload[0])
_, err := strconv.Atoi(channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
}
channelmention, err := s.Channel(channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
if channelmention.Name == "" {
s.ChannelMessageSend(m.ChannelID, "Invalid Usage: Channel must be mentioned")
return
}
err = h.registry.RemoveChannel(payload[1], channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, "<#"+channelmention.ID+">"+" has been removed from "+payload[0])
return
}
}
// EnableCommand function
func (h *CommandHandler) EnableCommand(message []string, s *discordgo.Session, m *discordgo.MessageCreate) {
if len(message) == 1 {
err := h.registry.AddChannel(message[0], m.ChannelID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, "Command "+message[0]+" enabled for this channel")
return
}
channelid := CleanChannel(message[1])
_, err := s.Channel(channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
h.ch.channeldb.CreateIfNotExists(channelid)
formattedchannel, err := MentionChannel(channelid, s)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
err = h.registry.AddChannel(message[0], channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, "Command "+message[0]+" enabled for "+formattedchannel)
return
}
// DisableCommand function
func (h *CommandHandler) DisableCommand(message []string, s *discordgo.Session, m *discordgo.MessageCreate) {
if len(message) == 1 {
err := h.registry.RemoveChannel(message[0], m.ChannelID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, "Command "+message[0]+" disabled for this channel")
return
}
channelid := CleanChannel(message[1])
_, err := s.Channel(channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
h.ch.channeldb.CreateIfNotExists(channelid)
formattedchannel, err := MentionChannel(channelid, s)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
err = h.registry.RemoveChannel(message[0], channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
s.ChannelMessageSend(m.ChannelID, "Command "+message[0]+" disabled for "+formattedchannel)
return
}
// DisplayUsage function
func (h *CommandHandler) DisplayUsage(message []string, s *discordgo.Session, m *discordgo.MessageCreate) {
command, err := h.registry.GetCommand(message[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
formattedmessage := ":\n" + " Usage guide for " + message[0] + "\n"
formattedmessage = formattedmessage + "```"
formattedmessage = formattedmessage + command.Usage
formattedmessage = formattedmessage + "```"
s.ChannelMessageSend(m.ChannelID, formattedmessage)
return
}
// DisplayDescription function
func (h *CommandHandler) DisplayDescription(message []string, s *discordgo.Session, m *discordgo.MessageCreate) {
command, err := h.registry.GetCommand(message[0])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
formattedmessage := ":\n" + " Description for " + message[0] + "\n"
formattedmessage = formattedmessage + "```"
formattedmessage = formattedmessage + command.Description
formattedmessage = formattedmessage + "```"
s.ChannelMessageSend(m.ChannelID, formattedmessage)
return
}
// ListCommands function
func (h *CommandHandler) ListCommands(channelid string, page int, s *discordgo.Session, m *discordgo.MessageCreate) {
if channelid == "" {
channelid = m.ChannelID
}
recordlist, err := h.registry.CommandsForChannel(page, channelid)
if err != nil {
if err.Error() == "not found" {
s.ChannelMessageSend(m.ChannelID, "No commands for this channel found")
return
}
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
pagecount, err := h.registry.CommandsForChannelPageCount(channelid)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
formattedmessage := h.FormatCommands(recordlist) + "\nPage " + strconv.Itoa(page+1) +
" of " + strconv.Itoa(pagecount)
s.ChannelMessageSend(m.ChannelID, formattedmessage)
return
}
// FormatCommands function
func (h *CommandHandler) FormatCommands(recordlist []CommandRecord) (formattedlist string) {
formattedmessage := ":\n" + " Command list for this channel (see command <description> or command <usage> if table is too small)\n"
formattedmessage = formattedmessage + "```"
formattedmessage = formattedmessage + "|---------------------|-------------------------|-------------------------|\n"
formattedmessage = formattedmessage + "| Command | Description | Usage |\n"
formattedmessage = formattedmessage + "|---------------------|-------------------------|-------------------------|\n"
// I am aware this is probably overcomplicated, but it's not a very frequently used command
commandColumnlen := len("| Command ") - 2
descriptionColumnlen := len("| Description ") - 2
usageColumnlen := len("| Usage ") - 2
for _, command := range recordlist {
commandlen := len(command.Command)
Command := command.Command
if commandlen < commandColumnlen {
diff := commandColumnlen - commandlen
for i := 0; i < diff; i++ {
Command = Command + " "
}
}
if commandlen > commandColumnlen {
diff := commandlen - commandColumnlen + 1
for i := 0; i < diff; i++ {
Command = Command[:len(Command)-1]
}
Command = Command + " "
}
descriptionlen := len(command.Description)
Description := command.Description
if descriptionlen < descriptionColumnlen {
diff := descriptionColumnlen - descriptionlen
for i := 0; i < diff; i++ {
Description = Description + " "
}
}
if descriptionlen > descriptionColumnlen {
diff := descriptionlen - descriptionColumnlen + 1
for i := 0; i < diff; i++ {
Description = Description[:len(Description)-1]
}
Description = Description + " "
}
usagelen := len(command.Usage)
Usage := command.Usage
if usagelen < usageColumnlen {
diff := usageColumnlen - usagelen
for i := 0; i < diff; i++ {
Usage = Usage + " "
}
}
if usagelen > usageColumnlen {
diff := usagelen - usageColumnlen + 1
for i := 0; i < diff; i++ {
Usage = Usage[:len(Usage)-1]
}
Usage = Usage + " "
}
formattedmessage = formattedmessage + "| " + Command + "| " + Description + "| " + Usage + "|\n"
}
formattedmessage = formattedmessage + "|---------------------|-------------------------|-------------------------|\n"
formattedmessage = formattedmessage + "```"
return formattedmessage
}