From d1ad28c5dfc752671d3374f3b5a1576b11ac902b Mon Sep 17 00:00:00 2001 From: Etzelia Date: Thu, 30 Jul 2020 16:25:24 -0500 Subject: [PATCH] Initial Commit Signed-off-by: Etzelia --- .gitignore | 4 + LICENSE | 7 + README.md | 7 + pom.xml | 31 +++++ src/main/java/xyz/etztech/Javacord.java | 15 +++ src/main/java/xyz/etztech/embed/Author.java | 31 +++++ src/main/java/xyz/etztech/embed/Embed.java | 127 ++++++++++++++++++ src/main/java/xyz/etztech/embed/Field.java | 34 +++++ src/main/java/xyz/etztech/embed/Footer.java | 35 +++++ src/main/java/xyz/etztech/embed/Image.java | 31 +++++ .../java/xyz/etztech/embed/Thumbnail.java | 7 + src/main/java/xyz/etztech/embed/Webhook.java | 27 ++++ src/test/java/Test.java | 15 +++ 13 files changed, 371 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/xyz/etztech/Javacord.java create mode 100644 src/main/java/xyz/etztech/embed/Author.java create mode 100644 src/main/java/xyz/etztech/embed/Embed.java create mode 100644 src/main/java/xyz/etztech/embed/Field.java create mode 100644 src/main/java/xyz/etztech/embed/Footer.java create mode 100644 src/main/java/xyz/etztech/embed/Image.java create mode 100644 src/main/java/xyz/etztech/embed/Thumbnail.java create mode 100644 src/main/java/xyz/etztech/embed/Webhook.java create mode 100644 src/test/java/Test.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..29a9f89 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# IntelliJ +.idea/ +*.iml +target/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..82b4eb1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2020 Etzelia + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f97556f --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Javacord + +A small, no-dependency Discord utility library. Currently used for webhooks/embeds. + +## License + +[MIT](LICENSE``) \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3496010 --- /dev/null +++ b/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + xyz.etztech + javacord + jar + 0.0.1 + + + + Etzelia + etzelia@hotmail.com + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + \ No newline at end of file diff --git a/src/main/java/xyz/etztech/Javacord.java b/src/main/java/xyz/etztech/Javacord.java new file mode 100644 index 0000000..cd1de57 --- /dev/null +++ b/src/main/java/xyz/etztech/Javacord.java @@ -0,0 +1,15 @@ +package xyz.etztech; + +public class Javacord { + + public static String escapeFormat(String input) { + return input + .replaceAll("_", "\\\\\\\\_") + .replaceAll("\\*", "\\\\\\\\*") + .replaceAll("~", "\\\\\\\\~"); + } + + public static String escapeQuote(String input) { + return input.replaceAll("\"", "\\\\\""); + } +} diff --git a/src/main/java/xyz/etztech/embed/Author.java b/src/main/java/xyz/etztech/embed/Author.java new file mode 100644 index 0000000..d9744de --- /dev/null +++ b/src/main/java/xyz/etztech/embed/Author.java @@ -0,0 +1,31 @@ +package xyz.etztech.embed; + +import xyz.etztech.Javacord; + +import java.util.ArrayList; +import java.util.List; + +public class Author { + private final String name; + private final String url; + private final String iconURL; + private final String proxyIconURL; + + public Author(String name, String url, String iconURL, String proxyIconURL) { + this.name = name; + this.url = url; + this.iconURL = iconURL; + this.proxyIconURL = proxyIconURL; + } + + public String toJSON() { + StringBuilder builder = new StringBuilder("{"); + List json = new ArrayList<>(); + if (!"".equals(name)) json.add(String.format("\"name\": \"%s\"", Javacord.escapeQuote(name))); + if (!"".equals(url)) json.add(String.format("\"url\": \"%s\"", Javacord.escapeQuote(url))); + if (!"".equals(iconURL)) json.add(String.format("\"icon_url\": \"%s\"", Javacord.escapeQuote(iconURL))); + if (!"".equals(proxyIconURL)) json.add(String.format("\"proxy_icon_url\": \"%s\"", Javacord.escapeQuote(proxyIconURL))); + builder.append(String.join(",", json)); + return builder.append("}").toString(); + } +} diff --git a/src/main/java/xyz/etztech/embed/Embed.java b/src/main/java/xyz/etztech/embed/Embed.java new file mode 100644 index 0000000..583cf92 --- /dev/null +++ b/src/main/java/xyz/etztech/embed/Embed.java @@ -0,0 +1,127 @@ +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 static Embed builder() { + Embed embed = new Embed(); + embed.content = ""; + embed.title = ""; + embed.description = ""; + embed.url = ""; + embed.color = 0; + embed.timestamp = null; + embed.footer = null; + embed.thumbnail = null; + embed.image = null; + embed.author = null; + embed.fields = new ArrayList<>(); + return embed; + } + + 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("[%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(); + } +} diff --git a/src/main/java/xyz/etztech/embed/Field.java b/src/main/java/xyz/etztech/embed/Field.java new file mode 100644 index 0000000..503114b --- /dev/null +++ b/src/main/java/xyz/etztech/embed/Field.java @@ -0,0 +1,34 @@ +package xyz.etztech.embed; + +import xyz.etztech.Javacord; + +import java.util.ArrayList; +import java.util.List; + +public class Field { + private final String name; + private final String value; + private final boolean inline; + + public Field(String name, String value) { + this.name = name; + this.value = value; + this.inline = false; + } + + public Field(String name, String value, boolean inline) { + this.name = name; + this.value = value; + this.inline = inline; + } + + public String toJSON() { + StringBuilder builder = new StringBuilder("{"); + List json = new ArrayList<>(); + json.add(String.format("\"name\": \"%s\"", Javacord.escapeQuote(name))); + json.add(String.format(", \"value\": \"%s\"", Javacord.escapeQuote(value))); + if (inline) json.add(String.format(", \"inline\": %s", true)); + builder.append(String.join(",", json)); + return builder.append("}").toString(); + } +} diff --git a/src/main/java/xyz/etztech/embed/Footer.java b/src/main/java/xyz/etztech/embed/Footer.java new file mode 100644 index 0000000..5d472ea --- /dev/null +++ b/src/main/java/xyz/etztech/embed/Footer.java @@ -0,0 +1,35 @@ +package xyz.etztech.embed; + +import xyz.etztech.Javacord; + +import java.util.ArrayList; +import java.util.List; + +public class Footer { + private final String text; + private final String iconURL; + private final String proxyIconURL; + + public Footer(String text) { + this.text = text; + this.iconURL = ""; + this.proxyIconURL = ""; + } + + public Footer(String text, String iconURL, String proxyIconURL) { + this.text = text; + this.iconURL = iconURL; + this.proxyIconURL = proxyIconURL; + } + + + public String toJSON() { + StringBuilder builder = new StringBuilder("{"); + builder.append(String.format("\"text\": \"%s\"", Javacord.escapeQuote(text))); + List json = new ArrayList<>(); + if (!"".equals(iconURL)) json.add(String.format("\"icon_url\": \"%s\"", Javacord.escapeQuote(iconURL))); + if (!"".equals(proxyIconURL)) json.add(String.format("\"proxy_icon_url\": \"%s\"", Javacord.escapeQuote(proxyIconURL))); + builder.append(String.join(",", json)); + return builder.append("}").toString(); + } +} diff --git a/src/main/java/xyz/etztech/embed/Image.java b/src/main/java/xyz/etztech/embed/Image.java new file mode 100644 index 0000000..249c125 --- /dev/null +++ b/src/main/java/xyz/etztech/embed/Image.java @@ -0,0 +1,31 @@ +package xyz.etztech.embed; + +import xyz.etztech.Javacord; + +import java.util.ArrayList; +import java.util.List; + +public class Image { + private final String url; + private final String proxyURL; + private final int height; + private final int width; + + public Image(String url, String proxyURL, int height, int width) { + this.url = url; + this.proxyURL = proxyURL; + this.height = height; + this.width = width; + } + + public String toJSON() { + StringBuilder builder = new StringBuilder("{"); + List json = new ArrayList<>(); + if (!"".equals(url)) json.add(String.format("\"url\": \"%s\"", Javacord.escapeQuote(url))); + if (!"".equals(proxyURL)) json.add(String.format("\"proxy_url\": \"%s\"", Javacord.escapeQuote(proxyURL))); + if (height != 0) json.add(String.format("\"height\": \"%d\"", height)); + if (width != 0) json.add(String.format("\"width\": \"%d\"", width)); + builder.append(String.join(",", json)); + return builder.append("}").toString(); + } +} diff --git a/src/main/java/xyz/etztech/embed/Thumbnail.java b/src/main/java/xyz/etztech/embed/Thumbnail.java new file mode 100644 index 0000000..218a5d5 --- /dev/null +++ b/src/main/java/xyz/etztech/embed/Thumbnail.java @@ -0,0 +1,7 @@ +package xyz.etztech.embed; + +public class Thumbnail extends Image { + public Thumbnail(String url, String proxyURL, int height, int width) { + super(url, proxyURL, height, width); + } +} diff --git a/src/main/java/xyz/etztech/embed/Webhook.java b/src/main/java/xyz/etztech/embed/Webhook.java new file mode 100644 index 0000000..9da91ea --- /dev/null +++ b/src/main/java/xyz/etztech/embed/Webhook.java @@ -0,0 +1,27 @@ +package xyz.etztech.embed; + +import xyz.etztech.Javacord; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Webhook { + private final String content; + private final List embeds; + + public Webhook(String content, Embed... embeds) { + this.content = content; + this.embeds = Arrays.asList(embeds); + } + + public String toString() { + StringBuilder builder = new StringBuilder("{"); + List json = new ArrayList<>(); + if (!"".equals(content)) json.add(String.format("\"content\": \"%s\"", Javacord.escapeQuote(content))); + if (embeds.size() > 0) json.add(String.format("\"embeds\": [%s]", embeds.stream().map(Embed::toJSON).collect(Collectors.joining(",")))); + builder.append(String.join(",", json)); + return builder.append("}").toString(); + } +} diff --git a/src/test/java/Test.java b/src/test/java/Test.java new file mode 100644 index 0000000..bf4140c --- /dev/null +++ b/src/test/java/Test.java @@ -0,0 +1,15 @@ +import xyz.etztech.embed.Author; +import xyz.etztech.embed.Embed; +import xyz.etztech.embed.Webhook; + +public class Test { + + public static void main(String[] args) { + Embed embed = Embed.builder() + .color(3306460) + .description("Etzelia found some \"diamond\" ore") + .author(new Author("Etzelia", "", "https://minotar.net/helm/Etzelia/100.png", "")); + Webhook webhook = new Webhook("@here", embed); + System.out.println(webhook); + } +}