/
GuildAction.java
715 lines (668 loc) · 24 KB
/
GuildAction.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* 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.api.requests.restaction;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Icon;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.data.SerializableData;
import net.dv8tion.jda.internal.requests.restaction.GuildActionImpl;
import net.dv8tion.jda.internal.requests.restaction.PermOverrideData;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
/**
* {@link net.dv8tion.jda.api.requests.RestAction RestAction} extension
* specifically designed to allow for the creation of {@link net.dv8tion.jda.api.entities.Guild Guilds}.
* <br>This is available to all account types but may undergo certain restrictions by Discord.
*
* @since 3.4.0
*
* @see net.dv8tion.jda.api.JDA#createGuild(String)
*/
public interface GuildAction extends RestAction<Void>
{
@Nonnull
@Override
GuildAction setCheck(@Nullable BooleanSupplier checks);
@Nonnull
@Override
GuildAction timeout(long timeout, @Nonnull TimeUnit unit);
@Nonnull
@Override
GuildAction deadline(long timestamp);
/**
* Sets the {@link net.dv8tion.jda.api.entities.Icon Icon}
* for the resulting {@link net.dv8tion.jda.api.entities.Guild Guild}
*
* @param icon
* The {@link net.dv8tion.jda.api.entities.Icon Icon} to use
*
* @return The current GuildAction for chaining convenience
*/
@Nonnull
@CheckReturnValue
GuildAction setIcon(@Nullable Icon icon);
/**
* Sets the name for the resulting {@link net.dv8tion.jda.api.entities.Guild Guild}
*
* @param name
* The name to use
*
* @throws java.lang.IllegalArgumentException
* If the provided name is {@code null}, blank or not between 2-100 characters long
*
* @return The current GuildAction for chaining convenience
*/
@Nonnull
@CheckReturnValue
GuildAction setName(@Nonnull String name);
/**
* Sets the {@link net.dv8tion.jda.api.entities.Guild.VerificationLevel VerificationLevel}
* for the resulting {@link net.dv8tion.jda.api.entities.Guild Guild}
*
* @param level
* The {@link net.dv8tion.jda.api.entities.Guild.VerificationLevel VerificationLevel} to use
*
* @return The current GuildAction for chaining convenience
*/
@Nonnull
@CheckReturnValue
GuildAction setVerificationLevel(@Nullable Guild.VerificationLevel level);
/**
* Sets the {@link net.dv8tion.jda.api.entities.Guild.NotificationLevel NotificationLevel}
* for the resulting {@link net.dv8tion.jda.api.entities.Guild Guild}
*
* @param level
* The {@link net.dv8tion.jda.api.entities.Guild.NotificationLevel NotificationLevel} to use
*
* @return The current GuildAction for chaining convenience
*/
@Nonnull
@CheckReturnValue
GuildAction setNotificationLevel(@Nullable Guild.NotificationLevel level);
/**
* Sets the {@link net.dv8tion.jda.api.entities.Guild.ExplicitContentLevel ExplicitContentLevel}
* for the resulting {@link net.dv8tion.jda.api.entities.Guild Guild}
*
* @param level
* The {@link net.dv8tion.jda.api.entities.Guild.ExplicitContentLevel ExplicitContentLevel} to use
*
* @return The current GuildAction for chaining convenience
*/
@Nonnull
@CheckReturnValue
GuildAction setExplicitContentLevel(@Nullable Guild.ExplicitContentLevel level);
/**
* Adds a {@link GuildChannel GuildChannel} to the resulting
* Guild. This cannot be of type {@link ChannelType#CATEGORY CATEGORY}!
*
* @param channel
* The {@link ChannelData ChannelData}
* to use for the construction of the GuildChannel
*
* @throws java.lang.IllegalArgumentException
* If the provided channel is {@code null}!
*
* @return The current GuildAction for chaining convenience
*/
@Nonnull
@CheckReturnValue
GuildAction addChannel(@Nonnull ChannelData channel);
/**
* Gets the {@link ChannelData ChannelData}
* of the specified index. The index is 0 based on insertion order of {@link #addChannel(ChannelData)}!
*
* @param index
* The 0 based index of the channel
*
* @throws java.lang.IndexOutOfBoundsException
* If the provided index is not in bounds
*
* @return The current GuildAction for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelData getChannel(int index);
/**
* Removes the {@link ChannelData ChannelData}
* at the specified index and returns the removed object.
*
* @param index
* The index of the channel
*
* @throws java.lang.IndexOutOfBoundsException
* If the index is out of bounds
*
* @return The removed object
*/
@Nonnull
@CheckReturnValue
ChannelData removeChannel(int index);
/**
* Removes the provided {@link ChannelData ChannelData}
* from this GuildAction if present.
*
* @param data
* The ChannelData to remove
*
* @return The current GuildAction for chaining convenience
*/
@Nonnull
@CheckReturnValue
GuildAction removeChannel(@Nonnull ChannelData data);
/**
* Creates a new {@link ChannelData ChannelData}
* instance and adds it to this GuildAction.
*
* @param type
* The {@link ChannelType ChannelType} of the resulting GuildChannel
* <br>This may be of type {@link ChannelType#TEXT TEXT} or {@link ChannelType#VOICE VOICE}!
* @param name
* The name of the channel.
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If provided with an invalid ChannelType</li>
* <li>If the provided name is {@code null} or blank</li>
* <li>If the provided name is not between 2-100 characters long</li>
* </ul>
*
* @return The new ChannelData instance
*/
@Nonnull
@CheckReturnValue
ChannelData newChannel(@Nonnull ChannelType type, @Nonnull String name);
/**
* Retrieves the {@link RoleData RoleData} for the
* public role ({@link net.dv8tion.jda.api.entities.Guild#getPublicRole() Guild.getPublicRole()}) for the resulting Guild.
* <br>The public role is also known in the official client as the {@code @everyone} role.
*
* <p><b>You can only change the permissions of the public role!</b>
*
* @return RoleData of the public role
*/
@Nonnull
@CheckReturnValue
RoleData getPublicRole();
/**
* Retrieves the {@link RoleData RoleData} for the
* provided index.
* <br>The public role is at the index 0 and all others are ordered by insertion order!
*
* @param index
* The index of the role
*
* @throws java.lang.IndexOutOfBoundsException
* If the provided index is out of bounds
*
* @return RoleData of the provided index
*/
@Nonnull
@CheckReturnValue
RoleData getRole(int index);
/**
* Creates and add a new {@link RoleData RoleData} object
* representing a Role for the resulting Guild.
*
* <p>This can be used in {@link GuildAction.ChannelData#addPermissionOverride(GuildAction.RoleData, long, long) ChannelData.addPermissionOverride(...)}.
* <br>You may change any properties of this {@link RoleData RoleData} instance!
*
* @return RoleData for the new Role
*/
@Nonnull
@CheckReturnValue
RoleData newRole();
/**
* Mutable object containing information on a {@link net.dv8tion.jda.api.entities.Role Role}
* of the resulting {@link net.dv8tion.jda.api.entities.Guild Guild} that is constructed by a GuildAction instance
*
* <p>This may be used in {@link GuildAction.ChannelData#addPermissionOverride(GuildAction.RoleData, long, long)} ChannelData.addPermissionOverride(...)}!
*/
class RoleData implements SerializableData
{
protected final long id;
protected final boolean isPublicRole;
protected Long permissions;
protected String name;
protected Integer color;
protected Integer position;
protected Boolean mentionable, hoisted;
public RoleData(long id)
{
this.id = id;
this.isPublicRole = id == 0;
}
/**
* Sets the raw permission value for this Role
*
* @param rawPermissions
* Raw permission value
*
* @return The current RoleData instance for chaining convenience
*/
@Nonnull
public RoleData setPermissionsRaw(@Nullable Long rawPermissions)
{
this.permissions = rawPermissions;
return this;
}
/**
* Adds the provided permissions to the Role
*
* @param permissions
* The permissions to add
*
* @throws java.lang.IllegalArgumentException
* If any of the provided permissions is {@code null}
*
* @return The current RoleData instance for chaining convenience
*/
@Nonnull
public RoleData addPermissions(@Nonnull Permission... permissions)
{
Checks.notNull(permissions, "Permissions");
for (Permission perm : permissions)
Checks.notNull(perm, "Permissions");
if (this.permissions == null)
this.permissions = 0L;
this.permissions |= Permission.getRaw(permissions);
return this;
}
/**
* Adds the provided permissions to the Role
*
* @param permissions
* The permissions to add
*
* @throws java.lang.IllegalArgumentException
* If any of the provided permissions is {@code null}
*
* @return The current RoleData instance for chaining convenience
*/
@Nonnull
public RoleData addPermissions(@Nonnull Collection<Permission> permissions)
{
Checks.noneNull(permissions, "Permissions");
if (this.permissions == null)
this.permissions = 0L;
this.permissions |= Permission.getRaw(permissions);
return this;
}
/**
* Sets the name for this Role
*
* @param name
* The name
*
* @throws java.lang.IllegalStateException
* If this is the public role
*
* @return The current RoleData instance for chaining convenience
*/
@Nonnull
public RoleData setName(@Nullable String name)
{
checkPublic("name");
this.name = name;
return this;
}
/**
* Sets the color for this Role
*
* @param color
* The color for this Role
*
* @throws java.lang.IllegalStateException
* If this is the public role
*
* @return The current RoleData instance for chaining convenience
*/
@Nonnull
public RoleData setColor(@Nullable Color color)
{
checkPublic("color");
this.color = color == null ? null : color.getRGB();
return this;
}
/**
* Sets the color for this Role
*
* @param color
* The color for this Role, or {@code null} to unset
*
* @throws java.lang.IllegalStateException
* If this is the public role
*
* @return The current RoleData instance for chaining convenience
*/
@Nonnull
public RoleData setColor(@Nullable Integer color)
{
checkPublic("color");
this.color = color;
return this;
}
/**
* Sets the position for this Role
*
* @param position
* The position
*
* @throws java.lang.IllegalStateException
* If this is the public role
*
* @return The current RoleData instance for chaining convenience
*/
@Nonnull
public RoleData setPosition(@Nullable Integer position)
{
checkPublic("position");
this.position = position;
return this;
}
/**
* Sets whether the Role is mentionable
*
* @param mentionable
* Whether the role is mentionable
*
* @throws java.lang.IllegalStateException
* If this is the public role
*
* @return The current RoleData instance for chaining convenience
*/
@Nonnull
public RoleData setMentionable(@Nullable Boolean mentionable)
{
checkPublic("mentionable");
this.mentionable = mentionable;
return this;
}
/**
* Sets whether the Role is hoisted
*
* @param hoisted
* Whether the role is hoisted
*
* @throws java.lang.IllegalStateException
* If this is the public role
*
* @return The current RoleData instance for chaining convenience
*/
@Nonnull
public RoleData setHoisted(@Nullable Boolean hoisted)
{
checkPublic("hoisted");
this.hoisted = hoisted;
return this;
}
@Nonnull
@Override
public DataObject toData()
{
final DataObject o = DataObject.empty().put("id", Long.toUnsignedString(id));
if (permissions != null)
o.put("permissions", permissions);
if (position != null)
o.put("position", position);
if (name != null)
o.put("name", name);
if (color != null)
o.put("color", color & 0xFFFFFF);
if (mentionable != null)
o.put("mentionable", mentionable);
if (hoisted != null)
o.put("hoist", hoisted);
return o;
}
protected void checkPublic(String comment)
{
if (isPublicRole)
throw new IllegalStateException("Cannot modify " + comment + " for the public role!");
}
}
/**
* GuildChannel information used for the creation of {@link GuildChannel Channels} within
* the construction of a {@link net.dv8tion.jda.api.entities.Guild Guild} via GuildAction.
*
* <p>Use with {@link #addChannel(ChannelData) GuildAction.addChannel(ChannelData)}.
*/
class ChannelData implements SerializableData
{
protected final ChannelType type;
protected final String name;
protected final Set<PermOverrideData> overrides = new HashSet<>();
protected Integer position;
// Text only
protected String topic;
protected Boolean nsfw;
// Voice only
protected Integer bitrate, userlimit;
/**
* Constructs a data object containing information on
* a {@link GuildChannel GuildChannel} to be used in the construction
* of a {@link net.dv8tion.jda.api.entities.Guild Guild}!
*
* @param type
* The {@link ChannelType ChannelType} of the resulting GuildChannel
* <br>This may be of type {@link ChannelType#TEXT TEXT} or {@link ChannelType#VOICE VOICE}!
* @param name
* The name of the channel.
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If provided with an invalid ChannelType</li>
* <li>If the provided name is {@code null} or blank</li>
* <li>If the provided name is not between 2-100 characters long</li>
* </ul>
*/
public ChannelData(ChannelType type, String name)
{
Checks.notBlank(name, "Name");
Checks.check(type == ChannelType.TEXT || type == ChannelType.VOICE || type == ChannelType.STAGE,
"Can only create channels of type TEXT, STAGE, or VOICE in GuildAction!");
Checks.check(name.length() >= 2 && name.length() <= 100,
"Channel name has to be between 2-100 characters long!");
this.type = type;
this.name = name;
}
/**
* Sets the topic for this channel.
* <br>These are only relevant to channels of type {@link ChannelType#TEXT TEXT}.
*
* @param topic
* The topic for the channel
*
* @throws java.lang.IllegalArgumentException
* If the provided topic is bigger than 1024 characters
*
* @return This ChannelData instance for chaining convenience
*/
@Nonnull
public ChannelData setTopic(@Nullable String topic)
{
if (topic != null && topic.length() > 1024)
throw new IllegalArgumentException("Channel Topic must not be greater than 1024 in length!");
this.topic = topic;
return this;
}
/**
* Sets the whether this channel should be marked NSFW.
* <br>These are only relevant to channels of type {@link ChannelType#TEXT TEXT}.
*
* @param nsfw
* Whether this channel should be marked NSFW
*
* @return This ChannelData instance for chaining convenience
*/
@Nonnull
public ChannelData setNSFW(@Nullable Boolean nsfw)
{
this.nsfw = nsfw;
return this;
}
/**
* Sets the bitrate for this channel.
* <br>These are only relevant to channels of type {@link ChannelType#VOICE VOICE}.
*
* @param bitrate
* The bitrate for the channel (8000-96000)
*
* @throws java.lang.IllegalArgumentException
* If the provided bitrate is not between 8000-96000
*
* @return This ChannelData instance for chaining convenience
*/
@Nonnull
public ChannelData setBitrate(@Nullable Integer bitrate)
{
if (bitrate != null)
{
Checks.check(bitrate >= 8000, "Bitrate must be greater than 8000.");
Checks.check(bitrate <= 96000, "Bitrate must be less than 96000.");
}
this.bitrate = bitrate;
return this;
}
/**
* Sets the userlimit for this channel.
* <br>These are only relevant to channels of type {@link ChannelType#VOICE VOICE}.
*
* @param userlimit
* The userlimit for the channel (0-99)
*
* @throws java.lang.IllegalArgumentException
* If the provided userlimit is not between 0-99
*
* @return This ChannelData instance for chaining convenience
*/
@Nonnull
public ChannelData setUserlimit(@Nullable Integer userlimit)
{
if (userlimit != null && (userlimit < 0 || userlimit > 99))
throw new IllegalArgumentException("Userlimit must be between 0-99!");
this.userlimit = userlimit;
return this;
}
/**
* Sets the position for this channel.
*
* @param position
* The position for the channel
*
* @return This ChannelData instance for chaining convenience
*/
@Nonnull
public ChannelData setPosition(@Nullable Integer position)
{
this.position = position;
return this;
}
/**
* Adds a {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride} to this channel
* with the provided {@link RoleData RoleData}!
* <br>Use {@link #newRole() GuildAction.newRole()} to retrieve an instance of RoleData.
*
* @param role
* The target role
* @param allow
* The permissions to grant in the override
* @param deny
* The permissions to deny in the override
*
* @throws java.lang.IllegalArgumentException
* If the provided role is {@code null}
*
* @return This ChannelData instance for chaining convenience
*/
@Nonnull
public ChannelData addPermissionOverride(@Nonnull GuildActionImpl.RoleData role, long allow, long deny)
{
Checks.notNull(role, "Role");
this.overrides.add(new PermOverrideData(PermOverrideData.ROLE_TYPE, role.id, allow, deny));
return this;
}
/**
* Adds a {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride} to this channel
* with the provided {@link GuildAction.RoleData RoleData}!
* <br>Use {@link #newRole() GuildAction.newRole()} to retrieve an instance of RoleData.
*
* @param role
* The target role
* @param allow
* The permissions to grant in the override
* @param deny
* The permissions to deny in the override
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the provided role is {@code null}</li>
* <li>If any permission is {@code null}</li>
* </ul>
*
* @return This ChannelData instance for chaining convenience
*/
@Nonnull
public ChannelData addPermissionOverride(@Nonnull GuildActionImpl.RoleData role, @Nullable Collection<Permission> allow, @Nullable Collection<Permission> deny)
{
long allowRaw = 0;
long denyRaw = 0;
if (allow != null)
{
Checks.noneNull(allow, "Granted Permissions");
allowRaw = Permission.getRaw(allow);
}
if (deny != null)
{
Checks.noneNull(deny, "Denied Permissions");
denyRaw = Permission.getRaw(deny);
}
return addPermissionOverride(role, allowRaw, denyRaw);
}
@Nonnull
@Override
public DataObject toData()
{
final DataObject o = DataObject.empty();
o.put("name", name);
o.put("type", type.getId());
if (topic != null)
o.put("topic", topic);
if (nsfw != null)
o.put("nsfw", nsfw);
if (bitrate != null)
o.put("bitrate", bitrate);
if (userlimit != null)
o.put("user_limit", userlimit);
if (position != null)
o.put("position", position);
if (!overrides.isEmpty())
o.put("permission_overwrites", overrides);
return o;
}
}
}