forked from foxtrot/scuzzy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpermissions.go
100 lines (84 loc) · 1.87 KB
/
permissions.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
package permissions
import (
"github.com/bwmarrin/discordgo"
"github.com/foxtrot/scuzzy/models"
"strings"
)
type AdminRole struct {
Name string
ID string
}
type Permissions struct {
AdminRoles []AdminRole
CommandRestrictions []models.CommandRestriction
Config *models.Configuration
}
func New(config *models.Configuration, guild *discordgo.Guild) *Permissions {
var ars []AdminRole
for _, gRole := range guild.Roles {
for _, aRole := range config.AdminRoles {
if aRole != gRole.Name {
continue
}
ar := AdminRole{
Name: gRole.Name,
ID: gRole.ID,
}
ars = append(ars, ar)
}
}
var crs []models.CommandRestriction
for _, cRes := range config.CommandRestrictions {
cr := models.CommandRestriction{
Command: cRes.Command,
Mode: cRes.Mode,
Channels: cRes.Channels,
}
crs = append(crs, cr)
}
return &Permissions{
AdminRoles: ars,
CommandRestrictions: crs,
Config: config,
}
}
func (p *Permissions) CheckAdminRole(m *discordgo.Member) bool {
for _, aR := range p.AdminRoles {
for _, mID := range m.Roles {
if aR.ID == mID {
return true
}
}
}
return false
}
func (p *Permissions) CheckIgnoredUser(m *discordgo.User) bool {
for _, iU := range p.Config.IgnoredUsers {
if iU == m.ID {
return true
}
}
return false
}
func (p *Permissions) CheckCommandRestrictions(m *discordgo.MessageCreate) bool {
cName := strings.Split(m.Content, " ")[0]
cName = strings.Replace(cName, p.Config.CommandKey, "", 1)
cChanID := m.ChannelID
for _, cR := range p.CommandRestrictions {
if cName == cR.Command {
for _, cID := range cR.Channels {
if cID == cChanID && cR.Mode == "white" {
return true
} else if cID == cChanID && cR.Mode == "black" {
return false
}
}
if cR.Mode == "white" {
return false
} else {
return true
}
}
}
return true
}