-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
permission.go
184 lines (162 loc) · 5.15 KB
/
permission.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
package discord
type Permissions uint64
var (
// Allows creation of instant invites
PermissionCreateInstantInvite Permissions = 1 << 0
// Allows kicking members
PermissionKickMembers Permissions = 1 << 1
// Allows banning members
PermissionBanMembers Permissions = 1 << 2
// Allows all permissions and bypasses channel permission overwrites
PermissionAdministrator Permissions = 1 << 3
// Allows management and editing of channels
PermissionManageChannels Permissions = 1 << 4
// Allows management and editing of the guild
PermissionManageGuild Permissions = 1 << 5
// Allows for the addition of reactions to messages
PermissionAddReactions Permissions = 1 << 6
// Allows for viewing of audit logs
PermissionViewAuditLog Permissions = 1 << 7
// Allows for using priority speaker in a voice channel
PermissionPrioritySpeaker Permissions = 1 << 8
// Allows the user to go live
PermissionStream Permissions = 1 << 9
// Allows guild members to view a channel, which includes reading messages
// in text channels
PermissionViewChannel Permissions = 1 << 10
// Allows for sending messages in a channel
PermissionSendMessages Permissions = 1 << 11
// Allows for sending of /tts messages
PermissionSendTTSMessages Permissions = 1 << 12
// Allows for deletion of other users messages
PermissionManageMessages Permissions = 1 << 13
// Links sent by users with this permission will be auto-embedded
PermissionEmbedLinks Permissions = 1 << 14
// Allows for uploading images and files
PermissionAttachFiles Permissions = 1 << 15
// Allows for reading of message history
PermissionReadMessageHistory Permissions = 1 << 16
// Allows for using the @everyone tag to notify all users in a channel,
// and the @here tag to notify all online users in a channel
PermissionMentionEveryone Permissions = 1 << 17
// Allows the usage of custom emojis from other servers
PermissionUseExternalEmojis Permissions = 1 << 18
// ?
// Allows for joining of a voice channel
PermissionConnect Permissions = 1 << 20
// Allows for speaking in a voice channel
PermissionSpeak Permissions = 1 << 21
// Allows for muting members in a voice channel
PermissionMuteMembers Permissions = 1 << 22
// Allows for deafening of members in a voice channel
PermissionDeafenMembers Permissions = 1 << 23
// Allows for moving of members between voice channels
PermissionMoveMembers Permissions = 1 << 24
// Allows for using voice-activity-detection in a voice channel
PermissionUseVAD Permissions = 1 << 25
// Allows for modification of own nickname
PermissionChangeNickname Permissions = 1 << 26
// Allows for modification of other users nicknames
PermissionManageNicknames Permissions = 1 << 27
// Allows management and editing of roles
PermissionManageRoles Permissions = 1 << 28
// Allows management and editing of webhooks
PermissionManageWebhooks Permissions = 1 << 29
// Allows management and editing of emojis
PermissionManageEmojis Permissions = 1 << 30
PermissionAllText = 0 |
PermissionViewChannel |
PermissionSendMessages |
PermissionSendTTSMessages |
PermissionManageMessages |
PermissionEmbedLinks |
PermissionAttachFiles |
PermissionReadMessageHistory |
PermissionMentionEveryone |
PermissionUseExternalEmojis
PermissionAllVoice = 0 |
PermissionConnect |
PermissionSpeak |
PermissionMuteMembers |
PermissionDeafenMembers |
PermissionMoveMembers |
PermissionUseVAD |
PermissionPrioritySpeaker
PermissionAllChannel = 0 |
PermissionAllText |
PermissionAllVoice |
PermissionCreateInstantInvite |
PermissionManageRoles |
PermissionManageChannels |
PermissionAddReactions |
PermissionViewAuditLog
PermissionAll = 0 |
PermissionAllChannel |
PermissionKickMembers |
PermissionBanMembers |
PermissionManageGuild |
PermissionAdministrator |
PermissionManageWebhooks |
PermissionManageEmojis |
PermissionManageNicknames |
PermissionChangeNickname
)
func (p Permissions) Has(perm Permissions) bool {
return HasFlag(uint64(p), uint64(perm))
}
func (p Permissions) Add(perm Permissions) Permissions {
return p | perm
}
func CalcOverwrites(guild Guild, channel Channel, member Member) Permissions {
if guild.OwnerID == member.User.ID {
return PermissionAll
}
var perm Permissions
for _, role := range guild.Roles {
if role.ID == guild.ID {
perm |= role.Permissions
break
}
}
for _, role := range guild.Roles {
for _, id := range member.RoleIDs {
if id == role.ID {
perm |= role.Permissions
break
}
}
}
if perm.Has(PermissionAdministrator) {
return PermissionAll
}
for _, overwrite := range channel.Permissions {
if overwrite.ID == guild.ID {
perm &= ^overwrite.Deny
perm |= overwrite.Allow
break
}
}
var deny, allow Permissions
for _, overwrite := range channel.Permissions {
for _, id := range member.RoleIDs {
if id == overwrite.ID && overwrite.Type == "role" {
deny |= overwrite.Deny
allow |= overwrite.Allow
break
}
}
}
perm &= ^deny
perm |= allow
for _, overwrite := range channel.Permissions {
if overwrite.ID == member.User.ID {
perm &= ^overwrite.Deny
perm |= overwrite.Allow
break
}
}
if perm.Has(PermissionAdministrator) {
return PermissionAll
}
return perm
}