-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
PermOverrideManagerUpdatable.java
476 lines (437 loc) · 17.2 KB
/
PermOverrideManagerUpdatable.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
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* 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.managers;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Channel;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.PermissionOverride;
import net.dv8tion.jda.core.exceptions.PermissionException;
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.Arrays;
import java.util.Collection;
/**
* An {@link #update() updatable} manager that allows
* to modify {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverride} settings
* such as the granted and denied {@link net.dv8tion.jda.core.Permission Permissions}.
*
* <p>This manager allows to modify multiple fields at once followed by a call of {@link #update()}!
* <br>Default is no permissions granted/denied.
*
* <p>The {@link net.dv8tion.jda.core.managers.PermOverrideManager PermOverrideManager} implementation
* simplifies this process by giving simple setters that return the {@link #update() update} {@link net.dv8tion.jda.core.requests.RestAction RestAction}
*
* <p><b>Note</b>: To {@link #update() update} this manager
* the currently logged in account requires the Permission {@link net.dv8tion.jda.core.Permission#MANAGE_PERMISSIONS MANAGE_PERMISSIONS}
* in the parent {@link net.dv8tion.jda.core.entities.Channel Channel}
*/
public class PermOverrideManagerUpdatable
{
protected final PermissionOverride override;
protected Long allow;
protected Long deny;
protected boolean set;
/**
* Creates a new PermOverrideManagerUpdatable instance
*
* @param override
* The {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverride} to manage
*/
public PermOverrideManagerUpdatable(PermissionOverride override)
{
this.override = override;
}
/**
* The {@link net.dv8tion.jda.core.JDA JDA} instance of this Manager
*
* @return the corresponding JDA instance
*/
public JDA getJDA()
{
return override.getJDA();
}
/**
* The {@link net.dv8tion.jda.core.entities.Guild Guild} this Manager's
* {@link net.dv8tion.jda.core.entities.Channel Channel} is in.
* <br>This is logically the same as calling {@code getPermissionOverride().getGuild()}
*
* @return The parent {@link net.dv8tion.jda.core.entities.Guild Guild}
*/
public Guild getGuild()
{
return override.getGuild();
}
/**
* The {@link net.dv8tion.jda.core.entities.Channel Channel} this Manager's
* {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverride} is in.
* <br>This is logically the same as calling {@code getPermissionOverride().getChannel()}
*
* @return The parent {@link net.dv8tion.jda.core.entities.Channel Channel}
*/
public Channel getChannel()
{
return override.getChannel();
}
/**
* The target {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverride}
* that will be modified by this Manager
*
* @return The target {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverride}
*/
public PermissionOverride getPermissionOverride()
{
return override;
}
/**
* Grants the specified permission bits
* to the target {@link net.dv8tion.jda.core.entities.PermissionOverride}
*
* @param permissions
* Raw permission bits to grant
*
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If any of the provided permissions are not accessible
*
* @return The current Manager instance for chaining convenience
*/
public PermOverrideManagerUpdatable grant(long permissions)
{
return grant(Permission.getPermissions(permissions));
}
/**
* Grants the specified {@link net.dv8tion.jda.core.Permission Permissions}
* to the target {@link net.dv8tion.jda.core.entities.PermissionOverride}
*
* @param permissions
* Permissions to grant
*
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If any of the provided permissions are not accessible
* @throws IllegalArgumentException
* If any of the provided permissions is {@code null}
*
* @return The current Manager instance for chaining convenience
*/
public PermOverrideManagerUpdatable grant(Permission... permissions)
{
return grant(Arrays.asList(permissions));
}
/**
* Grants the specified {@link net.dv8tion.jda.core.Permission Permissions}
* to the target {@link net.dv8tion.jda.core.entities.PermissionOverride}
*
* @param permissions
* Permissions to grant
*
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If any of the provided permissions are not accessible
* @throws IllegalArgumentException
* If any of the provided permissions is {@code null}
*
* @return The current Manager instance for chaining convenience
*/
public PermOverrideManagerUpdatable grant(Collection<Permission> permissions)
{
Args.notNull(permissions, "Permission Collection");
permissions.forEach(perm ->
{
Args.notNull(perm, "Permission in Permission Collection");
//checkPermission(perm);
});
setupValues();
long allowBits = Permission.getRaw(permissions);
allow |= allowBits;
deny &= ~allowBits;
return this;
}
/**
* Denies the specified permission bits
* from the target {@link net.dv8tion.jda.core.entities.PermissionOverride}
*
* @param permissions
* Raw permission bits to deny
*
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If any of the provided permissions are not accessible
*
* @return The current Manager instance for chaining convenience
*/
public PermOverrideManagerUpdatable deny(long permissions)
{
return deny(Permission.getPermissions(permissions));
}
/**
* Denies the specified {@link net.dv8tion.jda.core.Permission Permissions}
* from the target {@link net.dv8tion.jda.core.entities.PermissionOverride}
*
* @param permissions
* Permissions to deny
*
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If any of the provided permissions are not accessible
* @throws IllegalArgumentException
* If any of the provided permissions is {@code null}
*
* @return The current Manager instance for chaining convenience
*/
public PermOverrideManagerUpdatable deny(Permission... permissions)
{
return deny(Arrays.asList(permissions));
}
/**
* Denies the specified {@link net.dv8tion.jda.core.Permission Permissions}
* from the target {@link net.dv8tion.jda.core.entities.PermissionOverride}
*
* @param permissions
* Permissions to deny
*
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If any of the provided permissions are not accessible
* @throws IllegalArgumentException
* If any of the provided permissions is {@code null}
*
* @return The current Manager instance for chaining convenience
*/
public PermOverrideManagerUpdatable deny(Collection<Permission> permissions)
{
Args.notNull(permissions, "Permission Collection");
permissions.forEach(perm ->
{
Args.notNull(perm, "Permission in Permission Collection");
//checkPermission(perm);
});
setupValues();
long denyBits = Permission.getRaw(permissions);
allow &= ~denyBits;
deny |= denyBits;
return this;
}
/**
* Clears the specified permission bits
* from the target {@link net.dv8tion.jda.core.entities.PermissionOverride}
* <br>This will make the specified Permissions be inherited
*
* @param permission
* Raw permission bits to clear
*
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If any of the provided permissions are not accessible
*
* @return The current Manager instance for chaining convenience
*/
public PermOverrideManagerUpdatable clear(long permission)
{
return clear(Permission.getPermissions(permission));
}
/**
* Clears the specified {@link net.dv8tion.jda.core.Permission Permissions}
* from the target {@link net.dv8tion.jda.core.entities.PermissionOverride}
* <br>This will make the specified Permissions be inherited
*
* @param permissions
* Permissions to clear
*
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If any of the provided permissions are not accessible
* @throws IllegalArgumentException
* If any of the provided permissions is {@code null}
*
* @return The current Manager instance for chaining convenience
*/
public PermOverrideManagerUpdatable clear(Permission... permissions)
{
return clear(Arrays.asList(permissions));
}
/**
* Clears the specified {@link net.dv8tion.jda.core.Permission Permissions}
* from the target {@link net.dv8tion.jda.core.entities.PermissionOverride}
* <br>This will make the specified Permissions be inherited
*
* @param permissions
* Permissions to clear
*
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If any of the provided permissions are not accessible
* @throws IllegalArgumentException
* If any of the provided permissions is {@code null}
*
* @return The current Manager instance for chaining convenience
*/
public PermOverrideManagerUpdatable clear(Collection<Permission> permissions)
{
Args.notNull(permissions, "Permission Collection");
permissions.forEach(perm ->
{
Args.notNull(perm, "Permission in Permission Collection");
//checkPermission(perm);
});
setupValues();
long clearBits = Permission.getRaw(permissions);
allow &= ~clearBits;
deny &= ~clearBits;
return this;
}
/**
* The granted {@link net.dv8tion.jda.core.Permission Permissions}
* value represented as raw long bits.
* <br>Use {@link Permission#getPermissions(long)} to retrieve a list of {@link net.dv8tion.jda.core.Permission Permissions}
* from the returned bits.
*
* <p>This value represents all permissions that should be granted by this {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverride}
*
* @return Granted {@link net.dv8tion.jda.core.Permission Permissions} value
* or {@code null} if {@link #isSet()} is {@code false}
*/
public Long getAllowBits()
{
return allow;
}
/**
* The denied {@link net.dv8tion.jda.core.Permission Permissions}
* value represented as raw long bits.
* <br>Use {@link Permission#getPermissions(long)} to retrieve a list of {@link net.dv8tion.jda.core.Permission Permissions}
* from the returned bits.
*
* <p>This value represents all permissions that should be denied by this {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverride}
*
* @return Denied {@link net.dv8tion.jda.core.Permission Permissions} value
* or {@code null} if {@link #isSet()} is {@code false}
*/
public Long getDenyBits()
{
return deny;
}
/**
* The inherited {@link net.dv8tion.jda.core.Permission Permissions}
* value represented as raw long bits.
* <br>Use {@link Permission#getPermissions(long)} to retrieve a list of {@link net.dv8tion.jda.core.Permission Permissions}
* from the returned bits.
*
* <p>This value represents all permissions that are not granted or denied by the settings of this Manager instance
* - thus they represent all permissions that are set to inherit from other overrides.
*
* @return Inherited {@link net.dv8tion.jda.core.Permission Permissions} value
* or {@code null} if {@link #isSet()} is {@code false}
*/
public Long getInheritBits()
{
if (!set)
return null;
long maxPerms = 0;
for (Permission perm : Permission.values())
{
if (perm.getOffset() > maxPerms)
maxPerms = perm.getOffset();
}
maxPerms = ~(1 << (maxPerms + 1)); //push 1 to max offset + 1, then flip to get a full-permission bit mask.
return (~allow | ~deny) & maxPerms;
}
/**
* Whether anything has been modified yet
*
* @return Whether anything has been modified
*/
public boolean isSet()
{
return set;
}
/**
* Resets all {@link net.dv8tion.jda.core.Permission Permission} values
* <br>This is automatically called by {@link #update()}
*/
public void reset()
{
set = false;
allow = null;
deny = null;
}
/**
* Creates a new {@link net.dv8tion.jda.core.requests.RestAction RestAction} instance
* that will apply <b>all</b> changes that have been made to this manager instance.
* <br>If no changes have been made this will simply return {@link net.dv8tion.jda.core.requests.RestAction.EmptyRestAction EmptyRestAction}.
*
* <p>Before applying new changes it is recommended to call {@link #reset()} to reset previous changes.
* <br>This is automatically called if this method returns successfully.
*
* <p>Possible {@link net.dv8tion.jda.core.requests.ErrorResponse ErrorResponses} for this
* update include the following:
* <ul>
* <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#UNKNOWN_OVERRIDE UNKNOWN_OVERRIDE}
* <br>If the PermissionOverride was deleted before finishing the task</li>
*
* <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
* <br>If the currently logged in account was removed from the Guild before finishing the task</li>
*
* <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
* <br>If the currently logged in account loses the {@link net.dv8tion.jda.core.Permission#MANAGE_PERMISSIONS MANAGE_PERMISSIONS Permission}</li>
* </ul>
*
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If the currently logged in account does not have the Permission {@link net.dv8tion.jda.core.Permission#MANAGE_PERMISSIONS MANAGE_PERMISSIONS}
* in the {@link #getChannel() Channel}
*
* @return {@link net.dv8tion.jda.core.requests.RestAction RestAction}
* <br>Applies all changes that have been made in a single api-call.
*/
public RestAction<Void> update()
{
checkPermission(Permission.MANAGE_PERMISSIONS);
if (!shouldUpdate())
return new RestAction.EmptyRestAction<>(null);
String targetId = override.isRoleOverride() ? override.getRole().getId() : override.getMember().getUser().getId();
JSONObject body = new JSONObject()
.put("id", targetId)
.put("type", override.isRoleOverride() ? "role" : "member")
.put("allow", getAllowBits())
.put("deny", getDenyBits());
reset();
Route.CompiledRoute route = Route.Channels.MODIFY_PERM_OVERRIDE.compile(override.getChannel().getId(), targetId);
return new RestAction<Void>(getJDA(), route, body)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
protected boolean shouldUpdate()
{
return set && (allow != override.getAllowedRaw() || deny != override.getDeniedRaw());
}
protected void setupValues()
{
if (!set)
{
set = true;
allow = override.getAllowedRaw();
deny = override.getDeniedRaw();
}
}
protected void checkPermission(Permission perm)
{
if (!getGuild().getSelfMember().hasPermission(getChannel(), perm))
throw new PermissionException(perm);
}
}