-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
PermissionOverrideAction.java
463 lines (420 loc) · 17.2 KB
/
PermissionOverrideAction.java
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
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.requests.restaction;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.entities.impl.PermissionOverrideImpl;
import net.dv8tion.jda.core.entities.impl.TextChannelImpl;
import net.dv8tion.jda.core.entities.impl.VoiceChannelImpl;
import net.dv8tion.jda.core.requests.Request;
import net.dv8tion.jda.core.requests.Response;
import net.dv8tion.jda.core.requests.RestAction;
import net.dv8tion.jda.core.requests.Route;
import org.apache.http.util.Args;
import org.json.JSONObject;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Extension of {@link net.dv8tion.jda.core.requests.RestAction RestAction} specifically
* designed to create a {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverride}
* for a {@link net.dv8tion.jda.core.entities.Channel Channel}.
* This extension allows setting properties before executing the action.
*
* @since 3.0
* @author Florian Spieß
*/
public class PermissionOverrideAction extends RestAction<PermissionOverride>
{
private long allow = 0;
private long deny = 0;
private final OverrideType type;
private final Channel channel;
private final Member member;
private final Role role;
/**
* Creates a new PermissionOverrideAction instance
*
* @param api
* The current JDA instance
* @param route
* The {@link net.dv8tion.jda.core.requests.Route.CompiledRoute Route.CompiledRoute} to be used for rate limit handling
* @param channel
* The target {@link net.dv8tion.jda.core.entities.Channel Channel} for the PermissionOverride
* @param member
* The target {@link net.dv8tion.jda.core.entities.Member Member} that will be affected by the PermissionOverride
*/
public PermissionOverrideAction(JDA api, Route.CompiledRoute route, Channel channel, Member member)
{
super(api, route, null);
this.type = OverrideType.MEMBER;
this.channel = channel;
this.member = member;
this.role = null;
}
/**
* Creates a new PermissionOverrideAction instance
*
* @param api
* The current JDA instance
* @param route
* The {@link net.dv8tion.jda.core.requests.Route.CompiledRoute Route.CompiledRoute} to be used for rate limit handling
* @param channel
* The target {@link net.dv8tion.jda.core.entities.Channel Channel} for the PermissionOverride
* @param role
* The target {@link net.dv8tion.jda.core.entities.Role Role} that will be affected by the PermissionOverride
*/
public PermissionOverrideAction(JDA api, Route.CompiledRoute route, Channel channel, Role role)
{
super(api, route, null);
this.type = OverrideType.ROLE;
this.channel = channel;
this.member = null;
this.role = role;
}
/**
* The currently set of allowed permission bits.
* <br>This value represents all <b>granted</b> permissions
* in the raw bitwise representation.
*
* <p>Use {@link #getAllowedPermissions()} to retrieve a {@link java.util.List List}
* with {@link net.dv8tion.jda.core.Permission Permissions} for this value
*
* @return long value of granted permissions
*/
public long getAllow()
{
return allow;
}
/**
* Immutable list of {@link net.dv8tion.jda.core.Permission Permissions}
* that would be <b>granted</b> by the PermissionOverride that is created by this action.
*
* @return immutable list of granted {@link net.dv8tion.jda.core.Permission Permissions}
*/
public List<Permission> getAllowedPermissions()
{
return Collections.unmodifiableList(Permission.getPermissions(allow));
}
/**
* The currently set of denied permission bits.
* <br>This value represents all <b>denied</b> permissions
* in the raw bitwise representation.
*
* <p>Use {@link #getDeniedPermissions()} to retrieve a {@link java.util.List List}
* with {@link net.dv8tion.jda.core.Permission Permissions} for this value
*
* @return long value of denied permissions
*/
public long getDeny()
{
return deny;
}
/**
* Immutable list of {@link net.dv8tion.jda.core.Permission Permissions}
* that would be <b>denied</b> by the PermissionOverride that is created by this action.
*
* @return immutable list of denied {@link net.dv8tion.jda.core.Permission Permissions}
*/
public List<Permission> getDeniedPermissions()
{
return Collections.unmodifiableList(Permission.getPermissions(deny));
}
/**
* The currently set of inherited permission bits.
* <br>This value represents all permissions that are not explicitly allowed or denied
* in their raw bitwise representation.
* <br>Inherited Permissions are permissions that are defined by other rules
* from maybe other PermissionOverrides or a Role.
*
* <p>Use {@link #getInheritedPermissions()} to retrieve a {@link java.util.List List}
* with {@link net.dv8tion.jda.core.Permission Permissions} for this value
*
* @return long value of inherited permissions
*/
public long getInherited()
{
return ~allow & ~deny;
}
/**
* Immutable list of {@link net.dv8tion.jda.core.Permission Permissions}
* that would be <b>inherited</b> from other permission holders.
* <br>Permissions returned are not explicitly granted or denied!
*
* @return immutable list of inherited {@link net.dv8tion.jda.core.Permission Permissions}
*
* @see #getInherited()
*/
public List<Permission> getInheritedPermissions()
{
return Permission.getPermissions(getInherited());
}
/**
* The type of PermissionOverride
* that will be created by this action.
*
* @return {@link net.dv8tion.jda.core.requests.restaction.PermissionOverrideAction.OverrideType OverrideType}
* representing the type of PermissionOverride that will be created by this action
*/
public OverrideType getType()
{
return type;
}
/**
* Sets the value of explicitly granted permissions
* using the bitwise representation of a set of {@link net.dv8tion.jda.core.Permission Permissions}.
* <br>This value can be retrieved through {@link net.dv8tion.jda.core.Permission#getRaw(net.dv8tion.jda.core.Permission...) Permissions.getRaw(Permission...)}!
* <br><b>Note: Permissions not marked as {@link net.dv8tion.jda.core.Permission#isChannel() isChannel()} will have no affect!</b>
*
* @param allowBits
* The <b>positive</b> bits representing the granted
* permissions for the new PermissionOverride
*
* @throws java.lang.IllegalArgumentException
* If the provided bits are negative
* or higher than {@link net.dv8tion.jda.core.Permission#ALL_PERMISSIONS Permission.ALL_PERMISSIONS}
*
* @return The current PermissionOverrideAction - for chaining convenience
*/
public PermissionOverrideAction setAllow(long allowBits)
{
Args.notNegative(allowBits, "Granted permissions value");
Args.check(allowBits <= Permission.ALL_PERMISSIONS, "Specified allow value may not be greater than a full permission set");
this.allow = allowBits;
return this;
}
/**
* Sets the value of explicitly granted permissions
* using a Collection of {@link net.dv8tion.jda.core.Permission Permissions}.
* <br><b>Note: Permissions not marked as {@link net.dv8tion.jda.core.Permission#isChannel() isChannel()} will have no affect!</b>
*
* @param permissions
* The Collection of Permissions representing the granted
* permissions for the new PermissionOverride.
* <br>If the provided value is {@code null} the permissions are reset to the default of none
*
* @throws java.lang.IllegalArgumentException
* If the any of the specified Permissions is {@code null}
*
* @return The current PermissionOverrideAction - for chaining convenience
*/
public PermissionOverrideAction setAllow(Collection<Permission> permissions)
{
if (permissions == null || permissions.isEmpty())
return setAllow(0);
checkNull(permissions, "Permission");
return setAllow(Permission.getRaw(permissions));
}
/**
* Sets the value of explicitly granted permissions
* using a set of {@link net.dv8tion.jda.core.Permission Permissions}.
* <br><b>Note: Permissions not marked as {@link net.dv8tion.jda.core.Permission#isChannel() isChannel()} will have no affect!</b>
*
* @param permissions
* The Permissions representing the granted
* permissions for the new PermissionOverride.
* <br>If the provided value is {@code null} the permissions are reset to the default of none
*
* @throws java.lang.IllegalArgumentException
* If the any of the specified Permissions is {@code null}
*
* @return The current PermissionOverrideAction - for chaining convenience
*/
public PermissionOverrideAction setAllow(Permission... permissions)
{
if (permissions == null || permissions.length < 1)
return setAllow(0);
checkNull(permissions, "Permission");
return setAllow(Permission.getRaw(permissions));
}
/**
* Sets the value of explicitly denied permissions
* using the bitwise representation of a set of {@link net.dv8tion.jda.core.Permission Permissions}.
* <br>This value can be retrieved through {@link net.dv8tion.jda.core.Permission#getRaw(net.dv8tion.jda.core.Permission...) Permissions.getRaw(Permission...)}!
* <br><b>Note: Permissions not marked as {@link net.dv8tion.jda.core.Permission#isChannel() isChannel()} will have no affect!</b>
*
* @param denyBits
* The <b>positive</b> bits representing the denied
* permissions for the new PermissionOverride
*
* @throws java.lang.IllegalArgumentException
* If the provided bits are negative
* or higher than {@link net.dv8tion.jda.core.Permission#ALL_PERMISSIONS Permission.ALL_PERMISSIONS}
*
* @return The current PermissionOverrideAction - for chaining convenience
*/
public PermissionOverrideAction setDeny(long denyBits)
{
Args.notNegative(denyBits, "Denied permissions value");
Args.check(denyBits <= Permission.ALL_PERMISSIONS, "Specified allow value may not be greater than a full permission set");
this.deny = denyBits;
return this;
}
/**
* Sets the value of explicitly denied permissions
* using a Collection of {@link net.dv8tion.jda.core.Permission Permissions}.
* <br><b>Note: Permissions not marked as {@link net.dv8tion.jda.core.Permission#isChannel() isChannel()} will have no affect!</b>
*
* @param permissions
* The Collection of Permissions representing the denied
* permissions for the new PermissionOverride.
* <br>If the provided value is {@code null} the permissions are reset to the default of none
*
* @throws java.lang.IllegalArgumentException
* If the any of the specified Permissions is {@code null}
*
* @return The current PermissionOverrideAction - for chaining convenience
*/
public PermissionOverrideAction setDeny(Collection<Permission> permissions)
{
if (permissions == null || permissions.isEmpty())
return setDeny(0);
checkNull(permissions, "Permission");
return setDeny(Permission.getRaw(permissions));
}
/**
* Sets the value of explicitly denied permissions
* using a set of {@link net.dv8tion.jda.core.Permission Permissions}.
* <br><b>Note: Permissions not marked as {@link net.dv8tion.jda.core.Permission#isChannel() isChannel()} will have no affect!</b>
*
* @param permissions
* The Permissions representing the denied
* permissions for the new PermissionOverride.
* <br>If the provided value is {@code null} the permissions are reset to the default of none
*
* @throws java.lang.IllegalArgumentException
* If the any of the specified Permissions is {@code null}
*
* @return The current PermissionOverrideAction - for chaining convenience
*/
public PermissionOverrideAction setDeny(Permission... permissions)
{
if (permissions == null || permissions.length < 1)
return setDeny(0);
checkNull(permissions, "Permission");
return setDeny(Permission.getRaw(permissions));
}
/**
* Combination of {@link #setAllow(long)} and {@link #setDeny(long)}
*
* @param allowBits
* A non-negative bitwise representation
* of granted Permissions
* @param denyBits
* A non-negative bitwise representation
* of denied Permissions
*
* @throws java.lang.IllegalArgumentException
* If any of the provided bits are negative
* or higher than {@link net.dv8tion.jda.core.Permission#ALL_PERMISSIONS Permission.ALL_PERMISSIONS}
*
* @return The current PermissionOverrideAction - for chaining convenience
*/
public PermissionOverrideAction setPermissions(long allowBits, long denyBits)
{
setAllow(allowBits);
setDeny(denyBits);
return this;
}
/**
* Combination of {@link #setAllow(java.util.Collection)} and {@link #setDeny(java.util.Collection)}
* <br>If a passed collection is {@code null} it resets the represented value to {@code 0} - no permission specifics.
*
* @param grantPermissions
* A Collection of {@link net.dv8tion.jda.core.Permission Permissions}
* representing all explicitly granted Permissions for the PermissionOverride
* @param denyPermissions
* A Collection of {@link net.dv8tion.jda.core.Permission Permissions}
* representing all explicitly denied Permissions for the PermissionOverride
*
* @throws java.lang.IllegalArgumentException
* If the any of the specified Permissions is {@code null}
*
* @return The current PermissionOverrideAction - for chaining convenience
*/
public PermissionOverrideAction setPermissions(Collection<Permission> grantPermissions, Collection<Permission> denyPermissions)
{
setAllow(grantPermissions);
setDeny(denyPermissions);
return this;
}
@Override
protected void finalizeData()
{
JSONObject object = new JSONObject();
object.put("type", type.name);
object.put("allow", allow);
object.put("deny", deny);
super.data = object;
super.finalizeData();
}
@Override
protected void handleResponse(Response response, Request request)
{
if (!response.isOk())
{
request.onFailure(response);
return;
}
JSONObject object = response.getObject();
PermissionOverrideImpl override = new PermissionOverrideImpl(channel, member, role).setAllow(allow).setDeny(deny);
boolean isText = channel instanceof TextChannelImpl;
switch (type)
{
case MEMBER:
{
if (isText)
((TextChannelImpl) channel).getMemberOverrideMap().put(member, override);
else
((VoiceChannelImpl) channel).getMemberOverrideMap().put(member, override);
break;
}
case ROLE:
{
if (isText)
((TextChannelImpl) channel).getRoleOverrideMap().put(role, override);
else
((VoiceChannelImpl) channel).getRoleOverrideMap().put(role, override);
break;
}
}
request.onSuccess(override);
}
private void checkNull(Collection<?> collection, String name)
{
Args.notNull(collection, name);
collection.forEach(e -> Args.notNull(e, name));
}
private <T> void checkNull(T[] arr, String name)
{
Args.notNull(arr, name);
for (T e : arr)
Args.notNull(e, name);
}
public enum OverrideType
{
ROLE(0, "role"),
MEMBER(1, "member");
private final int id;
private final String name;
OverrideType(int id, String name)
{
this.id = id;
this.name = name;
}
}
}