-
Notifications
You must be signed in to change notification settings - Fork 145
/
PermissionOverwrite.lua
238 lines (214 loc) · 6.35 KB
/
PermissionOverwrite.lua
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
--[=[
@c PermissionOverwrite x Snowflake
@d Represents an object that is used to allow or deny specific permissions for a
role or member in a Discord guild channel.
]=]
local Snowflake = require('containers/abstract/Snowflake')
local Permissions = require('utils/Permissions')
local Resolver = require('client/Resolver')
local PermissionOverwrite, get = require('class')('PermissionOverwrite', Snowflake)
function PermissionOverwrite:__init(data, parent)
Snowflake.__init(self, data, parent)
end
--[=[
@m delete
@t http
@r boolean
@d Deletes the permission overwrite. This can be undone by creating a new version of
the same overwrite.
]=]
function PermissionOverwrite:delete()
local data, err = self.client._api:deleteChannelPermission(self._parent._id, self._id)
if data then
local cache = self._parent._permission_overwrites
if cache then
cache:_delete(self._id)
end
return true
else
return false, err
end
end
--[=[
@m getObject
@t http?
@r Role/Member
@d Returns the object associated with this overwrite, either a role or member.
This may make an HTTP request if the object is not cached.
]=]
function PermissionOverwrite:getObject()
local guild = self._parent._parent
if self.type == 'role' then
return guild:getRole(self._id)
elseif self.type == 'member' then
return guild:getMember(self._id)
end
end
local function getPermissions(self)
return Permissions(self._allow_new or self._allow), Permissions(self._deny_new or self._deny)
end
local function setPermissions(self, allow, deny)
local data, err = self.client._api:editChannelPermissions(self._parent._id, self._id, {
allow = allow, deny = deny, type = self._type
})
if data then
self._allow, self._deny = allow, deny
return true
else
return false, err
end
end
--[=[
@m getAllowedPermissions
@t mem
@r Permissions
@d Returns a permissions object that represents the permissions that this overwrite
explicitly allows.
]=]
function PermissionOverwrite:getAllowedPermissions()
return Permissions(self._allow_new or self._allow)
end
--[=[
@m getDeniedPermissions
@t mem
@r Permissions
@d Returns a permissions object that represents the permissions that this overwrite
explicitly denies.
]=]
function PermissionOverwrite:getDeniedPermissions()
return Permissions(self._deny_new or self._deny)
end
--[=[
@m setPermissions
@t http
@p allowed Permissions-Resolvables
@p denied Permissions-Resolvables
@r boolean
@d Sets the permissions that this overwrite explicitly allows and denies. This
method does NOT resolve conflicts. Please be sure to use the correct parameters.
]=]
function PermissionOverwrite:setPermissions(allowed, denied)
local allow = Resolver.permissions(allowed)
local deny = Resolver.permissions(denied)
return setPermissions(self, allow, deny)
end
--[=[
@m setAllowedPermissions
@t http
@p allowed Permissions-Resolvables
@r boolean
@d Sets the permissions that this overwrite explicitly allows.
]=]
function PermissionOverwrite:setAllowedPermissions(allowed)
local allow = Permissions(Resolver.permissions(allowed))
local deny = allow:complement(self:getDeniedPermissions()) -- un-deny the allowed permissions
return setPermissions(self, allow.value, deny.value)
end
--[=[
@m setDeniedPermissions
@t http
@p denied Permissions-Resolvables
@r boolean
@d Sets the permissions that this overwrite explicitly denies.
]=]
function PermissionOverwrite:setDeniedPermissions(denied)
local deny = Permissions(Resolver.permissions(denied))
local allow = deny:complement(self:getAllowedPermissions()) -- un-allow the denied permissions
return setPermissions(self, allow.value, deny.value)
end
--[=[
@m allowPermissions
@t http
@p ... Permission-Resolvables
@r boolean
@d Allows individual permissions in this overwrite.
]=]
function PermissionOverwrite:allowPermissions(...)
local allowed, denied = getPermissions(self)
allowed:enable(...); denied:disable(...)
return setPermissions(self, allowed.value, denied.value)
end
--[=[
@m denyPermissions
@t http
@p ... Permission-Resolvables
@r boolean
@d Denies individual permissions in this overwrite.
]=]
function PermissionOverwrite:denyPermissions(...)
local allowed, denied = getPermissions(self)
allowed:disable(...); denied:enable(...)
return setPermissions(self, allowed.value, denied.value)
end
--[=[
@m clearPermissions
@t http
@p ... Permission-Resolvables
@r boolean
@d Clears individual permissions in this overwrite.
]=]
function PermissionOverwrite:clearPermissions(...)
local allowed, denied = getPermissions(self)
allowed:disable(...); denied:disable(...)
return setPermissions(self, allowed.value, denied.value)
end
--[=[
@m allowAllPermissions
@t http
@r boolean
@d Allows all permissions in this overwrite.
]=]
function PermissionOverwrite:allowAllPermissions()
local allowed, denied = getPermissions(self)
allowed:enableAll(); denied:disableAll()
return setPermissions(self, allowed.value, denied.value)
end
--[=[
@m denyAllPermissions
@t http
@r boolean
@d Denies all permissions in this overwrite.
]=]
function PermissionOverwrite:denyAllPermissions()
local allowed, denied = getPermissions(self)
allowed:disableAll(); denied:enableAll()
return setPermissions(self, allowed.value, denied.value)
end
--[=[
@m clearAllPermissions
@t http
@r boolean
@d Clears all permissions in this overwrite.
]=]
function PermissionOverwrite:clearAllPermissions()
local allowed, denied = getPermissions(self)
allowed:disableAll(); denied:disableAll()
return setPermissions(self, allowed.value, denied.value)
end
--[=[@p type string The overwrite type; either "role" or "member".]=]
function get.type(self)
if type(self._type) == 'string' then
return self._type
elseif self._type == 1 then
return 'member'
else -- 0
return 'role'
end
end
--[=[@p channel GuildChannel The channel in which this overwrite exists.]=]
function get.channel(self)
return self._parent
end
--[=[@p guild Guild The guild in which this overwrite exists. Equivalent to `PermissionOverwrite.channel.guild`.]=]
function get.guild(self)
return self._parent._parent
end
--[=[@p allowedPermissions number The number representing the total permissions allowed by this overwrite.]=]
function get.allowedPermissions(self)
return tonumber(self._allow_new) or tonumber(self._allow)
end
--[=[@p deniedPermissions number The number representing the total permissions denied by this overwrite.]=]
function get.deniedPermissions(self)
return tonumber(self._deny_new) or tonumber(self._deny)
end
return PermissionOverwrite