/
ChannelAction.java
569 lines (538 loc) · 23.1 KB
/
ChannelAction.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
/*
* 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.*;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
/**
* Extension of {@link net.dv8tion.jda.api.requests.RestAction RestAction} specifically
* designed to create a {@link net.dv8tion.jda.api.entities.GuildChannel GuildChannel}.
* This extension allows setting properties before executing the action.
*
* @since 3.0
*
* @see net.dv8tion.jda.api.entities.Guild
* @see net.dv8tion.jda.api.entities.Guild#createTextChannel(String)
* @see net.dv8tion.jda.api.entities.Guild#createVoiceChannel(String)
* @see net.dv8tion.jda.api.entities.Guild#createCategory(String)
* @see GuildChannel#createCopy()
* @see GuildChannel#createCopy(Guild)
*
* @param <T>
* The type of channel to create
*/
public interface ChannelAction<T extends GuildChannel> extends AuditableRestAction<T>
{
@Nonnull
@Override
ChannelAction<T> setCheck(@Nullable BooleanSupplier checks);
@Nonnull
@Override
ChannelAction<T> timeout(long timeout, @Nonnull TimeUnit unit);
@Nonnull
@Override
ChannelAction<T> deadline(long timestamp);
/**
* The guild to create this {@link GuildChannel} in
*
* @return The guild
*/
@Nonnull
Guild getGuild();
/**
* The {@link ChannelType} for the resulting channel
*
* @return The channel type
*/
@Nonnull
ChannelType getType();
/**
* Sets the name for the new GuildChannel
*
* @param name
* The not-null name for the new GuildChannel (1-100 chars long)
*
* @throws java.lang.IllegalArgumentException
* If the provided name is null or not between 1-100 chars long
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> setName(@Nonnull String name);
/**
* Sets the {@link net.dv8tion.jda.api.entities.Category Category} for the new GuildChannel.
*
* You can use {@link #syncPermissionOverrides()} to sync the channel with the category.
*
* @param category
* The parent for the new GuildChannel
*
* @throws UnsupportedOperationException
* If this ChannelAction is for a Category
* @throws IllegalArgumentException
* If the provided category is {@code null}
* or not from this Guild
*
* @return The current ChannelAction, for chaining convenience
*
* @see #syncPermissionOverrides()
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> setParent(@Nullable Category category);
/**
* Sets the position where the new Channel should be inserted into.
* This refers to the raw position value, not the computed (relative) position.
* <p>
* By default (or by providing this method with {@code null}),
* the position will automatically be computed based on the other Channels (inserted last in its respective group).
* <p>
* Note: This does not shift the position values of existing Channels if the values collide.
* <br>As a reminder: The ordering of Channels is determined first by its Category's position, then by its raw
* position value and finally by its id (younger Channels are below older ones)
*
* @param position
* The raw position value that should be used for the new Channel
*
* @throws IllegalArgumentException
* If the provided position value is {@code <0}
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> setPosition(@Nullable Integer position);
/**
* Sets the topic for the new TextChannel
*
* @param topic
* The topic for the new GuildChannel (max 1024 chars)
*
* @throws UnsupportedOperationException
* If this ChannelAction is not for a TextChannel
* @throws IllegalArgumentException
* If the provided topic is longer than 1024 chars
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> setTopic(@Nullable String topic);
/**
* Sets the NSFW flag for the new TextChannel
*
* @param nsfw
* The NSFW flag for the new GuildChannel
*
* @throws UnsupportedOperationException
* If this ChannelAction is not for a TextChannel
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> setNSFW(boolean nsfw);
/**
* Sets the slowmode value, which limits the amount of time that individual users must wait
* between sending messages in the new TextChannel. This is measured in seconds.
*
* <p>Note: Bots are unaffected by this.
* <br>Having {@link net.dv8tion.jda.api.Permission#MESSAGE_MANAGE MESSAGE_MANAGE} or
* {@link net.dv8tion.jda.api.Permission#MANAGE_CHANNEL MANAGE_CHANNEL} permission also
* grants immunity to slowmode.
*
* @param slowmode
* The number of seconds required to wait between sending messages in the channel.
*
* @throws UnsupportedOperationException
* If this ChannelAction is not for a TextChannel
* @throws IllegalArgumentException
* If the {@code slowmode} is greater than {@link net.dv8tion.jda.api.entities.TextChannel#MAX_SLOWMODE TextChannel.MAX_SLOWMODE}, or less than 0
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> setSlowmode(int slowmode);
/**
* Sets the news flag for the new TextChannel.
* Announcement-/News-Channels can be used to crosspost messages to other guilds.
*
* @param news
* The news flag for the new GuildChannel
*
* @throws UnsupportedOperationException
* If this ChannelAction is not for a TextChannel
* @throws java.lang.IllegalStateException
* If {@code news} is {@code true} and the guild doesn't have the NEWS feature
*
* @return The current ChannelAction, for chaining convenience
*
* @see net.dv8tion.jda.api.entities.TextChannel#isNews()
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> setNews(boolean news);
/**
* Adds a new Role or Member {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride}
* for the new GuildChannel.
*
* <p>If setting permission overwrites, only permissions your bot has in the guild can be allowed/denied.
*
* <p>Example:
* <pre>{@code
* Role role = guild.getPublicRole();
* EnumSet<Permission> allow = EnumSet.of(Permission.MESSAGE_READ);
* EnumSet<Permission> deny = EnumSet.of(Permission.MESSAGE_WRITE);
* channelAction.addPermissionOverride(role, allow, deny);
* }</pre>
*
* @param target
* The not-null {@link net.dv8tion.jda.api.entities.Role Role} or {@link net.dv8tion.jda.api.entities.Member Member} for the override
* @param allow
* The granted {@link net.dv8tion.jda.api.Permission Permissions} for the override or null
* @param deny
* The denied {@link net.dv8tion.jda.api.Permission Permissions} for the override or null
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If any permission is set in allow/deny that the currently logged in account is missing,
* unless {@link Permission#MANAGE_PERMISSIONS} or {@link Permission#MANAGE_ROLES} is granted to it within the context of the parent category.
* @throws java.lang.IllegalArgumentException
* If the specified target is null or not within the same guild.
*
* @return The current ChannelAction, for chaining convenience
*
* @see java.util.EnumSet
*/
@Nonnull
@CheckReturnValue
default ChannelAction<T> addPermissionOverride(@Nonnull IPermissionHolder target, @Nullable Collection<Permission> allow, @Nullable Collection<Permission> deny)
{
final long allowRaw = allow != null ? Permission.getRaw(allow) : 0;
final long denyRaw = deny != null ? Permission.getRaw(deny) : 0;
return addPermissionOverride(target, allowRaw, denyRaw);
}
/**
* Adds a new Role or Member {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride}
* for the new GuildChannel.
*
* <p>If setting permission overwrites, only permissions your bot has in the guild can be allowed/denied.
*
* <p>Example:
* <pre>{@code
* Role role = guild.getPublicRole();
* long allow = Permission.MESSAGE_READ.getRawValue();
* long deny = Permission.MESSAGE_WRITE.getRawValue() | Permission.MESSAGE_ADD_REACTION.getRawValue();
* channelAction.addPermissionOverride(role, allow, deny);
* }</pre>
*
* @param target
* The not-null {@link net.dv8tion.jda.api.entities.Role Role} or {@link net.dv8tion.jda.api.entities.Member Member} for the override
* @param allow
* The granted {@link net.dv8tion.jda.api.Permission Permissions} for the override.
* Use {@link net.dv8tion.jda.api.Permission#getRawValue()} to retrieve these Permissions.
* @param deny
* The denied {@link net.dv8tion.jda.api.Permission Permissions} for the override.
* Use {@link net.dv8tion.jda.api.Permission#getRawValue()} to retrieve these Permissions.
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If any permission is set in allow/deny that the currently logged in account is missing,
* unless {@link Permission#MANAGE_PERMISSIONS} or {@link Permission#MANAGE_ROLES} is granted to it within the context of the parent category.
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the specified target is null
* or not within the same guild.</li>
* <li>If one of the provided Permission values is invalid</li>
* </ul>
*
* @return The current ChannelAction, for chaining convenience
*
* @see net.dv8tion.jda.api.Permission#getRawValue()
* @see net.dv8tion.jda.api.Permission#getRaw(java.util.Collection)
* @see net.dv8tion.jda.api.Permission#getRaw(net.dv8tion.jda.api.Permission...)
*/
@Nonnull
@CheckReturnValue
default ChannelAction<T> addPermissionOverride(@Nonnull IPermissionHolder target, long allow, long deny)
{
Checks.notNull(target, "Override Role/Member");
if (target instanceof Role)
return addRolePermissionOverride(target.getIdLong(), allow, deny);
else if (target instanceof Member)
return addMemberPermissionOverride(target.getIdLong(), allow, deny);
throw new IllegalArgumentException("Cannot add override for " + target.getClass().getSimpleName());
}
/**
* Adds a new Member {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride}
* for the new GuildChannel.
*
* <p>If setting permission overwrites, only permissions your bot has in the guild can be allowed/denied.
*
* <p>Example:
* <pre>{@code
* long userId = user.getIdLong();
* EnumSet<Permission> allow = EnumSet.of(Permission.MESSAGE_READ);
* EnumSet<Permission> deny = EnumSet.of(Permission.MESSAGE_WRITE);
* channelAction.addMemberPermissionOverride(userId, allow, deny);
* }</pre>
*
* @param memberId
* The id for the member
* @param allow
* The granted {@link net.dv8tion.jda.api.Permission Permissions} for the override or null
* @param deny
* The denied {@link net.dv8tion.jda.api.Permission Permissions} for the override or null
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If any permission is set in allow/deny that the currently logged in account is missing,
* unless {@link Permission#MANAGE_PERMISSIONS} or {@link Permission#MANAGE_ROLES} is granted to it within the context of the parent category.
*
* @return The current ChannelAction, for chaining convenience
*
* @see java.util.EnumSet
*/
@Nonnull
@CheckReturnValue
default ChannelAction<T> addMemberPermissionOverride(long memberId, @Nullable Collection<Permission> allow, @Nullable Collection<Permission> deny)
{
final long allowRaw = allow != null ? Permission.getRaw(allow) : 0;
final long denyRaw = deny != null ? Permission.getRaw(deny) : 0;
return addMemberPermissionOverride(memberId, allowRaw, denyRaw);
}
/**
* Adds a new Role {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride}
* for the new GuildChannel.
*
* <p>If setting permission overwrites, only permissions your bot has in the guild can be allowed/denied.
*
* <p>Example:
* <pre>{@code
* long roleId = role.getIdLong();
* EnumSet<Permission> allow = EnumSet.of(Permission.MESSAGE_READ);
* EnumSet<Permission> deny = EnumSet.of(Permission.MESSAGE_WRITE);
* channelAction.addRolePermissionOverride(roleId, allow, deny);
* }</pre>
*
* @param roleId
* The id for the role
* @param allow
* The granted {@link net.dv8tion.jda.api.Permission Permissions} for the override or null
* @param deny
* The denied {@link net.dv8tion.jda.api.Permission Permissions} for the override or null
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If any permission is set in allow/deny that the currently logged in account is missing,
* unless {@link Permission#MANAGE_PERMISSIONS} or {@link Permission#MANAGE_ROLES} is granted to it within the context of the parent category.
*
* @return The current ChannelAction, for chaining convenience
*
* @see java.util.EnumSet
*/
@Nonnull
@CheckReturnValue
default ChannelAction<T> addRolePermissionOverride(long roleId, @Nullable Collection<Permission> allow, @Nullable Collection<Permission> deny)
{
final long allowRaw = allow != null ? Permission.getRaw(allow) : 0;
final long denyRaw = deny != null ? Permission.getRaw(deny) : 0;
return addRolePermissionOverride(roleId, allowRaw, denyRaw);
}
/**
* Adds a new Member {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride} for the new GuildChannel.
*
* <p>If setting permission overwrites, only permissions your bot has in the guild can be allowed/denied.
*
* <p>Example:
* <pre>{@code
* long userId = user.getIdLong();
* long allow = Permission.MESSAGE_READ.getRawValue();
* long deny = Permission.MESSAGE_WRITE.getRawValue() | Permission.MESSAGE_ADD_REACTION.getRawValue();
* channelAction.addMemberPermissionOverride(userId, allow, deny);
* }</pre>
*
* @param memberId
* The id for the member
* @param allow
* The granted {@link net.dv8tion.jda.api.Permission Permissions} for the override.
* Use {@link net.dv8tion.jda.api.Permission#getRawValue()} to retrieve these Permissions.
* @param deny
* The denied {@link net.dv8tion.jda.api.Permission Permissions} for the override.
* Use {@link net.dv8tion.jda.api.Permission#getRawValue()} to retrieve these Permissions.
*
* @throws java.lang.IllegalArgumentException
* If one of the provided Permission values is invalid
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If any permission is set in allow/deny that the currently logged in account is missing,
* unless {@link Permission#MANAGE_PERMISSIONS} or {@link Permission#MANAGE_ROLES} is granted to it within the context of the parent category.
*
* @return The current ChannelAction, for chaining convenience
*
* @see net.dv8tion.jda.api.Permission#getRawValue()
* @see net.dv8tion.jda.api.Permission#getRaw(java.util.Collection)
* @see net.dv8tion.jda.api.Permission#getRaw(net.dv8tion.jda.api.Permission...)
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> addMemberPermissionOverride(long memberId, long allow, long deny);
/**
* Adds a new Role {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride} for the new GuildChannel.
*
* <p>If setting permission overwrites, only permissions your bot has in the guild can be allowed/denied.
*
* <p>Example:
* <pre>{@code
* long roleId = role.getIdLong();
* long allow = Permission.MESSAGE_READ.getRawValue();
* long deny = Permission.MESSAGE_WRITE.getRawValue() | Permission.MESSAGE_ADD_REACTION.getRawValue();
* channelAction.addMemberPermissionOverride(roleId, allow, deny);
* }</pre>
*
* @param roleId
* The id for the role
* @param allow
* The granted {@link net.dv8tion.jda.api.Permission Permissions} for the override.
* Use {@link net.dv8tion.jda.api.Permission#getRawValue()} to retrieve these Permissions.
* @param deny
* The denied {@link net.dv8tion.jda.api.Permission Permissions} for the override.
* Use {@link net.dv8tion.jda.api.Permission#getRawValue()} to retrieve these Permissions.
*
* @throws java.lang.IllegalArgumentException
* If one of the provided Permission values is invalid
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If any permission is set in allow/deny that the currently logged in account is missing,
* unless {@link Permission#MANAGE_PERMISSIONS} or {@link Permission#MANAGE_ROLES} is granted to it within the context of the parent category.
*
* @return The current ChannelAction, for chaining convenience
*
* @see net.dv8tion.jda.api.Permission#getRawValue()
* @see net.dv8tion.jda.api.Permission#getRaw(java.util.Collection)
* @see net.dv8tion.jda.api.Permission#getRaw(net.dv8tion.jda.api.Permission...)
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> addRolePermissionOverride(long roleId, long allow, long deny);
/**
* Removes any existing override with the provided id.
* <br>If no override with the provided id exists, this method does nothing.
*
* @param id
* The member or role id of the override
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> removePermissionOverride(long id);
/**
* Removes any existing override with the provided id.
* <br>If no override with the provided id exists, this method does nothing.
*
* @param id
* The member or role id of the override
*
* @throws IllegalArgumentException
* If the provided string is not a valid snowflake or null
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
default ChannelAction<T> removePermissionOverride(@Nonnull String id)
{
return removePermissionOverride(MiscUtil.parseSnowflake(id));
}
/**
* Removes any existing override with the provided role/member.
* <br>If no override for the provided role/member exists, this method does nothing.
*
* @param holder
* The member or role of the override
*
* @throws IllegalArgumentException
* If the provided permission holder is null
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
default ChannelAction<T> removePermissionOverride(@Nonnull IPermissionHolder holder)
{
Checks.notNull(holder, "PermissionHolder");
return removePermissionOverride(holder.getIdLong());
}
/**
* Removes all currently configured permission overrides
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> clearPermissionOverrides();
/**
* Syncs the permission overrides of the channel with the category.
*
* <p>If setting permission overwrites, only permissions your bot has in the guild can be allowed/denied.
* In order to properly sync permissions the currently logged in account must have all allowed/denied permissions or {@link Permission#MANAGE_ROLES} in the parent category.
*
* @throws IllegalArgumentException
* If no parent has been configured. You have to use {@link #setParent(Category)} before calling this method.
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> syncPermissionOverrides();
/**
* Sets the bitrate for the new VoiceChannel
*
* @param bitrate
* The bitrate for the new VoiceChannel in {@code bps} (limits 8000 {@literal <}= bitrate {@literal <}= {@link Guild#getMaxBitrate()})
* or {@code null} to use the default 64kbps.
*
* @throws UnsupportedOperationException
* If this ChannelAction is not for a VoiceChannel
* @throws IllegalArgumentException
* If the provided bitrate is less than 8000 or greater than {@link net.dv8tion.jda.api.entities.Guild#getMaxBitrate()}
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> setBitrate(@Nullable Integer bitrate);
/**
* Sets the userlimit for the new VoiceChannel
*
* @param userlimit
* The userlimit for the new VoiceChannel or {@code null}/{@code 0} to use no limit,
*
* @throws UnsupportedOperationException
* If this ChannelAction is not for a VoiceChannel
* @throws IllegalArgumentException
* If the provided userlimit is negative or above {@code 99}
*
* @return The current ChannelAction, for chaining convenience
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> setUserlimit(@Nullable Integer userlimit);
}