public class MessageBuilder
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
MessageBuilder.Formatting
Holds the available formatting used in
MessageBuilder#appendString(String, net.dv8tion.jda.core.MessageBuilder.Formatting...) |
static class |
MessageBuilder.MentionType
Holds the available mention types used in MessageBuilder#stripMentions(JDA, MentionType...) and {@link MessageBuilder#stripMentions(Guild, MentionType...)}
|
static interface |
MessageBuilder.SplitPolicy |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
EVERYONE_KEY |
static IMentionable |
EVERYONE_MENTION
A constant for @everyone mentions.
|
static java.lang.String |
HERE_KEY |
static IMentionable |
HERE_MENTION
A constant for @here mentions.
|
static java.lang.String |
ROLE_KEY |
static java.lang.String |
TEXTCHANNEL_KEY |
static java.lang.String |
USER_KEY |
Constructor and Description |
---|
MessageBuilder() |
Modifier and Type | Method and Description |
---|---|
MessageBuilder |
append(java.lang.CharSequence text)
Appends a string to the Message
|
MessageBuilder |
append(java.lang.CharSequence text,
MessageBuilder.Formatting... format)
Appends a formatted string to the Message
|
MessageBuilder |
append(IMentionable mention)
Appends a mention to the Message
|
MessageBuilder |
append(java.lang.Object object)
Appends the string repesentation of an object to the Message.
|
MessageBuilder |
appendCodeBlock(java.lang.CharSequence text,
java.lang.CharSequence language)
Appends a code-block to the Message
|
MessageBuilder |
appendEveryoneMention()
Deprecated.
use append(IMentionable) with
EVERYONE_MENTION instead |
MessageBuilder |
appendFormat(java.lang.String format,
java.lang.Object... args)
This method is an extended form of
String.format(String, Object...) . |
MessageBuilder |
appendHereMention()
Deprecated.
use append(IMentionable) with
HERE_MENTION instead |
MessageBuilder |
appendMention(Role role)
Deprecated.
use
append(IMentionable) instead |
MessageBuilder |
appendMention(TextChannel channel)
Deprecated.
use
append(IMentionable) instead |
MessageBuilder |
appendMention(User user)
Deprecated.
use
append(IMentionable) instead |
MessageBuilder |
appendString(java.lang.CharSequence text)
Deprecated.
use
append(CharSequence) instead |
MessageBuilder |
appendString(java.lang.CharSequence text,
MessageBuilder.Formatting... format)
Deprecated.
use
append(CharSequence, Formatting...) instead |
Message |
build()
Creates a
Message object from this MessageBuilder |
java.util.Queue<Message> |
buildAll(MessageBuilder.SplitPolicy... policy)
Creates a
Queue of Message objects from this MessageBuilder. |
java.lang.StringBuilder |
getStringBuilder()
Returns the underlying
StringBuilder . |
int |
indexOf(java.lang.CharSequence target,
int fromIndex,
int endIndex)
Returns the index within this string of the first occurrence of the
specified substring between the specified indices.
|
boolean |
isEmpty()
Checks if the message contains any contend.
|
int |
lastIndexOf(java.lang.CharSequence target,
int fromIndex,
int endIndex)
Returns the index within this string of the last occurrence of the
specified substring between the specified indices.
|
int |
length()
|
MessageBuilder |
replaceAll(java.lang.String target,
java.lang.String replacement)
Replaces each substring that matches the target string with the specified replacement string.
|
MessageBuilder |
replaceFirst(java.lang.String target,
java.lang.String replacement)
Replaces the first substring that matches the target string with the specified replacement string.
|
MessageBuilder |
replaceLast(java.lang.String target,
java.lang.String replacement)
Replaces the last substring that matches the target string with the specified replacement string.
|
MessageBuilder |
setEmbed(MessageEmbed embed)
Adds an embed to the Message
|
MessageBuilder |
setTTS(boolean tts)
Makes the created Message a TTS message
|
MessageBuilder |
stripMentions(Guild guild)
Removes all mentions and replaces them with the closest looking textual representation.
|
MessageBuilder |
stripMentions(Guild guild,
MessageBuilder.MentionType... types)
Removes all mentions of the specified types and replaces them with the closest looking textual representation.
|
MessageBuilder |
stripMentions(JDA jda)
Removes all mentions and replaces them with the closest looking textual representation.
|
MessageBuilder |
stripMentions(JDA jda,
MessageBuilder.MentionType... types)
Removes all mentions of the specified types and replaces them with the closest looking textual representation.
|
public static final java.lang.String USER_KEY
public static final java.lang.String ROLE_KEY
public static final java.lang.String TEXTCHANNEL_KEY
public static final java.lang.String EVERYONE_KEY
public static final java.lang.String HERE_KEY
public static final IMentionable EVERYONE_MENTION
append(IMentionable)
public static final IMentionable HERE_MENTION
append(IMentionable)
public MessageBuilder setTTS(boolean tts)
tts
- whether the created Message should be a tts messagepublic MessageBuilder setEmbed(MessageEmbed embed)
embed
- the embed to add, or null to remove@Deprecated public MessageBuilder appendString(java.lang.CharSequence text)
append(CharSequence)
insteadtext
- the text to appendpublic MessageBuilder append(java.lang.CharSequence text)
text
- the text to appendpublic MessageBuilder append(java.lang.Object object)
This is the same as append(String.valueOf(object))
object
- the object to appendpublic MessageBuilder append(IMentionable mention)
mention
- the mention to appendpublic MessageBuilder append(java.lang.CharSequence text, MessageBuilder.Formatting... format)
text
- the text to appendformat
- the format(s) to apply to the text@Deprecated public MessageBuilder appendString(java.lang.CharSequence text, MessageBuilder.Formatting... format)
append(CharSequence, Formatting...)
insteadtext
- the text to appendformat
- the format(s) to apply to the textpublic MessageBuilder appendFormat(java.lang.String format, java.lang.Object... args)
String.format(String, Object...)
. It allows for all of
the token replacement functionality that String.format(String, Object...) supports, but it also supports
specialized token replacement specific to JDA objects.
Current tokens:
User
.
Same as appendMention(net.dv8tion.jda.core.entities.User)
Role
.
Same as appendMention(net.dv8tion.jda.core.entities.Role)
TextChannel
.
Same as appendMention(net.dv8tion.jda.core.entities.TextChannel)
appendEveryoneMention()
appendHereMention()
Example:
If you placed the following code in an method handling a
MessageReceivedEvent
User user = event.getAuthor();
MessageBuilder builder = new MessageBuilder();
builder.appendFormat("%U% is really cool!", user);
builder.build();
It would build a message that mentions the author and says that he is really cool!. If the user's
name was "Bob", it would say:"Bob is really cool!"
format
- a format string.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.public MessageBuilder appendCodeBlock(java.lang.CharSequence text, java.lang.CharSequence language)
text
- the code to appendlanguage
- the language of the code. If unknown use an empty string@Deprecated public MessageBuilder appendMention(User user)
append(IMentionable)
insteaduser
- the user to mention@Deprecated public MessageBuilder appendEveryoneMention()
EVERYONE_MENTION
instead@Deprecated public MessageBuilder appendHereMention()
HERE_MENTION
instead@Deprecated public MessageBuilder appendMention(TextChannel channel)
append(IMentionable)
insteadchannel
- the TextChannel
to mention@Deprecated public MessageBuilder appendMention(Role role)
append(IMentionable)
insteadrole
- the Role
to mentionpublic int length()
Message
when build()
is called.0
or greater than 2000
when build()
is called, an exception
will be raised.public boolean isEmpty()
public Message build()
Message
object from this MessageBuilderMessage
java.lang.UnsupportedOperationException
- public MessageBuilder replaceAll(java.lang.String target, java.lang.String replacement)
target
- the sequence of char values to be replacedreplacement
- the replacement sequence of char valuespublic MessageBuilder replaceFirst(java.lang.String target, java.lang.String replacement)
target
- the sequence of char values to be replacedreplacement
- the replacement sequence of char valuespublic MessageBuilder replaceLast(java.lang.String target, java.lang.String replacement)
target
- the sequence of char values to be replacedreplacement
- the replacement sequence of char valuespublic MessageBuilder stripMentions(JDA jda)
Use this over stripMentions(Guild)
if User
mentions should be replaced with their user names
jda
- the JDA instancepublic MessageBuilder stripMentions(Guild guild)
Use this over stripMentions(JDA)
if User
mentions should be replaced with their nicknames in a specific guild
guild
- the guild for User
mentionspublic MessageBuilder stripMentions(Guild guild, MessageBuilder.MentionType... types)
Use this over stripMentions(JDA, MentionType...)
if User
mentions should be replaced with their nicknames in a specific guild
guild
- the guild for User
mentionspublic MessageBuilder stripMentions(JDA jda, MessageBuilder.MentionType... types)
Use this over stripMentions(Guild, MentionType...)
if User
mentions should be replaced with their user names
jda
- the JDA
instance, only needed for User
, Channel
and Guild
mentionstypes
- the MentionTypes
that should be strippedpublic java.lang.StringBuilder getStringBuilder()
StringBuilder
.StringBuilder
used by this MessageBuilder
public int indexOf(java.lang.CharSequence target, int fromIndex, int endIndex)
-1
is returned.target
- the substring to search for.fromIndex
- the index from which to start the search.endIndex
- the index at which to end the search.-1
if there is no such occurrence.public int lastIndexOf(java.lang.CharSequence target, int fromIndex, int endIndex)
-1
is returned.target
- the substring to search for.fromIndex
- the index from which to start the search.endIndex
- the index at which to end the search.-1
if there is no such occurrence.public java.util.Queue<Message> buildAll(MessageBuilder.SplitPolicy... policy)
Queue
of Message
objects from this MessageBuilder.
This method splits the content if it exceeds 2000 chars. The splitting behaviour can be customized using SplitPolicies
.
The method will try the policies in the order they are passed to it.
If no SplitPolicy is provided the message will be splitted after exactly 2000 chars.
This is not MarkDown safe. An easy workaround is to include Zero Witdh Spaces as predetermined breaking points to the message and only split on them.
Messages