Class MessageEditBuilder
- All Implemented Interfaces:
MessageData
,MessageEditRequest<MessageEditBuilder>
,MessageRequest<MessageEditBuilder>
MessageEditData
.
These are used to edit messages and allow configuration that either replaces
the message or only updates specific fields.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionapplyData
(MessageEditData data) Applies the providedMessageEditData
to this request.build()
Builds a validated instance of this builder's state, which can then be used for requests.clear()
Clears this builder's state, resetting it to the initial state identical to creating a new instance.Closes and removes allFileUploads
added to this builder.static MessageEditBuilder
from
(MessageEditData data) Factory method to start a builder from an existing instance ofMessageEditData
.static MessageEditBuilder
Factory method to start a builder from an existing instance ofMessageCreateData
.static MessageEditBuilder
fromMessage
(Message message) Factory method to start a builder from an existing instance ofMessage
.List
<? extends AttachedFile> The configured message attachments asAttachedFile
, this is the opposite ofMessageRequest.setFiles(Collection)
and only returns what was set using that setter.boolean
isEmpty()
Whether this builder is considered empty, this checks for all required fields of the request type.boolean
Whether this request will replace the message and remove everything that is not currently set.boolean
isValid()
Whether this builder has a valid state to build.mention
(Collection<? extends IMentionable> mentions) mentionRepliedUser
(boolean mention) Whether to mention the user, when replying to a message.mentionRoles
(Collection<String> roleIds) Used to provide a whitelist ofRoles
that should be pinged, even when they would not be pinged otherwise according to the Set of allowed mention types.mentionUsers
(Collection<String> userIds) Used to provide a whitelist ofUsers
that should be pinged, even when they would not be pinged otherwise according to the Set of allowed mention types.setAllowedMentions
(Collection<Message.MentionType> allowedMentions) Sets theMentionTypes
that should be parsed.setAttachments
(Collection<? extends AttachedFile> attachments) TheAttachedFiles
that should be attached to the message.setComponents
(Collection<? extends LayoutComponent> components) TheLayoutComponents
that should be attached to the message.setContent
(String content) The message content, which shows above embeds and attachments.setEmbeds
(Collection<? extends MessageEmbed> embeds) TheMessageEmbeds
that should be attached to the message.setReplace
(boolean isReplace) Whether to replace the existing message completely.setSuppressEmbeds
(boolean suppress) Set whether embeds should be suppressed on this message.Methods inherited from class net.dv8tion.jda.api.utils.messages.AbstractMessageBuilder
getAllowedMentions, getComponents, getContent, getEmbeds, getMentionedRoles, getMentionedUsers, getMessageFlagsRaw, isMentionRepliedUser, isSuppressEmbeds
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.dv8tion.jda.api.utils.messages.MessageData
getAllowedMentions, getComponents, getContent, getEmbeds, getMentionedRoles, getMentionedUsers, isMentionRepliedUser, isSuppressEmbeds
Methods inherited from interface net.dv8tion.jda.api.utils.messages.MessageEditRequest
applyCreateData, applyMessage, setAttachments, setFiles
Methods inherited from interface net.dv8tion.jda.api.utils.messages.MessageRequest
mention, mentionRoles, mentionRoles, mentionUsers, mentionUsers, setActionRow, setActionRow, setComponents, setEmbeds, setFiles
-
Constructor Details
-
MessageEditBuilder
public MessageEditBuilder()
-
-
Method Details
-
from
Factory method to start a builder from an existing instance ofMessageEditData
.
Equivalent tonew MessageEditBuilder().applyData(data)
.- Parameters:
data
- The message edit data to apply- Returns:
- A new MessageEditBuilder instance with the applied data
- Throws:
IllegalArgumentException
- If null is provided- See Also:
-
fromCreateData
Factory method to start a builder from an existing instance ofMessageCreateData
.
Equivalent tonew MessageEditBuilder().applyCreateData(data)
.This will set the request to be
replacing
.- Parameters:
data
- The message create data to apply- Returns:
- A new MessageEditBuilder instance with the applied data
- Throws:
IllegalArgumentException
- If null is provided- See Also:
-
fromMessage
Factory method to start a builder from an existing instance ofMessage
.
Equivalent tonew MessageEditBuilder().applyMessage(data)
.This will set the request to be
replacing
.- Parameters:
message
- The message to apply- Returns:
- A new MessageEditBuilder instance with the applied data
- Throws:
IllegalArgumentException
- If null is provided or the message is a system message- See Also:
-
mentionRepliedUser
Description copied from interface:MessageRequest
Whether to mention the user, when replying to a message.
This only matters in combination withMessageCreateAction.setMessageReference(...)
!This is true by default but can be configured using
MessageRequest.setDefaultMentionRepliedUser(boolean)
!- Specified by:
mentionRepliedUser
in interfaceMessageRequest<MessageEditBuilder>
- Overrides:
mentionRepliedUser
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Parameters:
mention
- True, to mention the author in the referenced message- Returns:
- The same instance for chaining
-
setAllowedMentions
@Nonnull public MessageEditBuilder setAllowedMentions(@Nullable Collection<Message.MentionType> allowedMentions) Description copied from interface:MessageRequest
Sets theMentionTypes
that should be parsed.
If a message is sent with an empty Set of MentionTypes, then it will not ping any User, Role or@everyone
/@here
, while still showing up as mention tag.If
null
is provided to this method, then all Types will be mentionable (unless whitelisting via one of themention*
methods is used).Note: A default for this can be set using
AllowedMentions.setDefaultMentions(Collection)
.- Specified by:
setAllowedMentions
in interfaceMessageRequest<MessageEditBuilder>
- Overrides:
setAllowedMentions
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Parameters:
allowedMentions
- MentionTypes that are allowed to being parsed and mentioned. All other mention types will not be mentioned by this message. You can passnull
orEnumSet.allOf(MentionType.class)
to allow all mentions.- Returns:
- The same instance for chaining
-
mention
Description copied from interface:MessageRequest
Used to provide a whitelist forUsers
,Members
andRoles
that should be pinged, even when they would not be pinged otherwise according to the Set of allowed mention types.
On other types ofIMentionable
, this does nothing.Note: When a User/Member is whitelisted this way, then parsing of User mentions is automatically disabled (same applies to Roles).
Also note that whitelisting users or roles implicitly disables parsing of other mentions, if not otherwise set viaMessageRequest.setDefaultMentions(Collection)
orMessageRequest.setAllowedMentions(Collection)
.- Specified by:
mention
in interfaceMessageRequest<MessageEditBuilder>
- Overrides:
mention
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Parameters:
mentions
- Users, Members and Roles that should be explicitly whitelisted to be pingable.- Returns:
- The same instance for chaining
- See Also:
-
mentionUsers
Description copied from interface:MessageRequest
Used to provide a whitelist ofUsers
that should be pinged, even when they would not be pinged otherwise according to the Set of allowed mention types.Note: When a User is whitelisted this way, then parsing of User mentions is automatically disabled.
Also note that whitelisting users or roles implicitly disables parsing of other mentions, if not otherwise set viaMessageRequest.setDefaultMentions(Collection)
orMessageRequest.setAllowedMentions(Collection)
.- Specified by:
mentionUsers
in interfaceMessageRequest<MessageEditBuilder>
- Overrides:
mentionUsers
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Parameters:
userIds
- Ids of Users that should be explicitly whitelisted to be pingable.- Returns:
- The same instance for chaining
- See Also:
-
mentionRoles
Description copied from interface:MessageRequest
Used to provide a whitelist ofRoles
that should be pinged, even when they would not be pinged otherwise according to the Set of allowed mention types.Note: When a Role is whitelisted this way, then parsing of Role mentions is automatically disabled.
Also note that whitelisting users or roles implicitly disables parsing of other mentions, if not otherwise set viaMessageRequest.setDefaultMentions(Collection)
orMessageRequest.setAllowedMentions(Collection)
.- Specified by:
mentionRoles
in interfaceMessageRequest<MessageEditBuilder>
- Overrides:
mentionRoles
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Parameters:
roleIds
- Ids of Roles that should be explicitly whitelisted to be pingable.- Returns:
- The same instance for chaining
- See Also:
-
setAttachments
@Nonnull public MessageEditBuilder setAttachments(@Nullable Collection<? extends AttachedFile> attachments) Description copied from interface:MessageEditRequest
TheAttachedFiles
that should be attached to the message.
This will replace all the existing attachments on the message, you can useCollections.emptyList()
ornull
to clear all attachments.Resource Handling Note: Once the request is handed off to the requester, for example when you call
RestAction.queue()
, the requester will automatically clean up all opened files by itself. You are only responsible to close them yourself if it is never handed off properly. For instance, if an exception occurs after usingFileUpload.fromData(File)
, before callingRestAction.queue()
. You can safely use a try-with-resources to handle this, sinceFileUpload.close()
becomes ineffective once the request is handed off.Example
// Here "message" is an instance of the Message interface // Creates a list of the currently attached files of the message, important to get the generic parameter of the list right List<AttachedFile> attachments = new ArrayList<>(message.getAttachments()); // The name here will be "cat.png" to discord, what the file is called on your computer is irrelevant and only used to read the data of the image. FileUpload file = FileUpload.fromData(new File("mycat-final-copy.png"), "cat.png"); // Opens the file called "cat.png" and provides the data used for sending // Adds another file to upload in addition the current attachments of the message attachments.add(file); message.editMessage("New content") .setAttachments(attachments) .queue();
- Specified by:
setAttachments
in interfaceMessageEditRequest<MessageEditBuilder>
- Parameters:
attachments
- TheAttachedFiles
to attach to the message, null or an empty list will set the attachments to an empty list and remove them from the message- Returns:
- The same instance for chaining
- See Also:
-
getAttachments
Description copied from interface:MessageData
The configured message attachments asAttachedFile
, this is the opposite ofMessageRequest.setFiles(Collection)
and only returns what was set using that setter.For message edit requests, this will not be the current file attachments of the message.
- Specified by:
getAttachments
in interfaceMessageData
- Returns:
- The currently configured attachments, or an empty list if none were set yet
- See Also:
-
setReplace
Description copied from interface:MessageEditRequest
Whether to replace the existing message completely.By default, edit requests will only update the message fields which were explicitly set. Changing this to
true
, will instead replace everything and remove all unset fields.Example Default
A request such as this will only edit thecontent
of the message, and leave any existing embeds or attachments intact.message.editMessage("hello").queue();
Example Replace
A request such as this will replace the entire message, and remove any existing embeds, attachments, components, etc.message.editMessage("hello").setReplace(true).queue();
- Specified by:
setReplace
in interfaceMessageEditRequest<MessageEditBuilder>
- Parameters:
isReplace
- True, if only things explicitly set on this request should be present after the message is edited.- Returns:
- The same message edit request builder
-
isReplace
public boolean isReplace()Description copied from interface:MessageEditRequest
Whether this request will replace the message and remove everything that is not currently set.If this is false, the request will only edit the message fields which were explicitly set.
- Specified by:
isReplace
in interfaceMessageEditRequest<MessageEditBuilder>
- Returns:
- True, if this is a replacing request
- See Also:
-
setContent
Description copied from interface:MessageRequest
The message content, which shows above embeds and attachments.- Specified by:
setContent
in interfaceMessageRequest<MessageEditBuilder>
- Overrides:
setContent
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Parameters:
content
- The content (up to 2000 characters)- Returns:
- The same instance for chaining
-
setEmbeds
Description copied from interface:MessageRequest
TheMessageEmbeds
that should be attached to the message.
You can useCollections.emptyList()
to remove all embeds from the message.This requires
Permission.MESSAGE_EMBED_LINKS
in the channel.- Specified by:
setEmbeds
in interfaceMessageRequest<MessageEditBuilder>
- Overrides:
setEmbeds
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Parameters:
embeds
- The embeds to attach to the message (up to 10)- Returns:
- The same instance for chaining
- See Also:
-
setComponents
@Nonnull public MessageEditBuilder setComponents(@Nonnull Collection<? extends LayoutComponent> components) Description copied from interface:MessageRequest
TheLayoutComponents
that should be attached to the message.
You can useCollections.emptyList()
to remove all components from the message.The most commonly used layout is
ActionRow
.Example: Set action rows
final List<LayoutComponent> list = new ArrayList<>(); list.add(ActionRow.of(selectMenu); // first row list.add(ActionRow.of(button1, button2)); // second row (shows below the first) channel.sendMessage("Content is still required") .setComponents(list) .queue();
Example: Remove action rows
channel.sendMessage("Content is still required") .setComponents(Collections.emptyList()) .queue();
- Specified by:
setComponents
in interfaceMessageRequest<MessageEditBuilder>
- Overrides:
setComponents
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Parameters:
components
- The components for the message (up to 5)- Returns:
- The same instance for chaining
-
setSuppressEmbeds
Description copied from interface:MessageRequest
Set whether embeds should be suppressed on this message.
This also includes rich embeds added viaMessageRequest.setEmbeds(MessageEmbed...)
.Default: false
- Specified by:
setSuppressEmbeds
in interfaceMessageRequest<MessageEditBuilder>
- Overrides:
setSuppressEmbeds
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Parameters:
suppress
- True, if all embeds should be suppressed- Returns:
- The same instance for chaining
-
applyData
Description copied from interface:MessageEditRequest
Applies the providedMessageEditData
to this request.Note that this method will only call the setters which were also configured when building the message edit data instance, unless it was set to
replace
.- Specified by:
applyData
in interfaceMessageEditRequest<MessageEditBuilder>
- Parameters:
data
- The message edit data to apply- Returns:
- The same instance for chaining
-
isEmpty
public boolean isEmpty()Description copied from class:AbstractMessageBuilder
Whether this builder is considered empty, this checks for all required fields of the request type.
On a create request, this checks forcontent
,embeds
,components
, andfiles
.
An edit request is only considered empty if no setters were called. And never empty, if the builder is areplace request
.- Specified by:
isEmpty
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Returns:
- True, if the builder state is empty
-
isValid
public boolean isValid()Description copied from class:AbstractMessageBuilder
Whether this builder has a valid state to build.
If this isfalse
, thenAbstractMessageBuilder.build()
throws anIllegalStateException
. You can check the exception docs onAbstractMessageBuilder.build()
for specifics.- Specified by:
isValid
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Returns:
- True, if the builder is in a valid state
-
build
Description copied from class:AbstractMessageBuilder
Builds a validated instance of this builder's state, which can then be used for requests.- Specified by:
build
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Returns:
- The validated data instance
-
clear
Description copied from class:AbstractMessageBuilder
Clears this builder's state, resetting it to the initial state identical to creating a new instance.WARNING: This will remove all the files added to the builder, but will not close them. You can use
AbstractMessageBuilder.closeFiles()
before callingclear()
to close the files explicitly.- Overrides:
clear
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Returns:
- The same builder instance for chaining
-
closeFiles
Description copied from class:AbstractMessageBuilder
Closes and removes allFileUploads
added to this builder.This will keep any
AttachmentUpdates
added to this builder, as those do not require closing. You can useMessageEditRequest.setAttachments(AttachedFile...)
to remove them as well.- Specified by:
closeFiles
in classAbstractMessageBuilder<MessageEditData,
MessageEditBuilder> - Returns:
- The same builder instance for chaining
-