-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand_registry.go
629 lines (495 loc) · 12.5 KB
/
command_registry.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
package main
/*
The command registry adds the ability for commands to be "protected" by permissions.
Now I use quotes because there are no guarantees that a command may inadvertently kick off a process if permissions aren't checked properly.
*/
import (
"errors"
"fmt"
"github.com/bwmarrin/discordgo"
"strings"
)
// CommandRegistry struct
type CommandRegistry struct {
db *DBHandler
conf *Config
user *UserHandler
}
// CommandRecord struct
type CommandRecord struct {
Command string `storm:"id"`
Groups []string
Roles []string
Channels []string `storm:"index"`
Users []string `storm:"index"`
Description string
Usage string
}
// Register function
// Does the same thing as Create Command without a return value
func (h *CommandRegistry) Register(command string, description string, usage string) {
db := h.db.rawdb.From("Commands")
record := CommandRecord{}
err := db.One("Command", command, &record)
if err == nil {
return // command already exists
}
record.Command = command
record.Description = description
record.Usage = usage
err = h.SaveCommand(record)
if err != nil {
fmt.Println("Could not save command to registry" + err.Error())
return
}
return
}
// CreateCommand function
func (h *CommandRegistry) CreateCommand(command string) (err error) {
db := h.db.rawdb.From("Commands")
record := CommandRecord{}
err = db.One("Command", command, &record)
if err == nil {
return errors.New("Command already exists")
}
record.Command = command
err = h.SaveCommand(record)
if err != nil {
return err
}
return nil
}
// SaveCommand function
func (h *CommandRegistry) SaveCommand(record CommandRecord) (err error) {
db := h.db.rawdb.From("Commands")
err = db.DeleteStruct(&record)
err = db.Save(&record)
return err
}
// RemoveCommand function
func (h *CommandRegistry) RemoveCommand(record CommandRecord) (err error) {
db := h.db.rawdb.From("Commands")
err = db.DeleteStruct(&record)
return err
}
// GetCommand function
func (h *CommandRegistry) GetCommand(command string) (record CommandRecord, err error) {
db := h.db.rawdb.From("Commands")
record = CommandRecord{}
err = db.One("Command", command, &record)
if err != nil {
return record, err
}
return record, nil
}
// AddGroup function
func (h *CommandRegistry) AddGroup(command string, group string) (err error) {
record, err := h.GetCommand(command)
if err != nil {
return err
}
for _, v := range record.Groups {
if v == group {
return errors.New("Command already belongs to group " + group)
}
}
record.Groups = append(record.Groups, group)
h.SaveCommand(record)
return nil
}
// AddRole function
func (h *CommandRegistry) AddRole(command string, role string) (err error) {
role = strings.Title(role)
record, err := h.GetCommand(command)
if err != nil {
return err
}
for _, v := range record.Roles {
if v == role {
return errors.New("Command already belongs to role " + role)
}
}
record.Roles = append(record.Roles, role)
h.SaveCommand(record)
return nil
}
// RemoveGroup function
func (h *CommandRegistry) RemoveGroup(command string, group string) (err error) {
record, err := h.GetCommand(command)
if err != nil {
return err
}
for _, v := range record.Groups {
if v == group {
record.Groups = RemoveStringFromSlice(record.Groups, group)
h.SaveCommand(record)
return nil
}
}
return errors.New("Command does not belong to group " + group)
}
// RemoveRole function
func (h *CommandRegistry) RemoveRole(command string, role string) (err error) {
role = strings.Title(role)
record, err := h.GetCommand(command)
if err != nil {
return err
}
for _, v := range record.Roles {
if v == role {
record.Groups = RemoveStringFromSlice(record.Roles, role)
h.SaveCommand(record)
return nil
}
}
return errors.New("Command does not belong to role " + role)
}
// GetGroups function
func (h *CommandRegistry) GetGroups(command string) (groups []string, err error) {
record, err := h.GetCommand(command)
if err != nil {
return groups, err
}
return record.Groups, nil
}
// GetRoles function
func (h *CommandRegistry) GetRoles(command string) (roles []string, err error) {
record, err := h.GetCommand(command)
if err != nil {
return roles, err
}
return record.Roles, nil
}
// AddChannel function
func (h *CommandRegistry) AddChannel(command string, channel string) (err error) {
record, err := h.GetCommand(command)
if err != nil {
return err
}
for _, v := range record.Channels {
if v == channel {
return errors.New("Command already belongs to channel " + channel)
}
}
record.Channels = append(record.Channels, channel)
h.SaveCommand(record)
return nil
}
// RemoveChannel function
func (h *CommandRegistry) RemoveChannel(command string, channel string) (err error) {
record, err := h.GetCommand(command)
if err != nil {
return err
}
for _, v := range record.Channels {
if v == channel {
record.Channels = RemoveStringFromSlice(record.Channels, channel)
h.SaveCommand(record)
return nil
}
}
return errors.New("Command does not belong to channel " + channel)
}
// GetChannels function
func (h *CommandRegistry) GetChannels(command string) (channels []string, err error) {
record, err := h.GetCommand(command)
if err != nil {
return channels, err
}
return record.Channels, nil
}
// CheckChannel function
func (h *CommandRegistry) CheckChannel(command string, channel string) bool {
channels, err := h.GetChannels(command)
if err != nil {
return false
}
for _, c := range channels {
if c == channel {
return true
}
}
return false
}
// CheckGroup function
func (h *CommandRegistry) CheckGroup(command string, group string) bool {
groups, err := h.GetGroups(command)
if err != nil {
return false
}
for _, c := range groups {
if c == group {
return true
}
}
return false
}
// CheckUserGroups function
func (h *CommandRegistry) CheckUserGroups(command string, user User, s *discordgo.Session, m *discordgo.MessageCreate) bool {
groups, err := h.GetGroups(command)
if err != nil {
return false
}
usergroups, err := h.user.GetGroups(user.ID, s, m.ChannelID)
if err != nil {
return false
}
for _, group := range groups {
for _, usergroup := range usergroups {
if group == usergroup {
return true
}
}
}
return false
}
// CheckUserRoles function
func (h *CommandRegistry) CheckUserRoles(command string, user User, s *discordgo.Session, m *discordgo.MessageCreate) bool {
roles, err := h.GetRoles(command)
if err != nil {
return false
}
userroles, err := h.user.GetRoles(user.ID, s, m.ChannelID)
if err != nil {
return false
}
for _, role := range roles {
role = strings.Title(role)
for _, userrole := range userroles {
userrole = strings.Title(userrole)
if role == userrole {
return true
}
}
}
return false
}
// AddUser function
func (h *CommandRegistry) AddUser(command string, user string) (err error) {
record, err := h.GetCommand(command)
if err != nil {
return err
}
for _, v := range record.Users {
if v == user {
return errors.New(user + " already has permission to use " + command)
}
}
record.Users = append(record.Users, user)
h.SaveCommand(record)
return nil
}
// RemoveUser function
func (h *CommandRegistry) RemoveUser(command string, user string) (err error) {
record, err := h.GetCommand(command)
if err != nil {
return err
}
for _, v := range record.Users {
if v == user {
record.Users = RemoveStringFromSlice(record.Users, user)
h.SaveCommand(record)
return nil
}
}
return errors.New(user + " does not have permission to use " + command)
}
// GetUsers function
func (h *CommandRegistry) GetUsers(command string) (users []string, err error) {
record, err := h.GetCommand(command)
if err != nil {
return users, err
}
return record.Users, nil
}
// CheckUser function
func (h *CommandRegistry) CheckUser(command string, user string) bool {
users, err := h.GetUsers(command)
if err != nil {
return false
}
for _, c := range users {
if c == user {
return true
}
}
return false
}
// CheckPermission function
func (h *CommandRegistry) CheckPermission(command string, user User, s *discordgo.Session, m *discordgo.MessageCreate) bool {
userpermission := false
if h.CheckUser(command, user.ID) {
userpermission = true
}
channelpermission := false
if h.CheckChannel(command, m.ChannelID) {
channelpermission = true
}
if h.CheckUserRoles(command, user, s, m) {
userpermission = true
}
if h.CheckUserGroups(command, user, s, m) {
userpermission = true
}
if channelpermission && userpermission {
return true
}
return false
}
// ChannelList function
func (h *CommandRegistry) ChannelList(command string) (channels []string, err error) {
// Check Channels
cmd, err := h.GetCommand(command)
if err != nil {
fmt.Println("Could not get command for channel list")
return channels, err
}
for _, channel := range cmd.Channels {
channels = append(channels, channel)
}
return channels, nil
}
// UserList function
func (h *CommandRegistry) UserList(command string) (users []string, err error) {
// Check Users
cmd, err := h.GetCommand(command)
if err != nil {
return users, err
}
for _, user := range cmd.Users {
users = append(users, user)
}
return users, nil
}
// GroupList function
func (h *CommandRegistry) GroupList(command string) (groups []string, err error) {
// Check Groups
cmd, err := h.GetCommand(command)
if err != nil {
return groups, err
}
for _, group := range cmd.Groups {
groups = append(groups, group)
}
return groups, nil
}
// CommandsForChannel function
func (h *CommandRegistry) CommandsForChannel(page int, channel string) (records []CommandRecord, err error) {
db := h.db.rawdb.From("Commands")
// Sanitize our page count
pagecount, err := h.CommandsForChannelPageCount(channel)
if err != nil {
return records, err
}
if page > pagecount {
page = pagecount
}
if page < 0 {
return records, errors.New("Invalid page")
}
commandrecords := []CommandRecord{}
err = db.All(&commandrecords)
if err != nil {
return records, err
}
recordcount := 0
currentrecordcount := 0
for _, record := range commandrecords {
for _, channelID := range record.Channels {
if channelID == channel {
currentrecordcount = currentrecordcount + 1
if currentrecordcount > page*h.conf.MainConfig.PerPageCount {
if recordcount < h.conf.MainConfig.PerPageCount {
records = append(records, record)
recordcount = recordcount + 1
}
}
}
}
}
if len(records) < 1 {
return records, errors.New("not found")
}
return records, nil
}
// CommandsForChannelCount function
func (h *CommandRegistry) CommandsForChannelCount(channel string) (count int, err error) {
db := h.db.rawdb.From("Commands")
commandrecords := []CommandRecord{}
err = db.All(&commandrecords)
if err != nil {
return 0, err
}
for _, record := range commandrecords {
for _, channelID := range record.Channels {
if channelID == channel {
count = count + 1
}
}
}
return count, nil
}
// CommandsForChannelPageCount function
func (h *CommandRegistry) CommandsForChannelPageCount(channel string) (pages int, err error) {
// Check Groups
db := h.db.rawdb.From("Commands")
pages = 0
commandrecords := []CommandRecord{}
err = db.All(&commandrecords)
if err != nil {
return pages, err
}
commandCount, err := h.CommandsForChannelCount(channel)
if err != nil {
return pages, err
}
if commandCount < 1 {
return pages, errors.New("not found")
}
pages = (commandCount / h.conf.MainConfig.PerPageCount) + 1
return pages, nil
}
// SetDescription function
func (h *CommandRegistry) SetDescription(command string, description string) (err error) {
record, err := h.GetCommand(command)
if err != nil {
return err
}
record.Description = description
err = h.SaveCommand(record)
if err != nil {
return err
}
return nil
}
// GetDescription function
func (h *CommandRegistry) GetDescription(command string) (description string, err error) {
record, err := h.GetCommand(command)
if err != nil {
return description, err
}
description = record.Description
return description, nil
}
// SetUsage function
func (h *CommandRegistry) SetUsage(command string, usage string) (err error) {
record, err := h.GetCommand(command)
if err != nil {
return err
}
record.Usage = usage
err = h.SaveCommand(record)
if err != nil {
return err
}
return nil
}
// GetUsage function
func (h *CommandRegistry) GetUsage(command string) (usage string, err error) {
record, err := h.GetCommand(command)
if err != nil {
return usage, err
}
usage = record.Usage
return usage, nil
}