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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapplyData(MessageEditData data) Applies the providedMessageEditDatato 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 allFileUploadsadded to this builder.static MessageEditBuilderfrom(MessageEditData data) Factory method to start a builder from an existing instance ofMessageEditData.static MessageEditBuilderFactory method to start a builder from an existing instance ofMessageCreateData.static MessageEditBuilderfromMessage(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.booleanisEmpty()Whether this builder is considered empty, this checks for all required fields of the request type.booleanWhether this request will replace the message and remove everything that is not currently set.booleanisValid()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 ofRolesthat 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 ofUsersthat 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 theMentionTypesthat should be parsed.setAttachments(Collection<? extends AttachedFile> attachments) TheAttachedFilesthat should be attached to the message.setComponents(Collection<? extends MessageTopLevelComponent> components) TheMessageTopLevelComponentsthat should be attached to the message.setContent(String content) The message content, which shows above embeds and attachments.setEmbeds(Collection<? extends MessageEmbed> embeds) TheMessageEmbedsthat 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.useComponentsV2(boolean use) Sets whether this message is allowed to use V2 components, this is disabled by default.Methods inherited from class net.dv8tion.jda.api.utils.messages.AbstractMessageBuilder
getAllowedMentions, getComponents, getContent, getEmbeds, getMentionedRoles, getMentionedUsers, getMessageFlagsRaw, isMentionRepliedUser, isSuppressEmbeds, isUsingComponentsV2Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.dv8tion.jda.api.utils.messages.MessageData
getAllowedMentions, getComponents, getComponentTree, getContent, getEmbeds, getMentionedRoles, getMentionedUsers, isMentionRepliedUser, isSuppressEmbeds, isUsingComponentsV2Methods inherited from interface net.dv8tion.jda.api.utils.messages.MessageEditRequest
applyCreateData, applyMessage, setAttachments, setFilesMethods inherited from interface net.dv8tion.jda.api.utils.messages.MessageRequest
mention, mentionRoles, mentionRoles, mentionUsers, mentionUsers, setComponents, setComponents, setEmbeds, setFiles, useComponentsV2
-
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:MessageRequestWhether 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:
mentionRepliedUserin interfaceMessageRequest<MessageEditBuilder>- Overrides:
mentionRepliedUserin 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:MessageRequestSets theMentionTypesthat 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
nullis 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:
setAllowedMentionsin interfaceMessageRequest<MessageEditBuilder>- Overrides:
setAllowedMentionsin 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 passnullorEnumSet.allOf(MentionType.class)to allow all mentions.- Returns:
- The same instance for chaining
-
mention
Description copied from interface:MessageRequestUsed to provide a whitelist forUsers,MembersandRolesthat 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:
mentionin interfaceMessageRequest<MessageEditBuilder>- Overrides:
mentionin 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:MessageRequestUsed to provide a whitelist ofUsersthat 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:
mentionUsersin interfaceMessageRequest<MessageEditBuilder>- Overrides:
mentionUsersin 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:MessageRequestUsed to provide a whitelist ofRolesthat 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:
mentionRolesin interfaceMessageRequest<MessageEditBuilder>- Overrides:
mentionRolesin 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:MessageEditRequestTheAttachedFilesthat should be attached to the message.
This will replace all the existing attachments on the message, you can useCollections.emptyList()ornullto 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:
setAttachmentsin interfaceMessageEditRequest<MessageEditBuilder>- Parameters:
attachments- TheAttachedFilesto 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:MessageDataThe 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:
getAttachmentsin interfaceMessageData- Returns:
- The currently configured attachments, or an empty list if none were set yet
- See Also:
-
setReplace
Description copied from interface:MessageEditRequestWhether 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 thecontentof 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:
setReplacein 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:MessageEditRequestWhether 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:
isReplacein interfaceMessageEditRequest<MessageEditBuilder>- Returns:
- True, if this is a replacing request
- See Also:
-
setContent
Description copied from interface:MessageRequestThe message content, which shows above embeds and attachments.- Specified by:
setContentin interfaceMessageRequest<MessageEditBuilder>- Overrides:
setContentin classAbstractMessageBuilder<MessageEditData,MessageEditBuilder> - Parameters:
content- The content (up to 2000 characters)- Returns:
- The same instance for chaining
-
setEmbeds
Description copied from interface:MessageRequestTheMessageEmbedsthat should be attached to the message.
You can useCollections.emptyList()to remove all embeds from the message.This requires
Permission.MESSAGE_EMBED_LINKSin the channel.- Specified by:
setEmbedsin interfaceMessageRequest<MessageEditBuilder>- Overrides:
setEmbedsin 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 MessageTopLevelComponent> components) Description copied from interface:MessageRequestTheMessageTopLevelComponentsthat should be attached to the message.
You can useCollections.emptyList()to remove all components from the message.Example: Set action rows
final List<MessageTopLevelComponent> 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 here") .setComponents(list) .queue();Example: Remove action rows
channel.sendMessage("Content here") .setComponents(Collections.emptyList()) .queue();- Specified by:
setComponentsin interfaceMessageRequest<MessageEditBuilder>- Overrides:
setComponentsin classAbstractMessageBuilder<MessageEditData,MessageEditBuilder> - Parameters:
components- TheMessageTopLevelComponentsto set, can be empty to remove components, can contain up to 5 V1 components. There are no limits for V2 components outside the total tree size (40).- Returns:
- The same instance for chaining
-
useComponentsV2
Description copied from interface:MessageRequestSets whether this message is allowed to use V2 components, this is disabled by default.Using V2 components removes the top-level component limit, and allows more components in total (40).
They also allow you to use a larger choice of components, such as any component extendingMessageTopLevelComponent, as long as they are compatible.
The character limit for the messages also gets changed to 4000.This, however, comes with a few drawbacks:
- You cannot send content, embeds, polls or stickers
- It does not support voice messages
- It does not support previewing files
- URLs don't create embeds
- You cannot switch this message back to not using Components V2 (you can however upgrade a message to V2)
A default value can be set in
MessageRequest.setDefaultUseComponentsV2(boolean).- Specified by:
useComponentsV2in interfaceMessageRequest<MessageEditBuilder>- Overrides:
useComponentsV2in classAbstractMessageBuilder<MessageEditData,MessageEditBuilder> - Parameters:
use-trueto enable V2 components,falseto disabled them.- Returns:
- The same instance for chaining
- See Also:
-
setSuppressEmbeds
Description copied from interface:MessageRequestSet whether embeds should be suppressed on this message.
This also includes rich embeds added viaMessageRequest.setEmbeds(MessageEmbed...).Default: false
- Specified by:
setSuppressEmbedsin interfaceMessageRequest<MessageEditBuilder>- Overrides:
setSuppressEmbedsin classAbstractMessageBuilder<MessageEditData,MessageEditBuilder> - Parameters:
suppress- True, if all embeds should be suppressed- Returns:
- The same instance for chaining
-
applyData
Description copied from interface:MessageEditRequestApplies the providedMessageEditDatato 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:
applyDatain interfaceMessageEditRequest<MessageEditBuilder>- Parameters:
data- The message edit data to apply- Returns:
- The same instance for chaining
-
isEmpty
public boolean isEmpty()Description copied from class:AbstractMessageBuilderWhether 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:
isEmptyin classAbstractMessageBuilder<MessageEditData,MessageEditBuilder> - Returns:
- True, if the builder state is empty
-
isValid
public boolean isValid()Description copied from class:AbstractMessageBuilderWhether 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:
isValidin classAbstractMessageBuilder<MessageEditData,MessageEditBuilder> - Returns:
- True, if the builder is in a valid state
-
build
Description copied from class:AbstractMessageBuilderBuilds a validated instance of this builder's state, which can then be used for requests.- Specified by:
buildin classAbstractMessageBuilder<MessageEditData,MessageEditBuilder> - Returns:
- The validated data instance
-
clear
Description copied from class:AbstractMessageBuilderClears 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:
clearin classAbstractMessageBuilder<MessageEditData,MessageEditBuilder> - Returns:
- The same builder instance for chaining
-
closeFiles
Description copied from class:AbstractMessageBuilderCloses and removes allFileUploadsadded to this builder.This will keep any
AttachmentUpdatesadded to this builder, as those do not require closing. You can useMessageEditRequest.setAttachments(AttachedFile...)to remove them as well.- Specified by:
closeFilesin classAbstractMessageBuilder<MessageEditData,MessageEditBuilder> - Returns:
- The same builder instance for chaining
-