Initial Commit

Signed-off-by: Etzelia <etzelia@hotmail.com>
pull/1/head
Etzelia 2020-07-30 16:25:24 -05:00
commit d1ad28c5df
No known key found for this signature in database
GPG Key ID: 3CAEB74806C4ADE5
13 changed files with 371 additions and 0 deletions

4
.gitignore vendored 100644
View File

@ -0,0 +1,4 @@
# IntelliJ
.idea/
*.iml
target/

7
LICENSE 100644
View File

@ -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.

7
README.md 100644
View File

@ -0,0 +1,7 @@
# Javacord
A small, no-dependency Discord utility library. Currently used for webhooks/embeds.
## License
[MIT](LICENSE``)

31
pom.xml 100644
View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.etztech</groupId>
<artifactId>javacord</artifactId>
<packaging>jar</packaging>
<version>0.0.1</version>
<developers>
<developer>
<name>Etzelia</name>
<email>etzelia@hotmail.com</email>
</developer>
</developers>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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("\"", "\\\\\"");
}
}

View File

@ -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<String> 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();
}
}

View File

@ -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<Field> 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<Field> 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<String> 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<String> 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();
}
}

View File

@ -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<String> 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();
}
}

View File

@ -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<String> 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();
}
}

View File

@ -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<String> 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();
}
}

View File

@ -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);
}
}

View File

@ -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<Embed> embeds;
public Webhook(String content, Embed... embeds) {
this.content = content;
this.embeds = Arrays.asList(embeds);
}
public String toString() {
StringBuilder builder = new StringBuilder("{");
List<String> 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();
}
}

View File

@ -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);
}
}