Interface MessageEditRequest<R extends MessageEditRequest<R>>
- Type Parameters:
R
- The return type for method chaining convenience
- All Superinterfaces:
MessageData
,MessageRequest<R>
- All Known Subinterfaces:
MessageEditAction
,MessageEditCallbackAction
,WebhookMessageEditAction<T>
- All Known Implementing Classes:
MessageEditBuilder
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault R
Replaces all the fields configured on this request with the data provided byMessageCreateData
.applyData
(MessageEditData data) Applies the providedMessageEditData
to this request.default R
applyMessage
(Message message) Applies all the data of the providedMessage
and attempts to copy it.boolean
Whether this request will replace the message and remove everything that is not currently set.setAttachments
(Collection<? extends AttachedFile> attachments) TheAttachedFiles
that should be attached to the message.default R
setAttachments
(AttachedFile... attachments) TheAttachedFiles
that should be attached to the message.default R
setFiles
(Collection<? extends FileUpload> files) TheFileUploads
that should be attached to the message.setReplace
(boolean isReplace) Whether to replace the existing message completely.Methods inherited from interface net.dv8tion.jda.api.utils.messages.MessageData
getAllowedMentions, getAttachments, getComponents, getContent, getEmbeds, getMentionedRoles, getMentionedUsers, isMentionRepliedUser, isSuppressEmbeds
Methods inherited from interface net.dv8tion.jda.api.utils.messages.MessageRequest
mention, mention, mentionRepliedUser, mentionRoles, mentionRoles, mentionRoles, mentionUsers, mentionUsers, mentionUsers, setActionRow, setActionRow, setAllowedMentions, setComponents, setComponents, setContent, setEmbeds, setEmbeds, setFiles, setSuppressEmbeds
-
Method Details
-
setAttachments
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();
- 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
- Throws:
IllegalArgumentException
- If null is provided inside the collection- See Also:
-
setAttachments
TheAttachedFiles
that should be attached to the message.
This will replace all the existing attachments on the message, you can usenew FileAttachment[0]
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 // Take the first attachment of the message, all others will be removed AttachedFile attachment = message.getAttachments().get(0); // 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 // Edit request to keep the first attachment, and add one more file to the message message.editMessage("New content") .setAttachments(attachment, file) .queue();
- Parameters:
attachments
- TheAttachedFiles
to attach to the message, null or an empty array will set the attachments to an empty list and remove them from the message- Returns:
- The same instance for chaining
- Throws:
IllegalArgumentException
- If null is provided- See Also:
-
setFiles
Description copied from interface:MessageRequest
TheFileUploads
that should be attached to the message.
This will replace all the existing attachments on the message, if this is an edit request. You can usesetAttachments(Collection)
to keep existing attachments, instead of this method.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
Create an embed with a custom image, uploaded alongside the message:MessageEmbed embed = new EmbedBuilder() .setDescription("Image of a cute cat") .setImage("attachment://cat.png") // here "cat.png" is the name used in the FileUpload.fromData factory method .build(); // 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 channel.sendMessageEmbeds(embed) .setFiles(file) .queue();
- Specified by:
setFiles
in interfaceMessageRequest<R extends MessageEditRequest<R>>
- Parameters:
files
- TheFileUploads
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
-
setReplace
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();
- 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
boolean isReplace()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.
- Returns:
- True, if this is a replacing request
- See Also:
-
applyData
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
.- Parameters:
data
- The message edit data to apply- Returns:
- The same instance for chaining
- Throws:
IllegalArgumentException
- If the data is null
-
applyCreateData
Replaces all the fields configured on this request with the data provided byMessageCreateData
.
This will make this request areplace request
.- Parameters:
data
- The message create data to apply- Returns:
- The same instance for chaining
- Throws:
IllegalArgumentException
- If the data is null
-
applyMessage
Description copied from interface:MessageRequest
Applies all the data of the providedMessage
and attempts to copy it.
This cannot copy the file attachments of the message, they must be manually downloaded and provided toMessageRequest.setFiles(FileUpload...)
.
Theallowed mentions
are not updated to reflect the provided message, and might mention users that the message did not.For edit requests, this will set
setReplace(boolean)
totrue
, and replace the existing message completely.- Specified by:
applyMessage
in interfaceMessageRequest<R extends MessageEditRequest<R>>
- Parameters:
message
- The message to copy the data from- Returns:
- The same instance for chaining
-