-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
ChannelManager.java
754 lines (712 loc) · 30 KB
/
ChannelManager.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* 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 gnu.trove.map.hash.TLongObjectHashMap;
import gnu.trove.set.TLongSet;
import gnu.trove.set.hash.TLongHashSet;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.entities.impl.AbstractChannelImpl;
import net.dv8tion.jda.core.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.core.managers.impl.ManagerBase;
import net.dv8tion.jda.core.requests.Route;
import net.dv8tion.jda.core.requests.restaction.PermOverrideData;
import net.dv8tion.jda.core.utils.Checks;
import net.dv8tion.jda.core.utils.cache.UpstreamReference;
import okhttp3.RequestBody;
import org.json.JSONObject;
import javax.annotation.CheckReturnValue;
import java.util.Collection;
/**
* Manager providing functionality to update one or more fields for a {@link net.dv8tion.jda.core.entities.Channel Guild Channel}.
*
* <p><b>Example</b>
* <pre>{@code
* manager.setName("github-log")
* .setTopic("logs for github commits")
* .setNSFW(false)
* .queue();
* manager.reset(ChannelManager.TOPIC | ChannelManager.NAME)
* .setName("nsfw-commits")
* .setTopic(null)
* .setNSFW(true)
* .queue();
* }</pre>
*
* @see net.dv8tion.jda.core.entities.Channel#getManager()
*/
public class ChannelManager extends ManagerBase
{
/** Used to reset the name field */
public static final long NAME = 0x1;
/** Used to reset the parent field */
public static final long PARENT = 0x2;
/** Used to reset the topic field */
public static final long TOPIC = 0x4;
/** Used to reset the position field */
public static final long POSITION = 0x8;
/** Used to reset the nsfw field */
public static final long NSFW = 0x10;
/** Used to reset the userlimit field */
public static final long USERLIMIT = 0x20;
/** Used to reset the bitrate field */
public static final long BITRATE = 0x40;
/** Used to reset the permission field */
public static final long PERMISSION = 0x80;
/** Used to reset the rate-limit per user field */
public static final long SLOWMODE = 0x100;
protected final UpstreamReference<Channel> channel;
protected String name;
protected String parent;
protected String topic;
protected int position;
protected boolean nsfw;
protected int slowmode;
protected int userlimit;
protected int bitrate;
protected final Object lock = new Object();
protected final TLongObjectHashMap<PermOverrideData> overridesAdd;
protected final TLongSet overridesRem;
/**
* Creates a new ChannelManager instance
*
* @param channel
* {@link net.dv8tion.jda.core.entities.Channel Channel} that should be modified
* <br>Either {@link net.dv8tion.jda.core.entities.VoiceChannel Voice}- or {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
*/
public ChannelManager(Channel channel)
{
super(channel.getJDA(),
Route.Channels.MODIFY_CHANNEL.compile(channel.getId()));
this.channel = new UpstreamReference<>(channel);
if (isPermissionChecksEnabled())
checkPermissions();
this.overridesAdd = new TLongObjectHashMap<>();
this.overridesRem = new TLongHashSet();
}
/**
* The {@link net.dv8tion.jda.core.entities.ChannelType ChannelType}
*
* @return The ChannelType
*/
public ChannelType getType()
{
return getChannel().getType();
}
/**
* The {@link net.dv8tion.jda.core.entities.Channel Channel} that will
* be modified by this Manager instance
*
* @return The {@link net.dv8tion.jda.core.entities.Channel Channel}
*/
public Channel getChannel()
{
return channel.get();
}
/**
* 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 getChannel().getGuild()}
*
* @return The parent {@link net.dv8tion.jda.core.entities.Guild Guild}
*/
public Guild getGuild()
{
return getChannel().getGuild();
}
/**
* Resets the fields specified by the provided bit-flag pattern.
* You can specify a combination by using a bitwise OR concat of the flag constants.
* <br>Example: {@code manager.reset(ChannelManager.NAME | ChannelManager.PARENT);}
*
* <p><b>Flag Constants:</b>
* <ul>
* <li>{@link #NAME}</li>
* <li>{@link #PARENT}</li>
* <li>{@link #TOPIC}</li>
* <li>{@link #POSITION}</li>
* <li>{@link #NSFW}</li>
* <li>{@link #SLOWMODE}</li>
* <li>{@link #USERLIMIT}</li>
* <li>{@link #BITRATE}</li>
* <li>{@link #PERMISSION}</li>
* </ul>
*
* @param fields
* Integer value containing the flags to reset.
*
* @return ChannelManager for chaining convenience
*/
@Override
@CheckReturnValue
public ChannelManager reset(long fields)
{
super.reset(fields);
if ((fields & NAME) == NAME)
this.name = null;
if ((fields & PARENT) == PARENT)
this.parent = null;
if ((fields & TOPIC) == TOPIC)
this.topic = null;
if ((fields & PERMISSION) == PERMISSION)
{
withLock(lock, (lock) ->
{
this.overridesRem.clear();
this.overridesAdd.clear();
});
}
return this;
}
/**
* Resets the fields specified by the provided bit-flag patterns.
* <br>Example: {@code manager.reset(ChannelManager.NAME, ChannelManager.PARENT);}
*
* <p><b>Flag Constants:</b>
* <ul>
* <li>{@link #NAME}</li>
* <li>{@link #PARENT}</li>
* <li>{@link #TOPIC}</li>
* <li>{@link #POSITION}</li>
* <li>{@link #NSFW}</li>
* <li>{@link #USERLIMIT}</li>
* <li>{@link #BITRATE}</li>
* <li>{@link #PERMISSION}</li>
* </ul>
*
* @param fields
* Integer values containing the flags to reset.
*
* @return ChannelManager for chaining convenience
*/
@Override
@CheckReturnValue
public ChannelManager reset(long... fields)
{
super.reset(fields);
return this;
}
/**
* Resets all fields for this manager.
*
* @return ChannelManager for chaining convenience
*/
@Override
@CheckReturnValue
public ChannelManager reset()
{
super.reset();
this.name = null;
this.parent = null;
this.topic = null;
withLock(lock, (lock) ->
{
this.overridesRem.clear();
this.overridesAdd.clear();
});
return this;
}
/**
* Clears the overrides added via {@link #putPermissionOverride(IPermissionHolder, Collection, Collection)}.
*
* @return ChannelManager for chaining convenience
*/
public ChannelManager clearOverridesAdded()
{
withLock(lock, (lock) ->
{
this.overridesAdd.clear();
if (this.overridesRem.isEmpty())
set &= ~PERMISSION;
});
return this;
}
/**
* Clears the overrides removed via {@link #removePermissionOverride(IPermissionHolder)}.
*
* @return ChannelManager for chaining convenience
*/
public ChannelManager clearOverridesRemoved()
{
withLock(lock, (lock) ->
{
this.overridesRem.clear();
if (this.overridesAdd.isEmpty())
set &= ~PERMISSION;
});
return this;
}
/**
* Adds an override for the specified {@link net.dv8tion.jda.core.entities.IPermissionHolder IPermissionHolder}
* with the provided raw bitmasks as allowed and denied permissions. If the permission holder already
* had an override on this channel it will be updated instead.
*
* @param permHolder
* The permission holder
* @param allow
* The bitmask to grant
* @param deny
* The bitmask to deny
*
* @throws java.lang.IllegalArgumentException
* If the provided permission holder is {@code null}
* @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.core.Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* in this channel
*
* @return ChannelManager for chaining convenience
*
* @see #putPermissionOverride(IPermissionHolder, Collection, Collection)
* @see net.dv8tion.jda.core.Permission#getRaw(Permission...) Permission.getRaw(Permission...)
*/
@CheckReturnValue
public ChannelManager putPermissionOverride(IPermissionHolder permHolder, long allow, long deny)
{
Checks.notNull(permHolder, "PermissionHolder");
Checks.check(permHolder.getGuild().equals(getGuild()), "PermissionHolder is not from the same Guild!");
if (isPermissionChecksEnabled() && !getGuild().getSelfMember().hasPermission(getChannel(), Permission.MANAGE_PERMISSIONS))
throw new InsufficientPermissionException(Permission.MANAGE_PERMISSIONS);
final long id = getId(permHolder);
final int type = permHolder instanceof Role ? PermOverrideData.ROLE_TYPE : PermOverrideData.MEMBER_TYPE;
withLock(lock, (lock) ->
{
this.overridesRem.remove(id);
this.overridesAdd.put(id, new PermOverrideData(type, id, allow, deny));
set |= PERMISSION;
});
return this;
}
/**
* Adds an override for the specified {@link net.dv8tion.jda.core.entities.IPermissionHolder IPermissionHolder}
* with the provided permission sets as allowed and denied permissions. If the permission holder already
* had an override on this channel it will be updated instead.
* <br>Example: {@code putPermissionOverride(guild.getSelfMember(), EnumSet.of(Permission.MESSAGE_WRITE, Permission.MESSAGE_READ), null)}
*
* @param permHolder
* The permission holder
* @param allow
* The permissions to grant, or null
* @param deny
* The permissions to deny, or null
*
* @throws java.lang.IllegalArgumentException
* If the provided permission holder is {@code null}
* @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.core.Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* in this channel
*
* @return ChannelManager for chaining convenience
*
* @see #putPermissionOverride(IPermissionHolder, long, long)
* @see java.util.EnumSet EnumSet
*/
@CheckReturnValue
public ChannelManager putPermissionOverride(IPermissionHolder permHolder, Collection<Permission> allow, Collection<Permission> deny)
{
long allowRaw = allow == null ? 0 : Permission.getRaw(allow);
long denyRaw = deny == null ? 0 : Permission.getRaw(deny);
return putPermissionOverride(permHolder, allowRaw, denyRaw);
}
/**
* Removes the {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverride} for the specified
* {@link net.dv8tion.jda.core.entities.IPermissionHolder IPermissionHolder}. If no override existed for this member
* this does nothing.
*
* @param permHolder
* The permission holder
*
* @throws java.lang.IllegalArgumentException
* If the provided permission holder is {@code null}
* @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.core.Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* in this channel
*
* @return ChannelManager for chaining convenience
*/
@CheckReturnValue
public ChannelManager removePermissionOverride(IPermissionHolder permHolder)
{
Checks.notNull(permHolder, "PermissionHolder");
Checks.check(permHolder.getGuild().equals(getGuild()), "PermissionHolder is not from the same Guild!");
if (isPermissionChecksEnabled() && !getGuild().getSelfMember().hasPermission(getChannel(), Permission.MANAGE_PERMISSIONS))
throw new InsufficientPermissionException(Permission.MANAGE_PERMISSIONS);
final long id = getId(permHolder);
withLock(lock, (lock) ->
{
this.overridesRem.add(id);
this.overridesAdd.remove(id);
set |= PERMISSION;
});
return this;
}
/**
* Syncs all {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides} of this Channel with
* its parent ({@link net.dv8tion.jda.core.entities.Category Category}).
*
* <p>After this operation, all {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides}
* will be exactly the same as the ones from the parent.
* <br><b>That means that all current PermissionOverrides are lost!</b>
*
* <p>This behaves as if calling {@link #sync(Channel)} with this Channel's {@link net.dv8tion.jda.core.entities.Channel#getParent() Parent}.
*
* @throws java.lang.IllegalStateException
* If this Channel has no parent
* @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.core.Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* in this channel
*
* @return ChannelManager for chaining convenience
*
* @see <a href="https://discordapp.com/developers/docs/topics/permissions#permission-syncing" target="_blank">Discord Documentation - Permission Syncing</a>
*/
@CheckReturnValue
public ChannelManager sync()
{
if(getChannel().getParent() == null)
throw new IllegalStateException("sync() requires a parent category");
return sync(getChannel().getParent());
}
/**
* Syncs all {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides} of this Channel with
* the given ({@link net.dv8tion.jda.core.entities.Channel Channel}).
*
* <p>After this operation, all {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides}
* will be exactly the same as the ones from the syncSource.
* <br><b>That means that all current PermissionOverrides are lost!</b>
*
* <p>This will only work for Channels of the same {@link net.dv8tion.jda.core.entities.Guild Guild}!.
*
* @param syncSource
* The Channel from where all PermissionOverrides should be copied from
*
* @throws java.lang.IllegalArgumentException
* If the given snySource is {@code null}, this Channel or from a different Guild.
* @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.core.Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* in this channel
*
* @return ChannelManager for chaining convenience
*
* @see <a href="https://discordapp.com/developers/docs/topics/permissions#permission-syncing" target="_blank">Discord Documentation - Permission Syncing</a>
*/
@CheckReturnValue
public ChannelManager sync(Channel syncSource)
{
Checks.notNull(syncSource, "SyncSource");
Checks.check(getGuild().equals(syncSource.getGuild()), "Sync only works for channels of same guild");
if(syncSource.equals(getChannel()))
return this;
if (isPermissionChecksEnabled() && !getGuild().getSelfMember().hasPermission(getChannel(), Permission.MANAGE_PERMISSIONS))
throw new InsufficientPermissionException(Permission.MANAGE_PERMISSIONS);
withLock(lock, (lock) ->
{
this.overridesRem.clear();
this.overridesAdd.clear();
//set all current overrides to-be-removed
getChannel().getPermissionOverrides().forEach(permO ->
this.overridesRem.add(getId(permO.isRoleOverride() ? permO.getRole() : permO.getMember()))
);
//re-add all perm-overrides of syncSource
syncSource.getPermissionOverrides().forEach(permO ->
{
int type = permO.isRoleOverride() ? PermOverrideData.ROLE_TYPE : PermOverrideData.MEMBER_TYPE;
long id = getId(permO.isRoleOverride() ? permO.getRole() : permO.getMember());
this.overridesRem.remove(id);
this.overridesAdd.put(id, new PermOverrideData(type, id, permO.getAllowedRaw(), permO.getDeniedRaw()));
});
set |= PERMISSION;
});
return this;
}
/**
* Sets the <b><u>name</u></b> of the selected {@link net.dv8tion.jda.core.entities.Channel Channel}.
*
* <p>A channel name <b>must not</b> be {@code null} nor empty or more than 100 characters long!
* <br>TextChannel names may only be populated with alphanumeric (with underscore and dash).
*
* <p><b>Example</b>: {@code mod-only} or {@code generic_name}
* <br>Characters will automatically be lowercased by Discord for text channels!
*
* @param name
* The new name for the selected {@link net.dv8tion.jda.core.entities.Channel Channel}
*
* @throws IllegalArgumentException
* If the provided name is {@code null} or not between 1-100 characters long
*
* @return ChannelManager for chaining convenience
*/
@CheckReturnValue
public ChannelManager setName(String name)
{
Checks.notBlank(name, "Name");
Checks.check(name.length() > 0 && name.length() <= 100, "Name must be between 1-100 characters long");
if (getType() == ChannelType.TEXT)
Checks.noWhitespace(name, "Name");
this.name = name;
set |= NAME;
return this;
}
/**
* Sets the <b><u>{@link net.dv8tion.jda.core.entities.Category Parent Category}</u></b>
* of the selected {@link net.dv8tion.jda.core.entities.Channel Channel}.
*
*
* @param category
* The new parent for the selected {@link net.dv8tion.jda.core.entities.Channel Channel}
*
* @throws IllegalStateException
* If the target is a category itself
* @throws IllegalArgumentException
* If the provided category is not from the same Guild
*
* @return ChannelManager for chaining convenience
*
* @since 3.4.0
*/
@CheckReturnValue
public ChannelManager setParent(Category category)
{
if (category != null)
{
if (getType() == ChannelType.CATEGORY)
throw new IllegalStateException("Cannot set the parent of a category");
Checks.check(category.getGuild().equals(getGuild()), "Category is not from the same guild");
}
this.parent = category == null ? null : category.getId();
set |= PARENT;
return this;
}
/**
* Sets the <b><u>position</u></b> of the selected {@link net.dv8tion.jda.core.entities.Channel Channel}.
*
* <p><b>To modify multiple channels you should use
* <code>Guild.{@link net.dv8tion.jda.core.managers.GuildController getController()}.{@link GuildController#modifyTextChannelPositions() modifyTextChannelPositions()}</code>
* instead! This is not the same as looping through channels and using this to update positions!</b>
*
* @param position
* The new position for the selected {@link net.dv8tion.jda.core.entities.Channel Channel}
*
* @return ChannelManager for chaining convenience
*/
@CheckReturnValue
public ChannelManager setPosition(int position)
{
this.position = position;
set |= POSITION;
return this;
}
/**
* Sets the <b><u>topic</u></b> of the selected {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}.
*
* <p>A channel topic <b>must not</b> be more than {@code 1024} characters long!
* <br><b>This is only available to {@link net.dv8tion.jda.core.entities.TextChannel TextChannels}</b>
*
* @param topic
* The new topic for the selected {@link net.dv8tion.jda.core.entities.TextChannel TextChannel},
* {@code null} or empty String to reset
*
* @throws UnsupportedOperationException
* If the selected {@link net.dv8tion.jda.core.entities.Channel Channel}'s type is not {@link net.dv8tion.jda.core.entities.ChannelType#TEXT TEXT}
* @throws IllegalArgumentException
* If the provided topic is greater than {@code 1024} in length
*
* @return ChannelManager for chaining convenience
*/
@CheckReturnValue
public ChannelManager setTopic(String topic)
{
if (getType() != ChannelType.TEXT)
throw new IllegalStateException("Can only set topic on text channels");
Checks.check(topic == null || topic.length() <= 1024, "Topic must be less or equal to 1024 characters in length");
this.topic = topic;
set |= TOPIC;
return this;
}
/**
* Sets the <b><u>nsfw flag</u></b> of the selected {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}.
*
* @param nsfw
* The new nsfw flag for the selected {@link net.dv8tion.jda.core.entities.TextChannel TextChannel},
*
* @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException
* If the currently logged in account does not have the Permission {@link net.dv8tion.jda.core.Permission#MANAGE_CHANNEL MANAGE_CHANNEL}
* @throws IllegalStateException
* If the selected {@link net.dv8tion.jda.core.entities.Channel Channel}'s type is not {@link net.dv8tion.jda.core.entities.ChannelType#TEXT TEXT}
*
* @return ChannelManager for chaining convenience
*/
@CheckReturnValue
public ChannelManager setNSFW(boolean nsfw)
{
if (getType() != ChannelType.TEXT)
throw new IllegalStateException("Can only set nsfw on text channels");
this.nsfw = nsfw;
set |= NSFW;
return this;
}
/**
* Sets the <b><u>slowmode</u></b> of the selected {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}.
* <br>Provide {@code 0} to reset the slowmode of the {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
*
* <p>A channel slowmode <b>must not</b> be negative nor greater than {@code 120}!
* <br><b>This is only available to {@link net.dv8tion.jda.core.entities.TextChannel TextChannels}</b>
*
* <p>Note that only {@link net.dv8tion.jda.core.AccountType#CLIENT CLIENT} type accounts are
* affected by slowmode, and that {@link net.dv8tion.jda.core.AccountType#BOT BOT} accounts
* are immune to the restrictions.
* <br>Having {@link net.dv8tion.jda.core.Permission#MESSAGE_MANAGE MESSAGE_MANAGE} or
* {@link net.dv8tion.jda.core.Permission#MANAGE_CHANNEL MANAGE_CHANNEL} permission also
* grants immunity to slowmode.
*
* @param slowmode
* The new slowmode for the selected {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
*
* @throws IllegalStateException
* If the selected {@link net.dv8tion.jda.core.entities.Channel Channel}'s type is not {@link net.dv8tion.jda.core.entities.ChannelType#TEXT TEXT}
* @throws IllegalArgumentException
* If the provided slowmode is negative or greater than {@code 120}
*
* @return ChannelManager for chaining convenience
*/
@CheckReturnValue
public ChannelManager setSlowmode(int slowmode)
{
if (getType() != ChannelType.TEXT)
throw new IllegalStateException("Can only set slowmode on text channels");
Checks.check(slowmode <= 120 && slowmode >= 0, "Slowmode per user must be between 0 and 120 (seconds)!");
this.slowmode = slowmode;
set |= SLOWMODE;
return this;
}
/**
* Sets the <b><u>user-limit</u></b> of the selected {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}.
* <br>Provide {@code 0} to reset the user-limit of the {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}
*
* <p>A channel user-limit <b>must not</b> be negative nor greater than {@code 99}!
* <br><b>This is only available to {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannels}</b>
*
* @param userLimit
* The new user-limit for the selected {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}
*
* @throws IllegalStateException
* If the selected {@link net.dv8tion.jda.core.entities.Channel Channel}'s type is not {@link net.dv8tion.jda.core.entities.ChannelType#VOICE VOICE}
* @throws IllegalArgumentException
* If the provided user-limit is negative or greater than {@code 99}
*
* @return ChannelManager for chaining convenience
*/
@CheckReturnValue
public ChannelManager setUserLimit(int userLimit)
{
if (getType() != ChannelType.VOICE)
throw new IllegalStateException("Can only set userlimit on voice channels");
Checks.notNegative(userLimit, "Userlimit");
Checks.check(userLimit <= 99, "Userlimit may not be greater than 99");
this.userlimit = userLimit;
set |= USERLIMIT;
return this;
}
/**
* Sets the <b><u>bitrate</u></b> of the selected {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}.
* <br>The default value is {@code 64000}
*
* <p>A channel bitrate <b>must not</b> be less than {@code 8000} nor greater than {@code 96000} (for non-vip Guilds)!
* {@link net.dv8tion.jda.core.entities.Guild#getFeatures() VIP Guilds} allow a bitrate for up to {@code 128000}.
* <br><b>This is only available to {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannels}</b>
*
* @param bitrate
* The new bitrate for the selected {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}
*
* @throws IllegalStateException
* If the selected {@link net.dv8tion.jda.core.entities.Channel Channel}'s type is not {@link net.dv8tion.jda.core.entities.ChannelType#VOICE VOICE}
* @throws IllegalArgumentException
* If the provided bitrate is not between 8000-96000 (or 128000 for VIP Guilds)
*
* @return ChannelManager for chaining convenience
*
* @see net.dv8tion.jda.core.entities.Guild#getFeatures()
*/
@CheckReturnValue
public ChannelManager setBitrate(int bitrate)
{
if (getType() != ChannelType.VOICE)
throw new IllegalStateException("Can only set bitrate on voice channels");
final int maxBitrate = getGuild().getFeatures().contains("VIP_REGIONS") ? 128000 : 96000;
Checks.check(bitrate >= 8000, "Bitrate must be greater or equal to 8000");
Checks.check(bitrate <= maxBitrate, "Bitrate must be less or equal to %s", maxBitrate);
this.bitrate = bitrate;
set |= BITRATE;
return this;
}
@Override
protected RequestBody finalizeData()
{
JSONObject frame = new JSONObject().put("name", getChannel().getName());
if (shouldUpdate(NAME))
frame.put("name", name);
if (shouldUpdate(POSITION))
frame.put("position", position);
if (shouldUpdate(TOPIC))
frame.put("topic", opt(topic));
if (shouldUpdate(NSFW))
frame.put("nsfw", nsfw);
if (shouldUpdate(SLOWMODE))
frame.put("rate_limit_per_user", slowmode);
if (shouldUpdate(USERLIMIT))
frame.put("user_limit", userlimit);
if (shouldUpdate(BITRATE))
frame.put("bitrate", bitrate);
if (shouldUpdate(PARENT))
frame.put("parent_id", opt(parent));
withLock(lock, (lock) ->
{
if (shouldUpdate(PERMISSION))
frame.put("permission_overwrites", getOverrides());
});
reset();
return getRequestBody(frame);
}
@Override
protected boolean checkPermissions()
{
final Member selfMember = getGuild().getSelfMember();
if (!selfMember.hasPermission(getChannel(), Permission.MANAGE_CHANNEL))
throw new InsufficientPermissionException(Permission.MANAGE_CHANNEL);
return super.checkPermissions();
}
protected Collection<PermOverrideData> getOverrides()
{
//note: overridesAdd and overridesRem are mutually disjoint
TLongObjectHashMap<PermOverrideData> data = new TLongObjectHashMap<>(this.overridesAdd);
AbstractChannelImpl<?> impl = (AbstractChannelImpl<?>) getChannel();
impl.getOverrideMap().forEachEntry((id, override) ->
{
//removed by not adding them here, this data set overrides the existing one
//we can use remove because it will be reset afterwards either way
if (!overridesRem.remove(id) && !data.containsKey(id))
data.put(id, new PermOverrideData(override));
return true;
});
return data.valueCollection();
}
protected long getId(IPermissionHolder holder)
{
if (holder instanceof Role)
return ((Role) holder).getIdLong();
else
return ((Member) holder).getUser().getIdLong();
}
}