Skip to content

Commit

Permalink
Use deep copy for layout components when applying message data (#2236)
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Nov 8, 2022
1 parent 7660750 commit f181320
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
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()
{
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());
}

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

0 comments on commit f181320

Please sign in to comment.