Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use deep copy for layout components when applying message data #2236

Merged
merged 8 commits into from Nov 8, 2022
Expand Up @@ -269,6 +269,13 @@ public ActionRow asEnabled()
return withDisabled(false);
}

@Nonnull
@Override
public ActionRow createCopy()
{
return ActionRow.of(components);
}

@Nonnull
@Override
public Component.Type getType()
Expand Down
Expand Up @@ -190,6 +190,15 @@ default boolean isValid()
return true;
}

/**
* Creates a copy of this {@link LayoutComponent}.
* <br>This does not create copies of the contained components.
*
* @return A copy of this {@link LayoutComponent}
*/
@Nonnull
LayoutComponent createCopy();

/**
* Find and replace a component in this layout.
* <br>This will locate and replace the existing component with the specified ID. If you provide null it will be removed instead.
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/FileUpload.java
Expand Up @@ -301,6 +301,17 @@ public String getName()
return name;
}

/**
* The description for the file.
*
* @return The description
*/
@Nullable
public String getDescription()
freya022 marked this conversation as resolved.
Show resolved Hide resolved
{
return description;
}

/**
* The {@link InputStream} representing the data to upload as a file.
*
Expand Down
Expand Up @@ -325,14 +325,18 @@ default R addFiles(@Nonnull FileUpload... files)
default R applyData(@Nonnull MessageCreateData data)
{
Checks.notNull(data, "MessageCreateData");

final List<LayoutComponent> layoutComponents = data.getComponents().stream()
.map(LayoutComponent::createCopy)
.collect(Collectors.toList());
return setContent(data.getContent())
.setAllowedMentions(data.getAllowedMentions())
.mentionUsers(data.getMentionedUsers())
.mentionRoles(data.getMentionedRoles())
.mentionRepliedUser(data.isMentionRepliedUser())
.setEmbeds(data.getEmbeds())
.setTTS(data.isTTS())
.setComponents(data.getComponents())
.setComponents(layoutComponents)
.setFiles(data.getFiles());
freya022 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -377,7 +381,12 @@ default R applyEditData(@Nonnull MessageEditData data)
if (data.isSet(MessageEditBuilder.EMBEDS))
setEmbeds(data.getEmbeds());
if (data.isSet(MessageEditBuilder.COMPONENTS))
setComponents(data.getComponents());
{
final List<LayoutComponent> layoutComponents = data.getComponents().stream()
.map(LayoutComponent::createCopy)
.collect(Collectors.toList());
setComponents(layoutComponents);
}
if (data.isSet(MessageEditBuilder.ATTACHMENTS))
setFiles(data.getFiles());
if (data.isSet(MessageEditBuilder.MENTIONS))
Expand Down
Expand Up @@ -32,6 +32,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Builder specialized for building a {@link MessageEditData}.
Expand Down Expand Up @@ -247,7 +248,12 @@ public MessageEditBuilder applyData(@Nonnull MessageEditData data)
if (data.isSet(EMBEDS))
this.setEmbeds(data.getEmbeds());
if (data.isSet(COMPONENTS))
this.setComponents(data.getComponents());
{
final List<LayoutComponent> layoutComponents = data.getComponents().stream()
.map(LayoutComponent::createCopy)
.collect(Collectors.toList());
this.setComponents(layoutComponents);
}
if (data.isSet(ATTACHMENTS))
this.setAttachments(data.getAttachments());
if (data.isSet(MENTIONS))
Expand Down
Expand Up @@ -17,6 +17,7 @@
package net.dv8tion.jda.api.utils.messages;

import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.interactions.components.LayoutComponent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.AttachedFile;
import net.dv8tion.jda.api.utils.FileUpload;
Expand All @@ -29,6 +30,8 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Specialized abstraction of setters for editing existing messages throughout the API.
Expand Down Expand Up @@ -205,14 +208,17 @@ default R setFiles(@Nullable Collection<? extends FileUpload> files)
@Nonnull
default R applyCreateData(@Nonnull MessageCreateData data)
{
final List<LayoutComponent> layoutComponents = data.getComponents().stream()
.map(LayoutComponent::createCopy)
.collect(Collectors.toList());
return setReplace(true)
.setContent(data.getContent())
.setAllowedMentions(data.getAllowedMentions())
.mentionUsers(data.getMentionedUsers())
.mentionRoles(data.getMentionedRoles())
.mentionRepliedUser(data.isMentionRepliedUser())
.setEmbeds(data.getEmbeds())
.setComponents(data.getComponents())
.setComponents(layoutComponents)
.setFiles(data.getFiles());
}

Expand Down