Skip to content

Commit

Permalink
remove old classes, add new embed class with placeholder functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaktushose committed Feb 25, 2025
1 parent 7c88a89 commit 01afd7c
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 773 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.github.kaktushose.jda.commands.dispatching.middleware.internal.Middlewares;
import com.github.kaktushose.jda.commands.dispatching.validation.Validator;
import com.github.kaktushose.jda.commands.dispatching.validation.internal.Validators;
import com.github.kaktushose.jda.commands.embeds.configuration.EmbedConfigurationStage;
import com.github.kaktushose.jda.commands.embeds.EmbedConfiguration;
import com.github.kaktushose.jda.commands.embeds.error.ErrorMessageFactory;
import com.github.kaktushose.jda.commands.extension.Extension;
import com.github.kaktushose.jda.commands.extension.JDACBuilderData;
Expand All @@ -23,7 +23,6 @@
import net.dv8tion.jda.api.interactions.commands.localization.LocalizationFunction;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.*;
import java.util.function.Consumer;
Expand Down Expand Up @@ -188,8 +187,8 @@ public JDACBuilder filterExtensions(@NotNull FilterStrategy strategy, @NotNull S
}

@NotNull
public JDACBuilder embeds(Consumer<EmbedConfigurationStage> config) {
config.accept(embedConfiguration);
public JDACBuilder embeds(Consumer<EmbedConfiguration> config) {
config.accept(embeds);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.kaktushose.jda.commands.dispatching.reply;

import com.github.kaktushose.jda.commands.dispatching.events.ReplyableEvent;
import com.github.kaktushose.jda.commands.embeds.EmbedDTO;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
Expand Down Expand Up @@ -63,15 +62,4 @@ default Message reply(@NotNull String format, @NotNull Object... args) {
/// This might throw [RuntimeException]s if JDA fails to send the message.
Message reply(@NotNull EmbedBuilder builder);

/// Acknowledgement of this event with a text message.
///
/// @param embedDTO the [EmbedDTO] to send
/// @return the [Message] that got created
/// @implSpec Internally this method must call [RestAction#complete()], thus the [Message] object can get
/// returned directly.
///
/// This might throw [RuntimeException]s if JDA fails to send the message.
default Message reply(@NotNull EmbedDTO embedDTO) {
return reply(embedDTO.toEmbedBuilder());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.github.kaktushose.jda.commands.embeds;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.time.temporal.TemporalAccessor;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Embed {

private final EmbedBuilder embedBuilder;

public Embed(EmbedBuilder embedBuilder) {
this.embedBuilder = embedBuilder;
}

public Embed title(String title) {
embedBuilder.setTitle(title);
return this;
}


public Embed title(String title, String url) {
embedBuilder.setTitle(title, url);
return this;
}

public Embed description(CharSequence description) {
embedBuilder.setDescription(description);
return this;
}

public Embed color(int color) {
embedBuilder.setColor(color);
return this;
}

public Embed color(Color color) {
embedBuilder.setColor(color);
return this;
}

public Embed timestamp(TemporalAccessor accessor) {
embedBuilder.setTimestamp(accessor);
return this;
}

public Embed footer(String footer) {
embedBuilder.setFooter(footer);
return this;
}

public Embed footer(String footer, String iconUrl) {
embedBuilder.setFooter(footer, iconUrl);
return this;
}

public Embed thumbnail(String url) {
embedBuilder.setThumbnail(url);
return this;
}

public Embed image(String url) {
embedBuilder.setImage(url);
return this;
}

public Embed author(String name) {
embedBuilder.setAuthor(name);
return this;
}

public Embed author(String name, String url) {
embedBuilder.setAuthor(name, url);
return this;
}

public Embed author(String name, String url, String iconUrl) {
embedBuilder.setAuthor(name, url, iconUrl);
return this;
}

// TODO fields API

public Embed placeholder(String placeholder, Object value) {
DataObject object = embedBuilder.build().toData();
String json = object.toString();
json = json.replaceAll(
String.format(Pattern.quote("{%s}"), Matcher.quoteReplacement(placeholder)),
String.valueOf(value)
);
return new Embed(EmbedBuilder.fromData(DataObject.fromJson(json)));
}

public Embed placeholder(Map<String, Object> values) {
values.forEach(this::placeholder);
return this;
}

public @NotNull MessageCreateData toMessageCreateData() {
return MessageCreateData.fromEmbeds(embedBuilder.build());
}

public MessageEmbed build() {
return embedBuilder.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.kaktushose.jda.commands.embeds;

import org.jetbrains.annotations.NotNull;

import java.util.Locale;
import java.util.function.Supplier;

public interface EmbedConfiguration {

EmbedConfiguration source(@NotNull EmbedDataSource source);

default EmbedConfiguration placeholder(String key, Object value) {
return placeholder(key, () -> value);
}

<T> EmbedConfiguration placeholder(String key, Supplier<T> supplier);

EmbedConfiguration localization(Locale locale, EmbedDataSource embedDataSource);

}
Loading

0 comments on commit 01afd7c

Please # to comment.