Class MessageBuilder
- java.lang.Object
-
- net.dv8tion.jda.api.MessageBuilder
-
- All Implemented Interfaces:
java.lang.Appendable
public class MessageBuilder extends java.lang.Object implements java.lang.Appendable
Builder system used to buildMessages
.
Internally the builder uses aStringBuilder
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 andString.format(String, Object...)
.- Since:
- 1.0
- Author:
- Michael Ritter, Aljoscha Grebe
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MessageBuilder.Formatting
Holds the available formatting used inappend(java.lang.CharSequence, net.dv8tion.jda.api.MessageBuilder.Formatting...)
static interface
MessageBuilder.SplitPolicy
Interface to allow custom implementation of Splitting rules forMessageBuilder.buildAll(SplitPolicy...)
.
-
Constructor Summary
Constructors Constructor Description MessageBuilder()
MessageBuilder(java.lang.CharSequence content)
MessageBuilder(EmbedBuilder builder)
MessageBuilder(Message message)
MessageBuilder(MessageEmbed embed)
MessageBuilder(MessageBuilder builder)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description MessageBuilder
allowMentions(Message.MentionType... types)
Adds the providedMentionTypes
to the whitelist.MessageBuilder
append(char c)
MessageBuilder
append(java.lang.CharSequence text)
MessageBuilder
append(java.lang.CharSequence text, int start, int end)
MessageBuilder
append(java.lang.CharSequence text, MessageBuilder.Formatting... format)
Appends a String using the specified chatFormatting(s)
.MessageBuilder
append(java.lang.Object object)
Appends the string representation of an object to the Message.MessageBuilder
append(IMentionable mention)
Appends a mention to the Message.MessageBuilder
appendCodeBlock(java.lang.CharSequence text, java.lang.CharSequence language)
Appends a code-block to the Message.MessageBuilder
appendCodeLine(java.lang.CharSequence text)
Appends a code-line to the Message.MessageBuilder
appendFormat(java.lang.String format, java.lang.Object... args)
This method is an extended form ofString.format(String, Object...)
.Message
build()
Creates aMessage
object from this MessageBuilderjava.util.Queue<Message>
buildAll(MessageBuilder.SplitPolicy... policy)
Creates aQueue
ofMessage
objects from this MessageBuilder.MessageBuilder
clear()
Clears the current builder.MessageBuilder
clearMentionedRoles()
Removes the whitelist of mentioned roles.MessageBuilder
clearMentionedUsers()
Removes the whitelist of mentioned users.MessageBuilder
clearMentions()
Combination ofclearMentionedRoles()
andclearMentionedUsers()
.MessageBuilder
denyMentions(Message.MentionType... types)
Removes the providedMentionTypes
from the whitelist.java.lang.StringBuilder
getStringBuilder()
Returns the underlyingStringBuilder
.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
mention(java.util.Collection<? extends IMentionable> mentions)
Adds the providedIMentionable
instance to the whitelist of mentions.MessageBuilder
mention(IMentionable... mentions)
Adds the providedIMentionable
instance to the whitelist of mentions.MessageBuilder
mentionRoles(long... roles)
Adds the providedRoles
to the whitelist of mentions.MessageBuilder
mentionRoles(java.lang.String... roles)
Adds the providedRoles
to the whitelist of mentions.MessageBuilder
mentionUsers(long... users)
Adds the providedUsers
to the whitelist of mentions.MessageBuilder
mentionUsers(java.lang.String... users)
Adds the providedUsers
to the whitelist of mentions.MessageBuilder
replace(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
setActionRows(java.util.Collection<? extends ActionRow> rows)
Set the action rows for the message.MessageBuilder
setActionRows(ActionRow... rows)
Set the action rows for the message.MessageBuilder
setAllowedMentions(java.util.Collection<Message.MentionType> mentionTypes)
Sets whichMentionTypes
should be parsed from the input.MessageBuilder
setContent(java.lang.String content)
Sets the content of the resulting Message
This will replace already added content.MessageBuilder
setEmbed(MessageEmbed embed)
Deprecated.UsesetEmbeds(MessageEmbed...)
insteadMessageBuilder
setEmbeds(java.util.Collection<? extends MessageEmbed> embeds)
Adds up to 10MessageEmbeds
to the Message.MessageBuilder
setEmbeds(MessageEmbed... embeds)
Adds up to 10MessageEmbeds
to the Message.MessageBuilder
setNonce(java.lang.String nonce)
Sets the nonce of the built message(s).MessageBuilder
setTTS(boolean tts)
Makes the created Message a TTS message.MessageBuilder
stripMentions(Guild guild)
Deprecated.This is not a reliable way to remove mentions from the content, you should usesetAllowedMentions(Collection)
instead.MessageBuilder
stripMentions(Guild guild, Message.MentionType... types)
Deprecated.This is not a reliable way to remove mentions from the content, you should usesetAllowedMentions(Collection)
instead.MessageBuilder
stripMentions(JDA jda)
Deprecated.This is not a reliable way to remove mentions from the content, you should usesetAllowedMentions(Collection)
instead.MessageBuilder
stripMentions(JDA jda, Message.MentionType... types)
Deprecated.This is not a reliable way to remove mentions from the content, you should usesetAllowedMentions(Collection)
instead.
-
-
-
Constructor Detail
-
MessageBuilder
public MessageBuilder()
-
MessageBuilder
public MessageBuilder(@Nullable java.lang.CharSequence content)
-
MessageBuilder
public MessageBuilder(@Nullable Message message)
-
MessageBuilder
public MessageBuilder(@Nullable MessageBuilder builder)
-
MessageBuilder
public MessageBuilder(@Nullable EmbedBuilder builder)
-
MessageBuilder
public MessageBuilder(@Nullable MessageEmbed embed)
-
-
Method Detail
-
setTTS
@Nonnull public MessageBuilder setTTS(boolean tts)
Makes the created Message a TTS message.
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.- Parameters:
tts
- whether the created Message should be a tts message- Returns:
- The MessageBuilder instance. Useful for chaining.
-
setEmbed
@Nonnull @Deprecated @ForRemoval(deadline="5.0.0") @ReplaceWith("setEmbeds(embed)") @DeprecatedSince("4.4.0") public MessageBuilder setEmbed(@Nullable MessageEmbed embed)
Deprecated.UsesetEmbeds(MessageEmbed...)
insteadAdds aMessageEmbed
to the Message. Embeds can be built using theEmbedBuilder
and offer specialized formatting.- Parameters:
embed
- the embed to add, or null to remove- Returns:
- The MessageBuilder instance. Useful for chaining.
-
setEmbeds
@Nonnull public MessageBuilder setEmbeds(@Nonnull MessageEmbed... embeds)
Adds up to 10MessageEmbeds
to the Message. Embeds can be built using theEmbedBuilder
and offer specialized formatting.- Parameters:
embeds
- the embeds to add, or empty array to remove- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If any of the provided MessageEmbeds is null or not sendable according toMessageEmbed.isSendable()
! The sum of allMessageEmbed.getLength()
must not be greater thanMessageEmbed.EMBED_MAX_LENGTH_BOT
!
-
setEmbeds
@Nonnull public MessageBuilder setEmbeds(@Nonnull java.util.Collection<? extends MessageEmbed> embeds)
Adds up to 10MessageEmbeds
to the Message. Embeds can be built using theEmbedBuilder
and offer specialized formatting.- Parameters:
embeds
- the embeds to add, or empty list to remove- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If any of the provided MessageEmbeds is null or not sendable according toMessageEmbed.isSendable()
! The sum of allMessageEmbed.getLength()
must not be greater thanMessageEmbed.EMBED_MAX_LENGTH_BOT
!
-
setActionRows
@Nonnull public MessageBuilder setActionRows(@Nullable java.util.Collection<? extends ActionRow> rows)
Set the action rows for the message.- Parameters:
rows
- The new action rows, or null to reset the components- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If null is provided in the collection or more than 5 actions rows are provided
-
setActionRows
@Nonnull public MessageBuilder setActionRows(@Nullable ActionRow... rows)
Set the action rows for the message.- Parameters:
rows
- The new action rows, or null to reset the components- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If null is provided in the array or more than 5 actions rows are provided
-
setNonce
@Nonnull public MessageBuilder setNonce(@Nullable java.lang.String nonce)
Sets the nonce of the built message(s). It is recommended to have only 100% unique strings to validate messages via this nonce.
The nonce will be available from the resulting message viaMessage.getNonce()
in message received by events and RestAction responses.
Whennull
is provided no nonce will be used.- Parameters:
nonce
- Validation nonce string- Returns:
- The MessageBuilder instance. Useful for chaining.
- See Also:
Message.getNonce()
, Cryptographic Nonce - Wikipedia
-
setContent
@Nonnull public MessageBuilder setContent(@Nullable java.lang.String content)
Sets the content of the resulting Message
This will replace already added content.- Parameters:
content
- The content to use, ornull
to reset the content- Returns:
- The MessageBuilder instance. Useful for chaining.
- See Also:
Message.getContentRaw()
-
append
@Nonnull public MessageBuilder append(@Nullable java.lang.CharSequence text)
- Specified by:
append
in interfacejava.lang.Appendable
-
append
@Nonnull public MessageBuilder append(@Nullable java.lang.CharSequence text, int start, int end)
- Specified by:
append
in interfacejava.lang.Appendable
-
append
@Nonnull public MessageBuilder append(char c)
- Specified by:
append
in interfacejava.lang.Appendable
-
append
@Nonnull public MessageBuilder append(@Nullable java.lang.Object object)
Appends the string representation of an object to the Message.
This is the same asappend(String.valueOf(object))
- Parameters:
object
- the object to append- Returns:
- The MessageBuilder instance. Useful for chaining.
-
append
@Nonnull public MessageBuilder append(@Nonnull IMentionable mention)
Appends a mention to the Message.
Typical usage would be providing anIMentionable
likeUser
orTextChannel
.This will not add a rule to mention a
User
orRole
. You have to usemention(IMentionable...)
in addition to this method.- Parameters:
mention
- the mention to append- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If provided with null
-
append
@Nonnull public MessageBuilder append(@Nullable java.lang.CharSequence text, @Nonnull MessageBuilder.Formatting... format)
Appends a String using the specified chatFormatting(s)
.- Parameters:
text
- the text to append.format
- the format(s) to apply to the text.- Returns:
- The MessageBuilder instance. Useful for chaining.
-
appendFormat
@Nonnull public MessageBuilder appendFormat(@Nonnull java.lang.String format, @Nonnull java.lang.Object... args)
This method is an extended form ofString.format(String, Object...)
. It allows for all of the token replacement functionality that String.format(String, Object...) supports.
A lot of JDA entities implementFormattable
and will provide specific format outputs for their specific type.IMentionable
These will output theirgetAsMention
by default, some implementations have alternatives such asUser
andTextChannel
.MessageChannel
All message channels format to"#" + getName()
by default, TextChannel has special handling and uses the getAsMention output by default and the MessageChannel output as alternative (#
flag).Message
Messages by default output theirgetContentDisplay()
value and as alternative use thegetContentRaw()
value
Example:
If you placed the following code in an method handling aMessageReceivedEvent
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:User user = event.getAuthor(); MessageBuilder builder = new MessageBuilder(); builder.appendFormat("%#s is really cool!", user); builder.build();
"Minn#6688 is really cool!"
Note that this uses the#
flag to utilize the alternative format forUser
.
By default it would fallback toIMentionable.getAsMention()
- Parameters:
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.- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If the provided format string isnull
or emptyjava.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 Details section of the formatter class specification.
-
appendCodeLine
@Nonnull public MessageBuilder appendCodeLine(@Nullable java.lang.CharSequence text)
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.- Parameters:
text
- the code to append- Returns:
- The MessageBuilder instance. Useful for chaining.
-
appendCodeBlock
@Nonnull public MessageBuilder appendCodeBlock(@Nullable java.lang.CharSequence text, @Nullable java.lang.CharSequence language)
Appends a code-block to the Message.
Discord uses Highlight.js for its language highlighting support. You can find out what specific languages are supported here.- Parameters:
text
- the code to appendlanguage
- the language of the code. If unknown use an empty string- Returns:
- The MessageBuilder instance. Useful for chaining.
-
length
public int length()
Returns the current length of the content that will be built into aMessage
whenbuild()
is called.
If this value is0
(and there is no embed) or greater than2000
whenbuild()
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.Hint: You can use
build(int, int)
orbuildAll(SplitPolicy...)
as possible ways to deal with the 2000 character cap.- Returns:
- the current length of the content that will be built into a Message.
-
isEmpty
public boolean isEmpty()
Checks if the message contains any contend. This includes text as well as embeds.- Returns:
- whether the message contains content
-
replace
@Nonnull public MessageBuilder replace(@Nonnull java.lang.String target, @Nonnull java.lang.String replacement)
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".- Parameters:
target
- the sequence of char values to be replacedreplacement
- the replacement sequence of char values- Returns:
- The MessageBuilder instance. Useful for chaining.
-
replaceFirst
@Nonnull public MessageBuilder replaceFirst(@Nonnull java.lang.String target, @Nonnull java.lang.String replacement)
Replaces the first substring that matches the target string with the specified replacement string.- Parameters:
target
- the sequence of char values to be replacedreplacement
- the replacement sequence of char values- Returns:
- The MessageBuilder instance. Useful for chaining.
-
replaceLast
@Nonnull public MessageBuilder replaceLast(@Nonnull java.lang.String target, @Nonnull java.lang.String replacement)
Replaces the last substring that matches the target string with the specified replacement string.- Parameters:
target
- the sequence of char values to be replacedreplacement
- the replacement sequence of char values- Returns:
- The MessageBuilder instance. Useful for chaining.
-
clearMentionedUsers
@Nonnull public MessageBuilder clearMentionedUsers()
Removes the whitelist of mentioned users.
IfsetAllowedMentions(Collection)
does not containMentionType.USER
then no user will be mentioned.- Returns:
- The MessageBuilder instance. Useful for chaining.
-
clearMentionedRoles
@Nonnull public MessageBuilder clearMentionedRoles()
Removes the whitelist of mentioned roles.
IfsetAllowedMentions(Collection)
does not containMentionType.ROLE
then no role will be mentioned.- Returns:
- The MessageBuilder instance. Useful for chaining.
-
clearMentions
@Nonnull public MessageBuilder clearMentions()
Combination ofclearMentionedRoles()
andclearMentionedUsers()
.This will not affect
setAllowedMentions(Collection)
. You can reset those to default by usingsetAllowedMentions(null)
.- Returns:
- The MessageBuilder instance. Useful for chaining.
-
setAllowedMentions
@Nonnull public MessageBuilder setAllowedMentions(@Nullable java.util.Collection<Message.MentionType> mentionTypes)
Sets whichMentionTypes
should be parsed from the input. This will useMessageAction.getDefaultMentions()
by default, or ifnull
is provided.- Parameters:
mentionTypes
- Collection of allowed mention types, or null to useMessageAction.getDefaultMentions()
.- Returns:
- The MessageBuilder instance. Useful for chaining.
-
allowMentions
@Nonnull public MessageBuilder allowMentions(@Nonnull Message.MentionType... types)
Adds the providedMentionTypes
to the whitelist.- Parameters:
types
- The mention types to allow- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If null is provided
-
denyMentions
@Nonnull public MessageBuilder denyMentions(@Nonnull Message.MentionType... types)
Removes the providedMentionTypes
from the whitelist.- Parameters:
types
- The mention types to deny- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If null is provided
-
mention
@Nonnull public MessageBuilder mention(@Nonnull IMentionable... mentions)
Adds the providedIMentionable
instance to the whitelist of mentions.
This will only affect instances ofUser
,Member
, andRole
.
The content will not be affected by this. To append a mention useappend(IMentionable)
.See
AllowedMentions.mention(IMentionable...)
for more details.- Parameters:
mentions
- Whitelist of mentions to apply- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If null is provided- See Also:
clearMentions()
,AllowedMentions.mention(IMentionable...)
-
mention
@Nonnull public MessageBuilder mention(@Nonnull java.util.Collection<? extends IMentionable> mentions)
Adds the providedIMentionable
instance to the whitelist of mentions.
This will only affect instances ofUser
,Member
, andRole
.
The content will not be affected by this. To append a mention useappend(IMentionable)
.See
AllowedMentions.mention(IMentionable...)
for more details.- Parameters:
mentions
- Whitelist of mentions to apply- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If null is provided- See Also:
clearMentions()
,AllowedMentions.mention(IMentionable...)
-
mentionUsers
@Nonnull public MessageBuilder mentionUsers(@Nonnull java.lang.String... users)
Adds the providedUsers
to the whitelist of mentions.
The provided list must only contain IDs of users.
The content will not be affected by this. To append a mention useappend("<@").append(id).append(">")
.See
AllowedMentions.mentionUsers(String...)
for more details.- Parameters:
users
- Whitelist of user IDs to apply- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If null is provided- See Also:
clearMentionedUsers()
,AllowedMentions.mentionUsers(String...)
-
mentionRoles
@Nonnull public MessageBuilder mentionRoles(@Nonnull java.lang.String... roles)
Adds the providedRoles
to the whitelist of mentions.
The provided list must only contain IDs of roles.
The content will not be affected by this. To append a mention useappend("<@&").append(id).append(">")
.See
AllowedMentions.mentionRoles(String...)
for more details.- Parameters:
roles
- Whitelist of role IDs to apply- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If null is provided- See Also:
clearMentionedRoles()
,AllowedMentions.mentionRoles(String...)
-
mentionUsers
@Nonnull public MessageBuilder mentionUsers(@Nonnull long... users)
Adds the providedUsers
to the whitelist of mentions.
The provided list must only contain IDs of users.
The content will not be affected by this. To append a mention useappend("<@").append(id).append(">")
.See
AllowedMentions.mentionUsers(long...)
for more details.- Parameters:
users
- Whitelist of user IDs to apply- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If null is provided- See Also:
clearMentionedUsers()
,AllowedMentions.mentionUsers(long...)
-
mentionRoles
@Nonnull public MessageBuilder mentionRoles(@Nonnull long... roles)
Adds the providedRoles
to the whitelist of mentions.
The provided list must only contain IDs of roles.
The content will not be affected by this. To append a mention useappend("<@&").append(id).append(">")
.See
AllowedMentions.mentionRoles(long...)
for more details.- Parameters:
roles
- Whitelist of role IDs to apply- Returns:
- The MessageBuilder instance. Useful for chaining.
- Throws:
java.lang.IllegalArgumentException
- If null is provided- See Also:
clearMentionedRoles()
,AllowedMentions.mentionRoles(long...)
-
stripMentions
@Nonnull @Deprecated @ForRemoval(deadline="4.4.0") @ReplaceWith("setAllowedMentions(Collections.emptyList())") @DeprecatedSince("4.2.0") public MessageBuilder stripMentions(@Nonnull JDA jda)
Deprecated.This is not a reliable way to remove mentions from the content, you should usesetAllowedMentions(Collection)
instead.Removes all mentions and replaces them with the closest looking textual representation.Use this over
stripMentions(Guild)
ifUser
mentions should be replaced with theirUser.getName()
instead of their Nicknames.- Parameters:
jda
- The JDA instance used to resolve the mentions.- Returns:
- The MessageBuilder instance. Useful for chaining.
-
stripMentions
@Nonnull @Deprecated @ForRemoval(deadline="4.4.0") @ReplaceWith("setAllowedMentions(Collections.emptyList())") @DeprecatedSince("4.2.0") public MessageBuilder stripMentions(@Nonnull Guild guild)
Deprecated.This is not a reliable way to remove mentions from the content, you should usesetAllowedMentions(Collection)
instead.Removes all mentions and replaces them with the closest looking textual representation.Use this over
stripMentions(JDA)
ifUser
mentions should be replaced with their nicknames in a specific guild based.
UsesMember.getEffectiveName()
- Parameters:
guild
- the guild forUser
mentions- Returns:
- The MessageBuilder instance. Useful for chaining.
-
stripMentions
@Nonnull @Deprecated @ForRemoval(deadline="4.4.0") @ReplaceWith("denyMentions(types)") @DeprecatedSince("4.2.0") public MessageBuilder stripMentions(@Nonnull Guild guild, @Nonnull Message.MentionType... types)
Deprecated.This is not a reliable way to remove mentions from the content, you should usesetAllowedMentions(Collection)
instead.Removes all mentions of the specified types and replaces them with the closest looking textual representation.Use this over
stripMentions(JDA, Message.MentionType...)
ifUser
mentions should be replaced with their nicknames in a specific guild based.
UsesMember.getEffectiveName()
- Parameters:
guild
- the guild forUser
mentionstypes
- theMentionTypes
that should be stripped- Returns:
- The MessageBuilder instance. Useful for chaining.
-
stripMentions
@Nonnull @Deprecated @ForRemoval(deadline="4.4.0") @ReplaceWith("denyMentions(types)") @DeprecatedSince("4.2.0") public MessageBuilder stripMentions(@Nonnull JDA jda, @Nonnull Message.MentionType... types)
Deprecated.This is not a reliable way to remove mentions from the content, you should usesetAllowedMentions(Collection)
instead.Removes all mentions of the specified types and replaces them with the closest looking textual representation.Use this over
stripMentions(Guild, Message.MentionType...)
ifUser
mentions should be replaced with theirUser.getName()
.- Parameters:
jda
- The JDA instance used to resolve the mentions.types
- theMentionTypes
that should be stripped- Returns:
- The MessageBuilder instance. Useful for chaining.
-
getStringBuilder
@Nonnull public java.lang.StringBuilder getStringBuilder()
Returns the underlyingStringBuilder
.- Returns:
- The
StringBuilder
used by thisMessageBuilder
-
clear
@Nonnull public MessageBuilder clear()
Clears the current builder. Useful for mass message creation.This will not clear the allowed mentions.
- Returns:
- The MessageBuilder instance. Useful for chaining.
-
indexOf
public int indexOf(@Nonnull 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.If no such value of
target
exists, then-1
is returned.- Parameters:
target
- the substring to search for.fromIndex
- the index from which to start the search.endIndex
- the index at which to end the search.- Returns:
- the index of the first occurrence of the specified substring between
the specified indices or
-1
if there is no such occurrence. - Throws:
java.lang.IndexOutOfBoundsException
-
-
lastIndexOf
public int lastIndexOf(@Nonnull 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. If no such value oftarget
exists, then-1
is returned.- Parameters:
target
- the substring to search for.fromIndex
- the index from which to start the search.endIndex
- the index at which to end the search.- Returns:
- the index of the last occurrence of the specified substring between
the specified indices or
-1
if there is no such occurrence. - Throws:
java.lang.IndexOutOfBoundsException
-
-
build
@Nonnull public Message build()
Creates aMessage
object from this MessageBuilderHint: You can use
build(int, int)
orbuildAll(SplitPolicy...)
as possible ways to deal with the 2000 character cap.- Returns:
- the created
Message
- Throws:
java.lang.IllegalStateException
-- If you attempt to build() an empty Message (
length()
is0
and noMessageEmbed
was provided tosetEmbed(net.dv8tion.jda.api.entities.MessageEmbed)
) - If you attempt to build() a Message with more than 2000 characters of content.
- If you attempt to build() an empty Message (
-
buildAll
@Nonnull public java.util.Queue<Message> buildAll(@Nullable MessageBuilder.SplitPolicy... policy)
Creates aQueue
ofMessage
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 each message will be split after exactly 2000 chars.This is not Markdown safe. An easy workaround is to include Zero Width Spaces as predetermined breaking points to the message and only split on them.
- Parameters:
policy
- TheMessageBuilder.SplitPolicy
defining how to split the text in the MessageBuilder into different, individual messages.- Returns:
- the created
Messages
-
-