-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
permission.go
240 lines (218 loc) · 6.67 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
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
package discord
type Permissions uint64
// https://discord.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags
const (
// Allows creation of instant invites
PermissionCreateInstantInvite Permissions = 1 << iota
// Allows kicking members
PermissionKickMembers
// Allows banning members
PermissionBanMembers
// Allows all permissions and bypasses channel permission overwrites
PermissionAdministrator
// Allows management and editing of channels
PermissionManageChannels
// Allows management and editing of the guild
PermissionManageGuild
// Allows for the addition of reactions to messages
PermissionAddReactions
// Allows for viewing of audit logs
PermissionViewAuditLog
// Allows for using priority speaker in a voice channel
PermissionPrioritySpeaker
// Allows the user to go live
PermissionStream
// Allows guild members to view a channel, which includes reading messages
// in text channels
PermissionViewChannel
// Allows for sending messages in a channel
PermissionSendMessages
// Allows for sending of /tts messages
PermissionSendTTSMessages
// Allows for deletion of other users messages
PermissionManageMessages
// Links sent by users with this permission will be auto-embedded
PermissionEmbedLinks
// Allows for uploading images and files
PermissionAttachFiles
// Allows for reading of message history
PermissionReadMessageHistory
// 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
// Allows the usage of custom emojis from other servers
PermissionUseExternalEmojis
// Allows for viewing guild insights
PermissionViewGuildInsights
// Allows for joining of a voice channel
PermissionConnect
// Allows for speaking in a voice channel
PermissionSpeak
// Allows for muting members in a voice channel
PermissionMuteMembers
// Allows for deafening of members in a voice channel
PermissionDeafenMembers
// Allows for moving of members between voice channels
PermissionMoveMembers
// Allows for using voice-activity-detection in a voice channel
PermissionUseVAD
// Allows for modification of own nickname
PermissionChangeNickname
// Allows for modification of other users nicknames
PermissionManageNicknames
// Allows management and editing of roles
PermissionManageRoles
// Allows management and editing of webhooks
PermissionManageWebhooks
// Allows members to use slash commands in text channels
PermissionManageEmojisAndStickers
// Allows members to use slash commands in text channels
PermissionUseSlashCommands
// Allows for requesting to speak in stage channels. (This permission is
// under active development and may be changed or removed.)
PermissionRequestToSpeak
// Allows for creating, editing, and deleting scheduled events.
PermissionManageEvents
// Allows for deleting and archiving threads, and viewing all private
// threads
PermissionManageThreads
// Allows for creating and participating in threads.
PermissionCreatePublicThreads
// Allows for creating and participating in private threads.
PermissionCreatePrivateThreads
// Allows the usage of custom stickers from other servers
PermissionUseExternalStickers
// Allows for sending messages in threads
PermissionSendMessagesInThreads
// Allows for launching activities (applications with the EMBEDDED flag)
// in a voice channel
PermissionStartEmbeddedActivities
// Allows for timing out users
PermissionModerateMembers
// Allows for viewing role subscription insights
PermissionViewCreatorMonetizationAnalytics
// Allows for using soundboard in a voice channel
PermissionUseSoundboard
_
_
// Allows the usage of custom soundboard sounds from other servers
PermissionUseExternalSounds
// Allows sending voice messages
PermissionSendVoiceMessages
PermissionAllText = 0 |
PermissionViewChannel |
PermissionSendMessages |
PermissionSendTTSMessages |
PermissionManageMessages |
PermissionEmbedLinks |
PermissionAttachFiles |
PermissionReadMessageHistory |
PermissionMentionEveryone |
PermissionUseExternalEmojis |
PermissionUseSlashCommands |
PermissionManageThreads |
PermissionCreatePublicThreads |
PermissionCreatePrivateThreads |
PermissionUseExternalStickers |
PermissionAddReactions |
PermissionSendMessagesInThreads
PermissionAllVoice = 0 |
PermissionViewChannel |
PermissionConnect |
PermissionSpeak |
PermissionStream |
PermissionMuteMembers |
PermissionDeafenMembers |
PermissionMoveMembers |
PermissionUseVAD |
PermissionPrioritySpeaker |
PermissionRequestToSpeak |
PermissionStartEmbeddedActivities
PermissionAllChannel = 0 |
PermissionAllText |
PermissionAllVoice |
PermissionCreateInstantInvite |
PermissionManageRoles |
PermissionManageChannels
PermissionAll = 0 |
PermissionAllChannel |
PermissionKickMembers |
PermissionBanMembers |
PermissionManageGuild |
PermissionAdministrator |
PermissionManageWebhooks |
PermissionManageEmojisAndStickers |
PermissionManageNicknames |
PermissionChangeNickname |
PermissionViewAuditLog |
PermissionManageEvents
)
func NewPermissions(p ...Permissions) *Permissions {
var perm Permissions
for _, permission := range p {
perm |= permission
}
return &perm
}
func (p Permissions) Has(perm Permissions) bool {
return HasFlag(uint64(p), uint64(perm))
}
func (p Permissions) Add(perm Permissions) Permissions {
return p | perm
}
// CalcOverrides calculates the permissions for a member in the given channel.
// Most of the time, you should use state.State.Permissions instead.
func CalcOverrides(
guild Guild, channel Channel, member Member, roles []Role) Permissions {
if guild.OwnerID == member.User.ID {
return PermissionAll
}
var perm Permissions
for _, role := range roles {
if role.ID == RoleID(guild.ID) {
perm |= role.Permissions
break
}
}
for _, role := range roles {
for _, id := range member.RoleIDs {
if id == role.ID {
perm |= role.Permissions
break
}
}
}
if perm.Has(PermissionAdministrator) {
return PermissionAll
}
for _, overwrite := range channel.Overwrites {
if GuildID(overwrite.ID) == guild.ID {
perm &= ^overwrite.Deny
perm |= overwrite.Allow
break
}
}
var deny, allow Permissions
for _, overwrite := range channel.Overwrites {
for _, id := range member.RoleIDs {
if id == RoleID(overwrite.ID) && overwrite.Type == OverwriteRole {
deny |= overwrite.Deny
allow |= overwrite.Allow
break
}
}
}
perm &= ^deny
perm |= allow
for _, overwrite := range channel.Overwrites {
if UserID(overwrite.ID) == member.User.ID {
perm &= ^overwrite.Deny
perm |= overwrite.Allow
break
}
}
if perm.Has(PermissionAdministrator) {
return PermissionAll
}
return perm
}