package xyz.etztech.embed; import xyz.etztech.Javacord; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Embed { private String content; private String title; private String description; private String url; private int color; private OffsetDateTime timestamp; private Footer footer; private Thumbnail thumbnail; private Image image; private Author author; private List fields; public Embed() { this.content = ""; this.title = ""; this.description = ""; this.url = ""; this.color = 0; this.timestamp = null; this.footer = null; this.thumbnail = null; this.image = null; this.author = null; this.fields = new ArrayList<>(); } public Embed content(String content) { this.content = content; return this; } public Embed title(String title) { this.title = title; return this; } public Embed description(String description) { this.description = description; return this; } public Embed url(String url) { this.url = url; return this; } public Embed color(int color) { this.color = color; return this; } public Embed timestamp(OffsetDateTime timestamp) { this.timestamp = timestamp; return this; } public Embed footer(Footer footer) { this.footer = footer; return this; } public Embed thumbnail(Thumbnail thumbnail) { this.thumbnail = thumbnail; return this; } public Embed image(Image image) { this.image = image; return this; } public Embed author(Author author) { this.author = author; return this; } public Embed fields(List fields) { this.fields = fields; return this; } public Embed addField(Field field) { this.fields.add(field); return this; } public String toJSON() { StringBuilder builder = new StringBuilder("{"); List json = new ArrayList<>(); if (!"".equals(title)) json.add(String.format("\"title\": \"%s\"", Javacord.escapeQuote(title))); if (!"".equals(description)) json.add(String.format("\"description\": \"%s\"", Javacord.escapeQuote(description))); if (!"".equals(url)) json.add(String.format("\"url\": \"%s\"", Javacord.escapeQuote(url))); if (color != 0) json.add(String.format("\"color\": \"%d\"", color)); if (timestamp != null) json.add(String.format("\"timestamp\": \"%s\"", timestamp.format(DateTimeFormatter.ISO_INSTANT))); if (footer != null) json.add(String.format("\"footer\": %s", footer.toJSON())); if (thumbnail != null) json.add(String.format("\"thumbnail\": %s", thumbnail.toJSON())); if (image != null) json.add(String.format("\"image\": %s", image.toJSON())); if (author != null) json.add(String.format("\"author\": %s", author.toJSON())); if (fields.size() > 0) json.add(String.format("\"fields\": [%s]", fields.stream().map(Field::toJSON).collect(Collectors.joining(",")))); builder.append(String.join(",", json)); return builder.append("}").toString(); } public String toString() { StringBuilder builder = new StringBuilder("{"); List json = new ArrayList<>(); if (!"".equals(content)) json.add(String.format("\"content\": \"%s\"", Javacord.escapeQuote(content))); if (!"{}".equals(toJSON())) json.add(String.format("\"embed\": %s", toJSON())); builder.append(String.join(",", json)); return builder.append("}").toString(); } }