/
GuildController.java
885 lines (804 loc) · 39.6 KB
/
GuildController.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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/*
* Copyright 2015-2016 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.*;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import net.dv8tion.jda.core.entities.impl.MemberImpl;
import net.dv8tion.jda.core.exceptions.GuildUnavailableException;
import net.dv8tion.jda.core.exceptions.PermissionException;
import net.dv8tion.jda.core.requests.*;
import net.dv8tion.jda.core.utils.PermissionUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.*;
import java.util.stream.Collectors;
public class GuildController
{
protected final Guild guild;
public GuildController(Guild guild)
{
this.guild = guild;
}
public Guild getGuild()
{
return guild;
}
public JDA getJDA()
{
return guild.getJDA();
}
/**
* Changes a member's nickname in this guild.
* The nickname is visible to all members of this guild.<br>
* This requires
* ({@link net.dv8tion.jda.core.Permission#NICKNAME_MANAGE NICKNAME_MANAGE} for others and self, however if only modifying self,
* then only {@link net.dv8tion.jda.core.Permission#NICKNAME_CHANGE NICKNAME_CHANGE} is needed.
*
* @param member
* The member for which the nickname should be changed.
* @param nickname
* The new nickname of the member, or null / "" to reset
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* <ul>
* <li>If attempting to set nickname for self and the logged in account has neither {@link net.dv8tion.jda.core.Permission#NICKNAME_CHANGE}
* nor {@link net.dv8tion.jda.core.Permission#NICKNAME_MANAGE}</li>
* <li>If attempting to set nickname for another user and the logged in account does not have {@link net.dv8tion.jda.core.Permission#NICKNAME_MANAGE}</li>
* <li>If attempting to set nickname for another user and the logged in account cannot manipulate the other user due to permission hierarchy position.</li>
* </ul>
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> setNickname(Member member, String nickname)
{
checkAvailable();
checkNull(member, "member");
checkGuild(member.getGuild(), "member");
if(member.equals(guild.getSelfMember()))
{
if(!PermissionUtil.checkPermission(guild, member, Permission.NICKNAME_CHANGE)
&& !PermissionUtil.checkPermission(guild, member, Permission.NICKNAME_MANAGE))
throw new PermissionException(Permission.NICKNAME_CHANGE, "You neither have NICKNAME_CHANGE nor NICKNAME_MANAGE permission!");
}
else
{
checkPermission(Permission.NICKNAME_MANAGE);
checkPosition(member);
}
if (Objects.equals(nickname, member.getNickname()))
return new RestAction.EmptyRestAction<Void>(null);
if (nickname == null)
nickname = "";
JSONObject body = new JSONObject().put("nick", nickname);
Route.CompiledRoute route;
if (member.equals(guild.getSelfMember()))
route = Route.Guilds.MODIFY_SELF_NICK.compile(guild.getId());
else
route = Route.Guilds.MODIFY_MEMBER.compile(guild.getId(), member.getUser().getId());
return new RestAction<Void>(guild.getJDA(), route, body)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
/**
* Used to move a {@link net.dv8tion.jda.core.entities.Member Member} from one {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}
* to another {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}.<br>
* As a note, you cannot move a Member that isn't already in a VoiceChannel. Also they must be in a VoiceChannel
* in the same Guild as the one that you are moving them to.
*
* @param member
* The {@link net.dv8tion.jda.core.entities.Member Member} that you are moving.
* @param voiceChannel
* The destination {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel} to which the member is being
* moved to.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws java.lang.IllegalStateException
* If the Member isn't currently in a VoiceChannel in this Guild.
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the provided Member isn't part of this {@link net.dv8tion.jda.core.entities.Guild Guild}</li>
* <li>If the provided VoiceChannel isn't part of this {@link net.dv8tion.jda.core.entities.Guild Guild}</li>
* </ul>
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* <ul>
* <li>If this account doesn't have {@link Permission#VOICE_MOVE_OTHERS} in the VoiceChannel that
* the Member is currently in.</li>
* <li>If this account <b>AND</b> the Member being moved don't have
* {@link Permission#VOICE_CONNECT} for the destination VoiceChannel.</li>
* </ul>
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> moveVoiceMember(Member member, VoiceChannel voiceChannel)
{
checkAvailable();
checkNull(member, "member");
checkNull(member, "voiceChannel");
checkGuild(member.getGuild(), "member");
checkGuild(voiceChannel.getGuild(), "voiceChannel");
GuildVoiceState vState = member.getVoiceState();
if (!vState.inVoiceChannel())
throw new IllegalStateException("You cannot move a Member who isn't in a VoiceChannel!");
if (!PermissionUtil.checkPermission(vState.getChannel(), guild.getSelfMember(), Permission.VOICE_MOVE_OTHERS))
throw new PermissionException(Permission.VOICE_MOVE_OTHERS, "This account does not have Permission to MOVE_OTHERS out of the channel that the Member is currently in.");
if (!PermissionUtil.checkPermission(voiceChannel, guild.getSelfMember(), Permission.VOICE_CONNECT)
&& !PermissionUtil.checkPermission(voiceChannel, member, Permission.VOICE_CONNECT))
throw new PermissionException(Permission.VOICE_CONNECT,
"Neither this account nor the Member that is attempting to be moved have the VOICE_CONNECT permission " +
"for the destination VoiceChannel, so the move cannot be done.");
JSONObject body = new JSONObject().put("channel_id", voiceChannel.getId());
Route.CompiledRoute route = Route.Guilds.MODIFY_MEMBER.compile(guild.getId(), member.getUser().getId());
return new RestAction<Void>(guild.getJDA(), route, body)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
/**
* This method will prune (kick) all members who were offline for at least <i>days</i> days.<br>
* The RestAction returned from this method will return the amount of Members that were pruned.<br>
* You can use {@link #getPrunableMemberCount(int)} to determine how many Members would be pruned if you were to
* call this method.
*
* @param days
* Minimum number of days since a member has been offline to get affected.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: {@link java.lang.Integer Integer}<br>
* <b>Value</b>: The amount of Members that were pruned from the Guild.
* @throws PermissionException
* If the account doesn't have {@link net.dv8tion.jda.core.Permission#KICK_MEMBERS KICK_MEMBER} Permission.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Integer> prune(int days)
{
checkAvailable();
checkPermission(Permission.KICK_MEMBERS);
if (days < 1)
throw new IllegalArgumentException("Days amount must be at minimum 1 day.");
Route.CompiledRoute route = Route.Guilds.PRUNE_MEMBERS.compile(guild.getId(), Integer.toString(days));
return new RestAction<Integer>(guild.getJDA(), route, null)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(response.getObject().getInt("pruned"));
else
request .onFailure(response);
}
};
}
/**
* The method calculates the amount of Members that would be pruned if {@link #prune(int)} was executed.
* Prunability is determined by a member being offline for at least <i>days</i> days.<br>
*
* @param days
* Minimum number of days since a member has been offline to get affected.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: {@link java.lang.Integer Integer}<br>
* <b>Value</b>: The amount of Members that would be pruned.
* @throws PermissionException
* If the account doesn't have {@link net.dv8tion.jda.core.Permission#KICK_MEMBERS KICK_MEMBER} Permission.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Integer> getPrunableMemberCount(int days)
{
checkAvailable();
checkPermission(Permission.KICK_MEMBERS);
if (days < 1)
throw new IllegalArgumentException("Days amount must be at minimum 1 day.");
Route.CompiledRoute route = Route.Guilds.PRUNABLE_COUNT.compile(guild.getId(), Integer.toString(days));
return new RestAction<Integer>(guild.getJDA(), route, null)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(response.getObject().getInt("pruned"));
else
request .onFailure(response);
}
};
}
/**
* Kicks a {@link net.dv8tion.jda.core.entities.Member Member} from the {@link net.dv8tion.jda.core.entities.Guild Guild}.
* <p>
* <b>Note:</b> {@link net.dv8tion.jda.core.entities.Guild#getMembers()} will still contain the {@link net.dv8tion.jda.core.entities.User User}
* until Discord sends the {@link net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent GuildMemberLeaveEvent}.
*
* @param member
* The {@link net.dv8tion.jda.core.entities.Member Member} to kick from the from the {@link net.dv8tion.jda.core.entities.Guild Guild}.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* <ul>
* <li>If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#KICK_MEMBERS} permission.</li>
* <li>If the logged in account cannot kick the other member due to permission hierarchy position.</li>
* </ul>
* @throws java.lang.IllegalArgumentException
* If the provided member is not a Member of this Guild.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> kick(Member member)
{
checkAvailable();
checkNull(member, "member");
checkGuild(member.getGuild(), "member");
checkPermission(Permission.KICK_MEMBERS);
checkPosition(member);
Route.CompiledRoute route = Route.Guilds.KICK_MEMBER.compile(guild.getId(), member.getUser().getId());
return new RestAction<Void>(guild.getJDA(), route, null)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
/**
* Kicks the {@link net.dv8tion.jda.core.entities.Member Member} specified by the userId from the from the {@link net.dv8tion.jda.core.entities.Guild Guild}.
* <p>
* <b>Note:</b> {@link net.dv8tion.jda.core.entities.Guild#getMembers()} will still contain the {@link net.dv8tion.jda.core.entities.User User}
* until Discord sends the {@link net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent GuildMemberLeaveEvent}.
*
* @param userId
* The id of the {@link net.dv8tion.jda.core.entities.User User} to kick from the from the {@link net.dv8tion.jda.core.entities.Guild Guild}.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* <ul>
* <li>If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#KICK_MEMBERS} permission.</li>
* <li>If the logged in account cannot kick the other member due to permission hierarchy position.</li>
* </ul>
* @throws java.lang.IllegalArgumentException
* If the userId provided does not correspond to a Member in this Guild.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> kick(String userId)
{
checkNull(userId, "userId");
Member member = guild.getMemberById(userId);
if (member == null)
throw new IllegalArgumentException("The provided userId does not correspond to a member in this guild! Provided userId: " + userId);
return kick(member);
}
/**
* Bans a {@link net.dv8tion.jda.core.entities.Member Member} and deletes messages sent by the user
* based on the amount of delDays.<br>
* If you wish to ban a member without deleting any messages, provide delDays with a value of 0.
* This change will be applied immediately.
* <p>
* <b>Note:</b> {@link net.dv8tion.jda.core.entities.Guild#getMembers()} will still contain the
* {@link net.dv8tion.jda.core.entities.Member Member} until Discord sends the
* {@link net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent GuildMemberLeaveEvent}.
*
* @param member
* The {@link net.dv8tion.jda.core.entities.Member Member} to ban.
* @param delDays
* The history of messages, in days, that will be deleted.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* <ul>
* <li>If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#BAN_MEMBERS} permission.</li>
* <li>If the logged in account cannot ban the other user due to permission hierarchy position.</li>
* </ul>
* @throws java.lang.IllegalArgumentException
* If the provided amount of days (delDays) is less than 0.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> ban(Member member, int delDays)
{
checkAvailable();
checkNull(member, "member");
//Don't check if the provided member is from this guild. It doesn't matter if they are or aren't.
return ban(member.getUser(), delDays);
}
/**
* Bans a {@link net.dv8tion.jda.core.entities.User User} and deletes messages sent by the user
* based on the amount of delDays.<br>
* If you wish to ban a user without deleting any messages, provide delDays with a value of 0.
* This change will be applied immediately.
* <p>
* <b>Note:</b> {@link net.dv8tion.jda.core.entities.Guild#getMembers()} will still contain the {@link net.dv8tion.jda.core.entities.User User's}
* {@link net.dv8tion.jda.core.entities.Member Member} object (if the User was in the Guild)
* until Discord sends the {@link net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent GuildMemberLeaveEvent}.
*
* @param user
* The {@link net.dv8tion.jda.core.entities.User User} to ban.
* @param delDays
* The history of messages, in days, that will be deleted.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* <ul>
* <li>If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#BAN_MEMBERS} permission.</li>
* <li>If the logged in account cannot ban the other user due to permission hierarchy position.</li>
* </ul>
* @throws java.lang.IllegalArgumentException
* If the provided amount of days (delDays) is less than 0.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> ban(User user, int delDays)
{
checkAvailable();
checkNull(user, "user");
checkPermission(Permission.BAN_MEMBERS);
if (guild.isMember(user)) // If user is in guild. Check if we are able to ban.
checkPosition(guild.getMember(user));
if (delDays < 0)
throw new IllegalArgumentException("Provided delDays cannot be less that 0. How can you delete messages that are -1 days old?");
Route.CompiledRoute route;
if (delDays > 0)
route = Route.Guilds.BAN_WITH_DELETE.compile(guild.getId(), user.getId(), Integer.toString(delDays));
else
route = Route.Guilds.BAN.compile(guild.getId(), user.getId());
return new RestAction<Void>(guild.getJDA(), route, null)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
/**
* Bans the a user specified by the userId and deletes messages sent by the user
* based on the amount of delDays.<br>
* If you wish to ban a user without deleting any messages, provide delDays with a value of 0.
* This change will be applied immediately.
* <p>
* <b>Note:</b> {@link net.dv8tion.jda.core.entities.Guild#getMembers()} will still contain the {@link net.dv8tion.jda.core.entities.User User's}
* {@link net.dv8tion.jda.core.entities.Member Member} object (if the User was in the Guild)
* until Discord sends the {@link net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent GuildMemberLeaveEvent}.
*
* @param userId
* The id of the {@link net.dv8tion.jda.core.entities.User User} to ban.
* @param delDays
* The history of messages, in days, that will be deleted.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* <ul>
* <li>If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#BAN_MEMBERS} permission.</li>
* <li>If the logged in account cannot ban the other user due to permission hierarchy position.</li>
* </ul>
* @throws java.lang.IllegalArgumentException
* If the provided amount of days (delDays) is less than 0.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> ban(String userId, int delDays)
{
checkAvailable();
checkNull(userId, "userId");
checkPermission(Permission.BAN_MEMBERS);
User user = guild.getJDA().getUserById(userId);
if (user != null) // If we have the user cached then we should use the additional information available to use during the ban process.
{
return ban(user, delDays);
}
Route.CompiledRoute route;
if (delDays > 0)
route = Route.Guilds.BAN_WITH_DELETE.compile(guild.getId(), userId, Integer.toString(delDays));
else
route = Route.Guilds.BAN.compile(guild.getId(), userId);
return new RestAction<Void>(guild.getJDA(), route, null)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else if (response.code == 404)
request.onFailure(new IllegalArgumentException("User with provided id \"" + userId + "\" does not exist! Cannot ban a non-existent user!"));
else
request.onFailure(response);
}
};
}
/**
* Unbans the specified {@link net.dv8tion.jda.core.entities.User User} from this Guild.
*
* @param user
* The id of the {@link net.dv8tion.jda.core.entities.User User} to unban.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#BAN_MEMBERS} permission.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> unban(User user)
{
checkNull(user, "user");
return unban(user.getId());
}
/**
* Unbans the a user specified by the userId from this Guild.
*
* @param userId
* The id of the {@link net.dv8tion.jda.core.entities.User User} to unban.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#BAN_MEMBERS} permission.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> unban(String userId)
{
checkAvailable();
checkNull(userId, "userId");
checkPermission(Permission.BAN_MEMBERS);
Route.CompiledRoute route = Route.Guilds.UNBAN.compile(guild.getId(), userId);
return new RestAction<Void>(guild.getJDA(), route, null)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else if (response.code == 404)
request.onFailure(new IllegalArgumentException("User with provided id \"" + userId + "\" does not exist! Cannot unban a non-existent user!"));
else
request.onFailure(response);
}
};
}
/**
* Sets the Guild Deafened state state of the {@link net.dv8tion.jda.core.entities.Member Member} based on the provided
* boolean.
* <p>
* <b>Note:</b> The Member's {@link net.dv8tion.jda.core.entities.GuildVoiceState#isGuildDeafened()} value won't change
* until JDA receives the {@link net.dv8tion.jda.core.events.guild.voice.GuildVoiceGuildDeafenEvent} event related to this change.
*
* @param member
* The {@link net.dv8tion.jda.core.entities.Member Member} who's {@link GuildVoiceState VoiceState} is being changed.
* @param deafen
* Whether this {@link net.dv8tion.jda.core.entities.Member Member} should be deafened or undeafened.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* <ul>
* <li>If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#VOICE_DEAF_OTHERS} permission.</li>
* <li>If the provided member is the Guild's owner. You cannot modify the owner of a Guild.</li>
* </ul>
* @throws java.lang.IllegalArgumentException
* If the provided member is not from this Guild.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> setDeafen(Member member, boolean deafen)
{
checkAvailable();
checkNull(member, "member");
checkGuild(member.getGuild(), "member");
checkPermission(Permission.VOICE_DEAF_OTHERS);
//We check the owner instead of Position because, apparently, Discord doesn't care about position for
// muting and deafening, only whether the affected Member is the owner.
if (guild.getOwner().equals(member))
throw new PermissionException("Cannot modified Guild Deafen status the Owner of the Guild");
if (member.getVoiceState().isGuildDeafened() == deafen)
return new RestAction.EmptyRestAction<Void>(null);
JSONObject body = new JSONObject().put("deaf", deafen);
Route.CompiledRoute route = Route.Guilds.MODIFY_MEMBER.compile(guild.getId(), member.getUser().getId());
return new RestAction<Void>(guild.getJDA(), route, body)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
/**
* Sets the Guild Muted state state of the {@link net.dv8tion.jda.core.entities.Member Member} based on the provided
* boolean.
* <p>
* <b>Note:</b> The Member's {@link net.dv8tion.jda.core.entities.GuildVoiceState#isGuildMuted()} value won't change
* until JDA receives the {@link net.dv8tion.jda.core.events.guild.voice.GuildVoiceGuildMuteEvent} event related to this change.
*
* @param member
* The {@link net.dv8tion.jda.core.entities.Member Member} who's {@link GuildVoiceState VoiceState} is being changed.
* @param mute
* Whether this {@link net.dv8tion.jda.core.entities.Member Member} should be muted or unmuted.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: Void<br>
* <b>Value</b>: None
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* <ul>
* <li>If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#VOICE_MUTE_OTHERS} permission.</li>
* <li>If the provided member is the Guild's owner. You cannot modify the owner of a Guild.</li>
* </ul>
* @throws java.lang.IllegalArgumentException
* If the provided member is not from this Guild.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> setMute(Member member, boolean mute)
{
checkAvailable();
checkNull(member, "member");
checkGuild(member.getGuild(), "member");
checkPermission(Permission.VOICE_MUTE_OTHERS);
//We check the owner instead of Position because, apparently, Discord doesn't care about position for
// muting and deafening, only whether the affected Member is the owner.
if (guild.getOwner().equals(member))
throw new PermissionException("Cannot modified Guild Mute status the Owner of the Guild");
if (member.getVoiceState().isGuildMuted() == mute)
return new RestAction.EmptyRestAction<Void>(null);
JSONObject body = new JSONObject().put("mute", mute);
Route.CompiledRoute route = Route.Guilds.MODIFY_MEMBER.compile(guild.getId(), member.getUser().getId());
return new RestAction<Void>(guild.getJDA(), route, body)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
/**
* Gets an unmodifiable list of the currently banned {@link net.dv8tion.jda.core.entities.User Users}.<br>
* If you wish to ban or unban a user, please {@link #ban(net.dv8tion.jda.core.entities.User, int)} or
* {@link #unban(net.dv8tion.jda.core.entities.User)};
*
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} - <br>
* <b>Type</b>: {@link java.util.List}<{@link net.dv8tion.jda.core.entities.User}><br>
* <b>Value</b>: An unmodifiable list of all users currently banned from this Guild.
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#BAN_MEMBERS} permission.
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<List<User>> getBans()
{
checkAvailable();
checkPermission(Permission.BAN_MEMBERS);
Route.CompiledRoute route = Route.Guilds.GET_BANS.compile(guild.getId());
return new RestAction<List<User>>(guild.getJDA(), route, null)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (!response.isOk())
{
request.onFailure(response);
return;
}
EntityBuilder builder = EntityBuilder.get(guild.getJDA());
List<User> bans = new LinkedList<>();
JSONArray bannedArr = response.getArray();
for (int i = 0; i < bannedArr.length(); i++)
{
JSONObject user = bannedArr.getJSONObject(i).getJSONObject("user");
bans.add(builder.createFakeUser(user, false));
}
request.onSuccess(Collections.unmodifiableList(bans));
}
};
}
public RestAction<Void> addRolesToMember(Member member, Role... roles)
{
return modifyMemberRoles(member, Arrays.asList(roles), Collections.emptyList());
}
public RestAction<Void> addRolesToMember(Member member, Collection<Role> roles)
{
return modifyMemberRoles(member, roles, Collections.emptyList());
}
public RestAction<Void> removeRolesFromMember(Member member, Role... roles)
{
return modifyMemberRoles(member, Collections.emptyList(), Arrays.asList(roles));
}
public RestAction<Void> removeRolesFromMember(Member member, Collection<Role> roles)
{
return modifyMemberRoles(member, Collections.emptyList(), roles);
}
public RestAction<Void> modifyMemberRoles(Member member, Collection<Role> rolesToAdd, Collection<Role> rolesToRemove)
{
checkAvailable();
checkNull(member, "member");
checkNull(rolesToAdd, "Collection containing roles to be added to the member");
checkNull(rolesToRemove, "Collection containing roles to be removed from the member");
checkGuild(member.getGuild(), "member");
checkPermission(Permission.MANAGE_ROLES);
rolesToAdd.forEach(role ->
{
checkNull(role, "role in rolesToAdd");
checkGuild(role.getGuild(), "role: " + role.toString());
checkPosition(role);
});
rolesToRemove.forEach(role ->
{
checkNull(role, "role in rolesToRemove");
checkGuild(role.getGuild(), "role: " + role.toString());
checkPosition(role);
});
Set<Role> currentRoles = new HashSet<>(((MemberImpl) member).getRoleSet());
currentRoles.addAll(rolesToAdd);
currentRoles.removeAll(rolesToRemove);
if (currentRoles.contains(guild.getPublicRole()))
throw new IllegalArgumentException("Cannot add the PublicRole of a Guild to a Member. All members have this role by default!");
JSONObject body = new JSONObject()
.put("roles", currentRoles.stream().map(Role::getId).collect(Collectors.toList()));
Route.CompiledRoute route = Route.Guilds.MODIFY_MEMBER.compile(guild.getId(), member.getUser().getId());
return new RestAction<Void>(guild.getJDA(), route, body)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
//Used to SET roles, not just modify the existing role list. e.g: The provided roles are the only ones the member
// will have afterwards
//Note: if no roles are provided, this member will have no roles.
public RestAction<Void> modifyMemberRoles(Member member, Role... roles)
{
return modifyMemberRoles(member, Arrays.asList(roles));
}
//Used to SET roles, not just modify the existing role list. e.g: The provided roles are the only ones the member
// will have afterwards.
//Note: if roles.isEmpty(), then this member will have no roles.
public RestAction<Void> modifyMemberRoles(Member member, Collection<Role> roles)
{
checkAvailable();
checkNull(member, "member");
checkNull(roles, "roles");
checkGuild(member.getGuild(), "member");
roles.forEach(role ->
{
checkNull(role, "role in collection");
checkGuild(role.getGuild(), "role: " + role.toString());
checkPosition(role);
});
if (roles.contains(guild.getPublicRole()))
throw new IllegalArgumentException("Cannot add the PublicRole of a Guild to a Member. All members have this role by default!");
//This is identical to the rest action stuff in #modifyMemberRoles(Member, Collection<Role>, Collection<Role>)
JSONObject body = new JSONObject()
.put("roles", roles.stream().map(Role::getId).collect(Collectors.toList()));
Route.CompiledRoute route = Route.Guilds.MODIFY_MEMBER.compile(guild.getId(), member.getUser().getId());
return new RestAction<Void>(guild.getJDA(), route, body)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
public RestAction<Void> transferOwnership(Member newOwner)
{
checkAvailable();
checkNull(newOwner, "newOwner member");
checkGuild(newOwner.getGuild(), "newOwner member");
if (!guild.getOwner().equals(guild.getSelfMember()))
throw new PermissionException("The logged in account must be the owner of this Guild to be able to transfer ownership");
if (guild.getSelfMember().equals(newOwner))
throw new IllegalArgumentException("The member provided as the newOwner is the currently logged in account. Provide a different member to give ownership to.");
JSONObject body = new JSONObject().put("owner_id", newOwner.getUser().getId());
Route.CompiledRoute route = Route.Guilds.MODIFY_GUILD.compile(guild.getId());
return new RestAction<Void>(guild.getJDA(), route, body)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
protected void checkAvailable()
{
if (!guild.isAvailable())
throw new GuildUnavailableException();
}
protected void checkNull(Object obj, String name)
{
if (obj == null)
throw new NullPointerException("Provided " + name + " was null!");
}
protected void checkGuild(Guild providedGuild, String comment)
{
if (!guild.equals(providedGuild))
throw new IllegalArgumentException("Provided " + comment + " is not part of this Guild!");
}
protected void checkPermission(Permission perm)
{
if (!PermissionUtil.checkPermission(guild, guild.getSelfMember(), perm))
throw new PermissionException(perm);
}
protected void checkPosition(Member member)
{
if(!PermissionUtil.canInteract(guild.getSelfMember(), member))
throw new PermissionException("Can't member a user with higher or equal highest role than yourself!");
}
protected void checkPosition(Role role)
{
if(!PermissionUtil.canInteract(guild.getSelfMember(), role))
throw new PermissionException("Can't modify a member with higher or equal highest role than yourself! Role: " + role.toString());
}
}