/
MessageBuilder.java
1436 lines (1333 loc) · 52.2 KB
/
MessageBuilder.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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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;
import net.dv8tion.jda.annotations.DeprecatedSince;
import net.dv8tion.jda.annotations.ForRemoval;
import net.dv8tion.jda.annotations.ReplaceWith;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.ComponentLayout;
import net.dv8tion.jda.api.requests.restaction.MessageAction;
import net.dv8tion.jda.internal.entities.DataMessage;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.regex.Matcher;
/**
* Builder system used to build {@link net.dv8tion.jda.api.entities.Message Messages}.
* <br>Internally the builder uses a {@link java.lang.StringBuilder} to take advantage of the efficiencies offered by the
* StringBuilder, and the methods provided by this class are a combination of those offered by the StringBuilder and
* {@link String#format(String, Object...)}.
*
* @since 1.0
* @author Michael Ritter
* @author Aljoscha Grebe
*/
public class MessageBuilder implements Appendable
{
protected final StringBuilder builder = new StringBuilder();
protected final List<MessageEmbed> embeds = new ArrayList<>();
protected final List<ComponentLayout> components = new ArrayList<>();
protected boolean isTTS = false;
protected String nonce;
protected EnumSet<Message.MentionType> allowedMentions = null;
protected Set<String> mentionedUsers = new HashSet<>();
protected Set<String> mentionedRoles = new HashSet<>();
public MessageBuilder() {}
public MessageBuilder(@Nullable CharSequence content)
{
if (content != null)
builder.append(content);
}
public MessageBuilder(@Nullable Message message)
{
if (message != null)
{
isTTS = message.isTTS();
builder.append(message.getContentRaw());
List<MessageEmbed> embeds = message.getEmbeds();
if (embeds != null)
embeds.stream().filter(it -> it.getType() == EmbedType.RICH).forEach(this.embeds::add);
components.addAll(message.getActionRows());
if (message instanceof DataMessage)
{
DataMessage data = (DataMessage) message;
if (data.getAllowedMentions() != null)
this.allowedMentions = Helpers.copyEnumSet(Message.MentionType.class, data.getAllowedMentions());
Collections.addAll(this.mentionedUsers, data.getMentionedUsersWhitelist());
Collections.addAll(this.mentionedRoles, data.getMentionedRolesWhitelist());
}
}
}
public MessageBuilder(@Nullable MessageBuilder builder)
{
if (builder != null)
{
this.isTTS = builder.isTTS;
this.builder.append(builder.builder);
this.nonce = builder.nonce;
this.embeds.addAll(builder.embeds);
this.components.addAll(builder.components);
if (builder.allowedMentions != null)
this.allowedMentions = Helpers.copyEnumSet(Message.MentionType.class, builder.allowedMentions);
this.mentionedRoles.addAll(builder.mentionedRoles);
this.mentionedUsers.addAll(builder.mentionedUsers);
}
}
public MessageBuilder(@Nullable EmbedBuilder builder)
{
if (builder != null)
this.embeds.add(builder.build());
}
public MessageBuilder(@Nullable MessageEmbed embed)
{
if (embed != null)
this.embeds.add(embed);
}
/**
* Makes the created Message a TTS message.
* <br>TTS stands for Text-To-Speech. When a TTS method is received by the Discord client,
* it is vocalized so long as the user has not disabled TTS.
*
* @param tts
* whether the created Message should be a tts message
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder setTTS(boolean tts)
{
this.isTTS = tts;
return this;
}
/**
* Adds a {@link net.dv8tion.jda.api.entities.MessageEmbed} to the Message. Embeds can be built using
* the {@link net.dv8tion.jda.api.EmbedBuilder} and offer specialized formatting.
*
* @param embed
* the embed to add, or null to remove
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @deprecated Use {@link #setEmbeds(MessageEmbed...)} instead
*/
@Nonnull
@Deprecated
@ForRemoval(deadline = "5.0.0")
@ReplaceWith("setEmbeds(embed)")
@DeprecatedSince("4.4.0")
public MessageBuilder setEmbed(@Nullable MessageEmbed embed)
{
return embed == null ? setEmbeds() : setEmbeds(embed);
}
/**
* Adds up to 10 {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbeds} to the Message. Embeds can be built using
* the {@link net.dv8tion.jda.api.EmbedBuilder} and offer specialized formatting.
*
* @param embeds
* the embeds to add, or empty array to remove
*
* @throws java.lang.IllegalArgumentException
* If any of the provided MessageEmbeds is null or not sendable according to {@link net.dv8tion.jda.api.entities.MessageEmbed#isSendable() MessageEmbed.isSendable()}!
* The sum of all {@link MessageEmbed#getLength()} must not be greater than {@link MessageEmbed#EMBED_MAX_LENGTH_BOT}!
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder setEmbeds(@Nonnull MessageEmbed... embeds)
{
Checks.noneNull(embeds, "MessageEmbeds");
return setEmbeds(Arrays.asList(embeds));
}
/**
* Adds up to 10 {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbeds} to the Message. Embeds can be built using
* the {@link net.dv8tion.jda.api.EmbedBuilder} and offer specialized formatting.
*
* @param embeds
* the embeds to add, or empty list to remove
*
* @throws java.lang.IllegalArgumentException
* If any of the provided MessageEmbeds is null or not sendable according to {@link net.dv8tion.jda.api.entities.MessageEmbed#isSendable() MessageEmbed.isSendable()}!
* The sum of all {@link MessageEmbed#getLength()} must not be greater than {@link MessageEmbed#EMBED_MAX_LENGTH_BOT}!
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder setEmbeds(@Nonnull Collection<? extends MessageEmbed> embeds)
{
Checks.noneNull(embeds, "MessageEmbeds");
embeds.forEach(embed ->
Checks.check(embed.isSendable(),
"Provided Message contains an empty embed or an embed with a length greater than %d characters, which is the max for bot accounts!",
MessageEmbed.EMBED_MAX_LENGTH_BOT)
);
Checks.check(embeds.size() <= 10, "Cannot have more than 10 embeds in a message!");
Checks.check(embeds.stream().mapToInt(MessageEmbed::getLength).sum() <= MessageEmbed.EMBED_MAX_LENGTH_BOT, "The sum of all MessageEmbeds may not exceed %d!", MessageEmbed.EMBED_MAX_LENGTH_BOT);
this.embeds.clear();
this.embeds.addAll(embeds);
return this;
}
/**
* Set the action rows for the message.
*
* @param rows
* The new action rows, or null to reset the components
*
* @throws IllegalArgumentException
* If null is provided in the collection or more than 5 actions rows are provided
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder setActionRows(@Nullable Collection<? extends ActionRow> rows)
{
if (rows == null)
{
this.components.clear();
return this;
}
Checks.noneNull(rows, "ActionRows");
Checks.check(rows.size() <= 5, "Can only have 5 action rows per message!");
this.components.clear();
this.components.addAll(rows);
return this;
}
/**
* Set the action rows for the message.
*
* @param rows
* The new action rows, or null to reset the components
*
* @throws IllegalArgumentException
* If null is provided in the array or more than 5 actions rows are provided
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder setActionRows(@Nullable ActionRow... rows)
{
if (rows == null)
{
this.components.clear();
return this;
}
return setActionRows(Arrays.asList(rows));
}
/**
* Sets the <a href="https://en.wikipedia.org/wiki/Cryptographic_nonce" target="_blank">nonce</a>
* of the built message(s). It is recommended to have only 100% unique strings to validate messages via this nonce.
* <br>The nonce will be available from the resulting message via {@link net.dv8tion.jda.api.entities.Message#getNonce() Message.getNonce()}
* in message received by events and RestAction responses.
* <br>When {@code null} is provided no nonce will be used.
*
* @param nonce
* Validation nonce string
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.api.entities.Message#getNonce()
* @see <a href="https://en.wikipedia.org/wiki/Cryptographic_nonce" target="_blank">Cryptographic Nonce - Wikipedia</a>
*/
@Nonnull
public MessageBuilder setNonce(@Nullable String nonce)
{
this.nonce = nonce;
return this;
}
/**
* Sets the content of the resulting Message
* <br>This will replace already added content.
*
* @param content
* The content to use, or {@code null} to reset the content
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.api.entities.Message#getContentRaw()
*/
@Nonnull
public MessageBuilder setContent(@Nullable String content)
{
if (content == null)
{
builder.setLength(0);
}
else
{
final int newLength = Math.max(builder.length(), content.length());
builder.replace(0, newLength, content);
}
return this;
}
@Nonnull
@Override
public MessageBuilder append(@Nullable CharSequence text)
{
builder.append(text);
return this;
}
@Nonnull
@Override
public MessageBuilder append(@Nullable CharSequence text, int start, int end)
{
builder.append(text, start, end);
return this;
}
@Nonnull
@Override
public MessageBuilder append(char c)
{
builder.append(c);
return this;
}
/**
* Appends the string representation of an object to the Message.
* <br>This is the same as {@link #append(CharSequence) append(String.valueOf(object))}
*
* @param object
* the object to append
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder append(@Nullable Object object)
{
return append(String.valueOf(object));
}
/**
* Appends a mention to the Message.
* <br>Typical usage would be providing an {@link net.dv8tion.jda.api.entities.IMentionable IMentionable} like
* {@link net.dv8tion.jda.api.entities.User User} or {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}.
*
* <p>This will not add a rule to mention a {@link User} or {@link Role}. You have to use {@link #mention(IMentionable...)}
* in addition to this method.
*
* @param mention
* the mention to append
*
* @throws java.lang.IllegalArgumentException
* If provided with null
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder append(@Nonnull IMentionable mention)
{
Checks.notNull(mention, "Mentionable");
builder.append(mention.getAsMention());
return this;
}
/**
* Appends a String using the specified chat {@link net.dv8tion.jda.api.MessageBuilder.Formatting Formatting(s)}.
*
* @param text
* the text to append.
* @param format
* the format(s) to apply to the text.
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder append(@Nullable CharSequence text, @Nonnull Formatting... format)
{
boolean blockPresent = false;
for (Formatting formatting : format)
{
if (formatting == Formatting.BLOCK)
{
blockPresent = true;
continue;
}
builder.append(formatting.getTag());
}
if (blockPresent)
builder.append(Formatting.BLOCK.getTag());
builder.append(text);
if (blockPresent)
builder.append(Formatting.BLOCK.getTag());
for (int i = format.length - 1; i >= 0; i--)
{
if (format[i] == Formatting.BLOCK) continue;
builder.append(format[i].getTag());
}
return this;
}
/**
* This method is an extended form of {@link String#format(String, Object...)}. It allows for all of
* the token replacement functionality that String.format(String, Object...) supports.
* <br>A lot of JDA entities implement {@link java.util.Formattable Formattable} and will provide
* specific format outputs for their specific type.
* <ul>
* <li>{@link net.dv8tion.jda.api.entities.IMentionable IMentionable}
* <br>These will output their {@link net.dv8tion.jda.api.entities.IMentionable#getAsMention() getAsMention} by default,
* some implementations have alternatives such as {@link net.dv8tion.jda.api.entities.User User} and {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}.</li>
* <li>{@link net.dv8tion.jda.api.entities.MessageChannel MessageChannel}
* <br>All message channels format to {@code "#" + getName()} by default, TextChannel has special handling
* and uses the getAsMention output by default and the MessageChannel output as alternative ({@code #} flag).</li>
* <li>{@link net.dv8tion.jda.api.entities.Message Message}
* <br>Messages by default output their {@link net.dv8tion.jda.api.entities.Message#getContentDisplay() getContentDisplay()} value and
* as alternative use the {@link net.dv8tion.jda.api.entities.Message#getContentRaw() getContentRaw()} value</li>
* </ul>
*
* <p>Example:
* <br>If you placed the following code in an method handling a
* {@link net.dv8tion.jda.api.events.message.MessageReceivedEvent MessageReceivedEvent}
* <br><pre>{@code
* User user = event.getAuthor();
* MessageBuilder builder = new MessageBuilder();
* builder.appendFormat("%#s is really cool!", user);
* builder.build();
* }</pre>
*
* It would build a message that mentions the author and says that he is really cool!. If the user's
* name was "Minn" and his discriminator "6688", it would say:
* <br><pre> "Minn#6688 is really cool!"</pre>
* <br>Note that this uses the {@code #} flag to utilize the alternative format for {@link net.dv8tion.jda.api.entities.User User}.
* <br>By default it would fallback to {@link net.dv8tion.jda.api.entities.IMentionable#getAsMention()}
*
* @param format
* a format string.
* @param args
* an array objects that will be used to replace the tokens, they must be
* provided in the order that the tokens appear in the provided format string.
*
* @throws java.lang.IllegalArgumentException
* If the provided format string is {@code null} or empty
* @throws java.util.IllegalFormatException
* If a format string contains an illegal syntax,
* a format specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other illegal conditions.
* For specification of all possible formatting errors,
* see the <a href="../util/Formatter.html#detail">Details</a>
* section of the formatter class specification.
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder appendFormat(@Nonnull String format, @Nonnull Object... args)
{
Checks.notEmpty(format, "Format String");
this.append(String.format(format, args));
return this;
}
/**
* Appends a code-line to the Message.
* Code Lines are similar to code-blocks, however they are displayed in-line and do not support language specific highlighting.
*
* @param text
* the code to append
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder appendCodeLine(@Nullable CharSequence text)
{
this.append(text, Formatting.BLOCK);
return this;
}
/**
* Appends a code-block to the Message.
* <br>Discord uses <a href="https://highlightjs.org/">Highlight.js</a> for its language highlighting support. You can find out what
* specific languages are supported <a href="https://github.com/isagalaev/highlight.js/tree/master/src/languages">here</a>.
*
* @param text
* the code to append
* @param language
* the language of the code. If unknown use an empty string
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder appendCodeBlock(@Nullable CharSequence text, @Nullable CharSequence language)
{
builder.append("```").append(language).append('\n').append(text).append("\n```");
return this;
}
/**
* Returns the current length of the content that will be built into a {@link net.dv8tion.jda.api.entities.Message Message}
* when {@link #build()} is called.
* <br>If this value is {@code 0} (and there is no embed) or greater than {@code 2000} when {@link #build()} is called, an exception
* will be raised as you cannot send an empty message to Discord and Discord has a hard limit of 2000 characters per message.
*
* <p><b>Hint:</b> You can use {@link #build(int, int)} or
* {@link #buildAll(net.dv8tion.jda.api.MessageBuilder.SplitPolicy...) buildAll(SplitPolicy...)} as possible ways to
* deal with the 2000 character cap.
*
* @return the current length of the content that will be built into a Message.
*/
public int length()
{
return builder.length();
}
/**
* Checks if the message contains any contend. This includes text as well as embeds.
*
* @return whether the message contains content
*/
public boolean isEmpty() {
return builder.length() == 0 && embeds.isEmpty();
}
/**
* Replaces each substring that matches the target string with the specified replacement string.
* The replacement proceeds from the beginning of the string to the end, for example, replacing
* "aa" with "b" in the message "aaa" will result in "ba" rather than "ab".
*
* @param target
* the sequence of char values to be replaced
* @param replacement
* the replacement sequence of char values
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder replace(@Nonnull String target, @Nonnull String replacement)
{
int index = builder.indexOf(target);
while (index != -1)
{
builder.replace(index, index + target.length(), replacement);
index = builder.indexOf(target, index + replacement.length());
}
return this;
}
/**
* Replaces the first substring that matches the target string with the specified replacement string.
*
* @param target
* the sequence of char values to be replaced
* @param replacement
* the replacement sequence of char values
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder replaceFirst(@Nonnull String target, @Nonnull String replacement)
{
int index = builder.indexOf(target);
if (index != -1)
{
builder.replace(index, index + target.length(), replacement);
}
return this;
}
/**
* Replaces the last substring that matches the target string with the specified replacement string.
*
* @param target
* the sequence of char values to be replaced
* @param replacement
* the replacement sequence of char values
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder replaceLast(@Nonnull String target, @Nonnull String replacement)
{
int index = builder.lastIndexOf(target);
if (index != -1)
{
builder.replace(index, index + target.length(), replacement);
}
return this;
}
/**
* Removes the whitelist of mentioned users.
* <br>If {@link #setAllowedMentions(Collection)} does not contain {@link net.dv8tion.jda.api.entities.Message.MentionType#USER MentionType.USER}
* then no user will be mentioned.
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder clearMentionedUsers()
{
mentionedUsers.clear();
return this;
}
/**
* Removes the whitelist of mentioned roles.
* <br>If {@link #setAllowedMentions(Collection)} does not contain {@link net.dv8tion.jda.api.entities.Message.MentionType#ROLE MentionType.ROLE}
* then no role will be mentioned.
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder clearMentionedRoles()
{
mentionedRoles.clear();
return this;
}
/**
* Combination of {@link #clearMentionedRoles()} and {@link #clearMentionedUsers()}.
*
* <p>This will not affect {@link #setAllowedMentions(Collection)}. You can reset those to default
* by using {@code setAllowedMentions(null)}.
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder clearMentions()
{
return clearMentionedUsers().clearMentionedRoles();
}
/**
* Sets which {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes} should be parsed from
* the input. This will use {@link MessageAction#getDefaultMentions()} by default, or if {@code null} is provided.
*
* @param mentionTypes
* Collection of allowed mention types, or null to use {@link MessageAction#getDefaultMentions()}.
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder setAllowedMentions(@Nullable Collection<Message.MentionType> mentionTypes)
{
this.allowedMentions = mentionTypes == null
? MessageAction.getDefaultMentions()
: Helpers.copyEnumSet(Message.MentionType.class, mentionTypes);
return this;
}
/**
* Adds the provided {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes} to the whitelist.
*
* @param types
* The mention types to allow
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder allowMentions(@Nonnull Message.MentionType... types)
{
Checks.noneNull(types, "MentionTypes");
if (types.length > 0)
{
if (allowedMentions == null)
allowedMentions = MessageAction.getDefaultMentions();
Collections.addAll(allowedMentions, types);
}
return this;
}
/**
* Removes the provided {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes} from the whitelist.
*
* @param types
* The mention types to deny
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder denyMentions(@Nonnull Message.MentionType... types)
{
Checks.noneNull(types, "MentionTypes");
if (types.length > 0)
{
if (allowedMentions == null)
allowedMentions = MessageAction.getDefaultMentions();
for (Message.MentionType type : types)
allowedMentions.remove(type);
}
return this;
}
/**
* Adds the provided {@link IMentionable IMentionable} instance to the whitelist of mentions.
* <br>This will only affect instances of {@link User}, {@link Member}, and {@link Role}.
* <br>The content will not be affected by this. To append a mention use {@link #append(IMentionable)}.
*
* <p>See {@link MessageAction#mention(IMentionable...)} for more details.
*
* @param mentions
* Whitelist of mentions to apply
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @see #clearMentions()
* @see MessageAction#mention(IMentionable...)
*/
@Nonnull
public MessageBuilder mention(@Nonnull IMentionable... mentions)
{
Checks.noneNull(mentions, "Mentions");
for (IMentionable mention : mentions)
{
if (mention instanceof User || mention instanceof Member)
mentionedUsers.add(mention.getId());
else if (mention instanceof Role)
mentionedRoles.add(mention.getId());
}
return this;
}
/**
* Adds the provided {@link IMentionable IMentionable} instance to the whitelist of mentions.
* <br>This will only affect instances of {@link User}, {@link Member}, and {@link Role}.
* <br>The content will not be affected by this. To append a mention use {@link #append(IMentionable)}.
*
* <p>See {@link MessageAction#mention(IMentionable...)} for more details.
*
* @param mentions
* Whitelist of mentions to apply
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @see #clearMentions()
* @see MessageAction#mention(IMentionable...)
*/
@Nonnull
public MessageBuilder mention(@Nonnull Collection<? extends IMentionable> mentions)
{
Checks.noneNull(mentions, "Mentions");
return mention(mentions.toArray(new IMentionable[0]));
}
/**
* Adds the provided {@link User Users} to the whitelist of mentions.
* <br>The provided list must only contain IDs of users.
* <br>The content will not be affected by this.
* To append a mention use {@code append("<@").append(id).append(">")}.
*
* <p>See {@link MessageAction#mentionUsers(String...)} for more details.
*
* @param users
* Whitelist of user IDs to apply
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @see #clearMentionedUsers()
* @see MessageAction#mentionUsers(String...)
*/
@Nonnull
public MessageBuilder mentionUsers(@Nonnull String... users)
{
Checks.noneNull(users, "Users");
Collections.addAll(mentionedUsers, users);
return this;
}
/**
* Adds the provided {@link Role Roles} to the whitelist of mentions.
* <br>The provided list must only contain IDs of roles.
* <br>The content will not be affected by this.
* To append a mention use {@code append("<@&").append(id).append(">")}.
*
* <p>See {@link MessageAction#mentionRoles(String...)} for more details.
*
* @param roles
* Whitelist of role IDs to apply
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @see #clearMentionedRoles()
* @see MessageAction#mentionRoles(String...)
*/
@Nonnull
public MessageBuilder mentionRoles(@Nonnull String... roles)
{
Checks.noneNull(roles, "Roles");
Collections.addAll(mentionedRoles, roles);
return this;
}
/**
* Adds the provided {@link User Users} to the whitelist of mentions.
* <br>The provided list must only contain IDs of users.
* <br>The content will not be affected by this.
* To append a mention use {@code append("<@").append(id).append(">")}.
*
* <p>See {@link MessageAction#mentionUsers(long...)} for more details.
*
* @param users
* Whitelist of user IDs to apply
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @see #clearMentionedUsers()
* @see MessageAction#mentionUsers(long...)
*/
@Nonnull
public MessageBuilder mentionUsers(@Nonnull long... users)
{
Checks.notNull(users, "Users");
return mentionUsers(toStringArray(users));
}
/**
* Adds the provided {@link Role Roles} to the whitelist of mentions.
* <br>The provided list must only contain IDs of roles.
* <br>The content will not be affected by this.
* To append a mention use {@code append("<@&").append(id).append(">")}.
*
* <p>See {@link MessageAction#mentionRoles(long...)} for more details.
*
* @param roles
* Whitelist of role IDs to apply
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @see #clearMentionedRoles()
* @see MessageAction#mentionRoles(long...)
*/
@Nonnull
public MessageBuilder mentionRoles(@Nonnull long... roles)
{
Checks.notNull(roles, "Roles");
return mentionRoles(toStringArray(roles));
}
/**
* Removes all mentions and replaces them with the closest looking textual representation.
*
* <p>Use this over {@link #stripMentions(Guild)} if {@link net.dv8tion.jda.api.entities.User User} mentions
* should be replaced with their {@link net.dv8tion.jda.api.entities.User#getName()} instead of their Nicknames.
*
* @param jda
* The JDA instance used to resolve the mentions.
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @deprecated This is not a reliable way to remove mentions from the content,
* you should use {@link #setAllowedMentions(Collection)} instead.
*/
@Nonnull
@Deprecated
@ForRemoval(deadline = "4.4.0")
@ReplaceWith("setAllowedMentions(Collections.emptyList())")
@DeprecatedSince("4.2.0")
public MessageBuilder stripMentions(@Nonnull JDA jda)
{
// Note: Users can rename to "everyone" or "here", so those
// should be stripped after the USER mention is stripped.
return this.stripMentions(jda, null, Message.MentionType.values());
}
/**
* Removes all mentions and replaces them with the closest looking textual representation.
*
* <p>Use this over {@link #stripMentions(JDA)} if {@link net.dv8tion.jda.api.entities.User User} mentions should
* be replaced with their nicknames in a specific guild based.
* <br>Uses {@link net.dv8tion.jda.api.entities.Member#getEffectiveName()}
*
* @param guild
* the guild for {@link net.dv8tion.jda.api.entities.User User} mentions
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @deprecated This is not a reliable way to remove mentions from the content,
* you should use {@link #setAllowedMentions(Collection)} instead.
*/
@Nonnull
@Deprecated
@ForRemoval(deadline = "4.4.0")
@ReplaceWith("setAllowedMentions(Collections.emptyList())")
@DeprecatedSince("4.2.0")
public MessageBuilder stripMentions(@Nonnull Guild guild)
{
// Note: Users can rename to "everyone" or "here", so those
// should be stripped after the USER mention is stripped.
return this.stripMentions(guild.getJDA(), guild, Message.MentionType.values());
}
/**
* Removes all mentions of the specified types and replaces them with the closest looking textual representation.
*
* <p>Use this over {@link #stripMentions(JDA, Message.MentionType...)} if {@link net.dv8tion.jda.api.entities.User User} mentions should
* be replaced with their nicknames in a specific guild based.
* <br>Uses {@link net.dv8tion.jda.api.entities.Member#getEffectiveName()}
*
* @param guild
* the guild for {@link net.dv8tion.jda.api.entities.User User} mentions
* @param types
* the {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes} that should be stripped
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @deprecated This is not a reliable way to remove mentions from the content,
* you should use {@link #setAllowedMentions(Collection)} instead.
*/
@Nonnull
@Deprecated
@ForRemoval(deadline = "4.4.0")
@ReplaceWith("denyMentions(types)")
@DeprecatedSince("4.2.0")
public MessageBuilder stripMentions(@Nonnull Guild guild, @Nonnull Message.MentionType... types)
{
return this.stripMentions(guild.getJDA(), guild, types);
}
/**
* Removes all mentions of the specified types and replaces them with the closest looking textual representation.
*
* <p>Use this over {@link #stripMentions(Guild, Message.MentionType...)} if {@link net.dv8tion.jda.api.entities.User User}
* mentions should be replaced with their {@link net.dv8tion.jda.api.entities.User#getName()}.
*
* @param jda
* The JDA instance used to resolve the mentions.
* @param types
* the {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes} that should be stripped
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @deprecated This is not a reliable way to remove mentions from the content,
* you should use {@link #setAllowedMentions(Collection)} instead.
*/
@Nonnull
@Deprecated
@ForRemoval(deadline = "4.4.0")
@ReplaceWith("denyMentions(types)")
@DeprecatedSince("4.2.0")
public MessageBuilder stripMentions(@Nonnull JDA jda, @Nonnull Message.MentionType... types)
{
return this.stripMentions(jda, null, types);
}
@Nonnull
private MessageBuilder stripMentions(JDA jda, Guild guild, Message.MentionType... types)
{
if (types == null)
return this;
String string = null;
for (Message.MentionType mention : types)
{
if (mention != null)
{
switch (mention)
{
case EVERYONE:
replace("@everyone", "@\u0435veryone");
break;
case HERE:
replace("@here", "@h\u0435re");
break;
case CHANNEL:
{
if (string == null)
{
string = builder.toString();
}
Matcher matcher = Message.MentionType.CHANNEL.getPattern().matcher(string);
while (matcher.find())
{
TextChannel channel = jda.getTextChannelById(matcher.group(1));
if (channel != null)
{
replace(matcher.group(), "#" + channel.getName());
}
}
break;
}