diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f9fc60c..4f6a1e4 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,9 +4,17 @@
-
-
+
+
+
+
+
+
+
+
+
+
@@ -178,7 +186,7 @@
-
+
+
+
+
+
+
+
+
+
+
+
+ false
+ true
+ false
+
+
@@ -284,28 +313,21 @@
-
-
-
-
-
-
-
-
-
+
+
+
-
@@ -485,7 +507,7 @@
-
+
diff --git a/src/main/java/org/hmcore/DiscordWebhook.java b/src/main/java/org/hmcore/DiscordWebhook.java
deleted file mode 100644
index 3c57dd8..0000000
--- a/src/main/java/org/hmcore/DiscordWebhook.java
+++ /dev/null
@@ -1,391 +0,0 @@
-package org.hmcore;
-
-import javax.net.ssl.HttpsURLConnection;
-import java.awt.Color;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.lang.reflect.Array;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Class used to execute Discord Webhooks with low effort
- */
-public class DiscordWebhook {
-
- private final String url;
- private String content;
- private String username;
- private String avatarUrl;
- private boolean tts;
- private final List embeds = new ArrayList<>();
-
- /**
- * Constructs a new DiscordWebhook instance
- *
- * @param url The webhook URL obtained in Discord
- */
- public DiscordWebhook(String url) {
- this.url = url;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public void setAvatarUrl(String avatarUrl) {
- this.avatarUrl = avatarUrl;
- }
-
- public void setTts(boolean tts) {
- this.tts = tts;
- }
-
- public void addEmbed(EmbedObject embed) {
- this.embeds.add(embed);
- }
-
- public void execute() throws IOException {
- if (this.content == null && this.embeds.isEmpty()) {
- throw new IllegalArgumentException("Set content or add at least one EmbedObject");
- }
-
- JSONObject json = new JSONObject();
-
- json.put("content", this.content);
- json.put("username", this.username);
- json.put("avatar_url", this.avatarUrl);
- json.put("tts", this.tts);
-
- if (!this.embeds.isEmpty()) {
- List embedObjects = new ArrayList<>();
-
- for (EmbedObject embed : this.embeds) {
- JSONObject jsonEmbed = new JSONObject();
-
- jsonEmbed.put("title", embed.getTitle());
- jsonEmbed.put("description", embed.getDescription());
- jsonEmbed.put("url", embed.getUrl());
-
- if (embed.getColor() != null) {
- Color color = embed.getColor();
- int rgb = color.getRed();
- rgb = (rgb << 8) + color.getGreen();
- rgb = (rgb << 8) + color.getBlue();
-
- jsonEmbed.put("color", rgb);
- }
-
- EmbedObject.Footer footer = embed.getFooter();
- EmbedObject.Image image = embed.getImage();
- EmbedObject.Thumbnail thumbnail = embed.getThumbnail();
- EmbedObject.Author author = embed.getAuthor();
- List fields = embed.getFields();
-
- if (footer != null) {
- JSONObject jsonFooter = new JSONObject();
-
- jsonFooter.put("text", footer.getText());
- jsonFooter.put("icon_url", footer.getIconUrl());
- jsonEmbed.put("footer", jsonFooter);
- }
-
- if (image != null) {
- JSONObject jsonImage = new JSONObject();
-
- jsonImage.put("url", image.getUrl());
- jsonEmbed.put("image", jsonImage);
- }
-
- if (thumbnail != null) {
- JSONObject jsonThumbnail = new JSONObject();
-
- jsonThumbnail.put("url", thumbnail.getUrl());
- jsonEmbed.put("thumbnail", jsonThumbnail);
- }
-
- if (author != null) {
- JSONObject jsonAuthor = new JSONObject();
-
- jsonAuthor.put("name", author.getName());
- jsonAuthor.put("url", author.getUrl());
- jsonAuthor.put("icon_url", author.getIconUrl());
- jsonEmbed.put("author", jsonAuthor);
- }
-
- List jsonFields = new ArrayList<>();
- for (EmbedObject.Field field : fields) {
- JSONObject jsonField = new JSONObject();
-
- jsonField.put("name", field.getName());
- jsonField.put("value", field.getValue());
- jsonField.put("inline", field.isInline());
-
- jsonFields.add(jsonField);
- }
-
- jsonEmbed.put("fields", jsonFields.toArray());
- embedObjects.add(jsonEmbed);
- }
-
- json.put("embeds", embedObjects.toArray());
- }
-
- URL url = new URL(this.url);
- HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
- connection.addRequestProperty("Content-Type", "application/json");
- connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_");
- connection.setDoOutput(true);
- connection.setRequestMethod("POST");
-
- OutputStream stream = connection.getOutputStream();
- stream.write(json.toString().getBytes());
- stream.flush();
- stream.close();
-
- connection.getInputStream().close(); //I'm not sure why but it doesn't work without getting the InputStream
- connection.disconnect();
- }
-
- public static class EmbedObject {
- private String title;
- private String description;
- private String url;
- private Color color;
-
- private Footer footer;
- private Thumbnail thumbnail;
- private Image image;
- private Author author;
- private final List fields = new ArrayList<>();
-
- public String getTitle() {
- return title;
- }
-
- public String getDescription() {
- return description;
- }
-
- public String getUrl() {
- return url;
- }
-
- public Color getColor() {
- return color;
- }
-
- public Footer getFooter() {
- return footer;
- }
-
- public Thumbnail getThumbnail() {
- return thumbnail;
- }
-
- public Image getImage() {
- return image;
- }
-
- public Author getAuthor() {
- return author;
- }
-
- public List getFields() {
- return fields;
- }
-
- public EmbedObject setTitle(String title) {
- this.title = title;
- return this;
- }
-
- public EmbedObject setDescription(String description) {
- this.description = description;
- return this;
- }
-
- public EmbedObject setUrl(String url) {
- this.url = url;
- return this;
- }
-
- public EmbedObject setColor(Color color) {
- this.color = color;
- return this;
- }
-
- public EmbedObject setFooter(String text, String icon) {
- this.footer = new Footer(text, icon);
- return this;
- }
-
- public EmbedObject setThumbnail(String url) {
- this.thumbnail = new Thumbnail(url);
- return this;
- }
-
- public EmbedObject setImage(String url) {
- this.image = new Image(url);
- return this;
- }
-
- public EmbedObject setAuthor(String name, String url, String icon) {
- this.author = new Author(name, url, icon);
- return this;
- }
-
- public EmbedObject addField(String name, String value, boolean inline) {
- this.fields.add(new Field(name, value, inline));
- return this;
- }
-
- private static class Footer {
- private final String text;
- private final String iconUrl;
-
- private Footer(String text, String iconUrl) {
- this.text = text;
- this.iconUrl = iconUrl;
- }
-
- private String getText() {
- return text;
- }
-
- private String getIconUrl() {
- return iconUrl;
- }
- }
-
- private static class Thumbnail {
- private final String url;
-
- private Thumbnail(String url) {
- this.url = url;
- }
-
- private String getUrl() {
- return url;
- }
- }
-
- private static class Image {
- private final String url;
-
- private Image(String url) {
- this.url = url;
- }
-
- private String getUrl() {
- return url;
- }
- }
-
- private static class Author {
- private final String name;
- private final String url;
- private final String iconUrl;
-
- private Author(String name, String url, String iconUrl) {
- this.name = name;
- this.url = url;
- this.iconUrl = iconUrl;
- }
-
- private String getName() {
- return name;
- }
-
- private String getUrl() {
- return url;
- }
-
- private String getIconUrl() {
- return iconUrl;
- }
- }
-
- private static class Field {
- private final String name;
- private final String value;
- private final boolean inline;
-
- private Field(String name, String value, boolean inline) {
- this.name = name;
- this.value = value;
- this.inline = inline;
- }
-
- private String getName() {
- return name;
- }
-
- private String getValue() {
- return value;
- }
-
- private boolean isInline() {
- return inline;
- }
- }
- }
-
- private static class JSONObject {
-
- private final HashMap map = new HashMap<>();
-
- void put(String key, Object value) {
- if (value != null) {
- map.put(key, value);
- }
- }
-
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- Set> entrySet = map.entrySet();
- builder.append("{");
-
- int i = 0;
- for (Map.Entry entry : entrySet) {
- Object val = entry.getValue();
- builder.append(quote(entry.getKey())).append(":");
-
- if (val instanceof String) {
- builder.append(quote(String.valueOf(val)));
- } else if (val instanceof Integer) {
- builder.append(Integer.valueOf(String.valueOf(val)));
- } else if (val instanceof Boolean) {
- builder.append(val);
- } else if (val instanceof JSONObject) {
- builder.append(val);
- } else if (val.getClass().isArray()) {
- builder.append("[");
- int len = Array.getLength(val);
- for (int j = 0; j < len; j++) {
- builder.append(Array.get(val, j).toString()).append(j != len - 1 ? "," : "");
- }
- builder.append("]");
- }
-
- builder.append(++i == entrySet.size() ? "}" : ",");
- }
-
- return builder.toString();
- }
-
- private String quote(String string) {
- return "\"" + string + "\"";
- }
- }
-
-}
\ No newline at end of file
diff --git a/src/main/java/org/hmcore/TwitterJob.java b/src/main/java/org/hmcore/TwitterJob.java
index fba04d2..421ec04 100644
--- a/src/main/java/org/hmcore/TwitterJob.java
+++ b/src/main/java/org/hmcore/TwitterJob.java
@@ -6,12 +6,10 @@ import com.github.redouane59.twitter.signature.TwitterCredentials;
import net.dv8tion.jda.api.MessageBuilder;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
-import org.quartz.JobExecutionException;
import java.util.Objects;
public class TwitterJob implements Job {
-
public static TwitterClient twitterClient = new TwitterClient(TwitterCredentials.builder()
.accessToken(Objects.requireNonNull(Admin.adFile.getTwitterApi()).getAccessToken())
.accessTokenSecret(Objects.requireNonNull(Admin.adFile.getTwitterApi()).getAccessTokenSecret())
@@ -24,24 +22,19 @@ public class TwitterJob implements Job {
public static String lastTweetID = twitterClient.getUserTimeline(hytaleTwitterID, 20).get(0).getId();
@Override
- public void execute(JobExecutionContext context) throws JobExecutionException {
-
+ public void execute(JobExecutionContext context) {
try {
-
Tweet tweet = twitterClient.getUserTimeline(hytaleTwitterID, 20).get(0);
String tweetID = tweet.getId();
- if(!lastTweetID.equalsIgnoreCase(tweetID)) {
+ if (!lastTweetID.equalsIgnoreCase(tweetID)) {
lastTweetID = tweetID;
Channels.INSTANCE.sentToAll(new MessageBuilder().append("https://twitter.com/Hytale/status/").append(tweetID).build());
-
}
} catch (Exception e) {
e.printStackTrace();
}
-
}
-
}
diff --git a/src/main/kotlin/org/hmcore/Channels.kt b/src/main/kotlin/org/hmcore/Channels.kt
index 99946dc..cc77fc2 100644
--- a/src/main/kotlin/org/hmcore/Channels.kt
+++ b/src/main/kotlin/org/hmcore/Channels.kt
@@ -2,17 +2,16 @@
package org.hmcore
-import org.hmcore.webhook.WebhookCaller
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.entities.Message
+import org.hmcore.extensions.toWebhook
import java.awt.Color
object Channels {
-
/**
* List of (ServerID, ChannelID)
*/
@@ -20,7 +19,8 @@ object Channels {
var serviceChannels: MutableList = refreshServiceChannelsFromDisk()
fun sentToAll(messageEmbed: Message) {
- WebhookCaller.sendToGuildedNews(messageEmbed)
+ messageEmbed.toWebhook().send(WEBHOOKS.blogPostsWebhookUrl)
+
Main.jdas.forEach { jda ->
for (channel_pair in channels) {
try {
diff --git a/src/main/kotlin/org/hmcore/extensions/Color.kt b/src/main/kotlin/org/hmcore/extensions/Color.kt
index ce7b474..01fe7b2 100644
--- a/src/main/kotlin/org/hmcore/extensions/Color.kt
+++ b/src/main/kotlin/org/hmcore/extensions/Color.kt
@@ -8,4 +8,7 @@ fun hex2Rgb(colorStr: String): Color {
Integer.valueOf(colorStr.substring(3, 5), 16),
Integer.valueOf(colorStr.substring(5, 7), 16)
)
-}
\ No newline at end of file
+}
+
+fun Color.toRgb() =
+ (((red shl 8) + green) shl 8) + blue
\ No newline at end of file
diff --git a/src/main/kotlin/org/hmcore/extensions/Message.kt b/src/main/kotlin/org/hmcore/extensions/Message.kt
new file mode 100644
index 0000000..b05026f
--- /dev/null
+++ b/src/main/kotlin/org/hmcore/extensions/Message.kt
@@ -0,0 +1,44 @@
+package org.hmcore.extensions
+
+import net.dv8tion.jda.api.entities.Message
+import org.hmcore.WEBHOOKS
+import org.hmcore.webhook.*
+
+fun Message.toWebhook(): DiscordWebhook {
+ val webhook = DiscordWebhook()
+ webhook.content = contentRaw
+ webhook.tts = false
+
+ embeds.forEach { embed ->
+ webhook.embeds.add(DiscordWebhookEmbed().apply {
+ title = embed.title
+ description = embed.description
+ color = embed.color?.toRgb()
+ image = Image(embed.image?.url)
+ url = embed.url
+
+ thumbnail = Thumbnail(embed.thumbnail?.url)
+ author = Author(
+ embed.author?.name,
+ embed.author?.url,
+ embed.author?.iconUrl
+ )
+ footer = Footer(
+ embed.footer?.text,
+ embed.footer?.iconUrl,
+ )
+
+ for (field in embed.fields) {
+ fields.add(
+ Field(
+ field.name,
+ field.value,
+ field.isInline,
+ )
+ )
+ }
+ })
+ }
+
+ return webhook
+}
\ No newline at end of file
diff --git a/src/main/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt b/src/main/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt
new file mode 100644
index 0000000..a14fb6c
--- /dev/null
+++ b/src/main/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt
@@ -0,0 +1,92 @@
+package org.hmcore.webhook
+
+import kotlinx.serialization.SerialName
+import kotlinx.serialization.Serializable
+import kotlinx.serialization.encodeToString
+import kotlinx.serialization.json.Json
+import java.io.IOException
+import java.io.OutputStream
+import java.net.URL
+import javax.net.ssl.HttpsURLConnection
+
+@Serializable
+data class Footer(
+ val text: String? = null,
+ @SerialName("icon_url")
+ val iconUrl: String? = null,
+)
+
+@Serializable
+data class Thumbnail(
+ val url: String? = null,
+)
+
+@Serializable
+data class Image(
+ val url: String? = null,
+)
+
+@Serializable
+data class Author(
+ val name: String? = null,
+ val url: String? = null,
+ @SerialName("icon_url")
+ val iconUrl: String? = null,
+)
+
+@Serializable
+data class Field(
+ val name: String? = null,
+ val value: String? = null,
+ val inline: Boolean? = null,
+)
+
+@Serializable
+data class DiscordWebhookEmbed(
+ var title: String? = null,
+ var description: String? = null,
+ var url: String? = null,
+ var color: Int? = null,
+
+ var footer: Footer? = null,
+ var thumbnail: Thumbnail? = null,
+ var image: Image? = null,
+ var author: Author? = null,
+ var fields: MutableList = mutableListOf(),
+)
+
+@Serializable
+data class DiscordWebhook(
+ var content: String? = null,
+ var username: String? = null,
+ var avatarUrl: String? = null,
+ var tts: Boolean = false,
+ var embeds: MutableList = mutableListOf(),
+) {
+ fun send(url: String): Boolean {
+ var connection: HttpsURLConnection? = null
+ var stream: OutputStream? = null
+
+ return try {
+ connection = URL(url).openConnection() as HttpsURLConnection
+ connection.addRequestProperty("Content-Type", "application/json")
+ connection.addRequestProperty("User-Agent", "Kotlin-DiscordWebhook")
+ connection.doOutput = true
+ connection.requestMethod = "POST"
+
+ stream = connection.outputStream
+ stream.write(Json.encodeToString(this).toByteArray())
+ stream.flush()
+
+ true
+ } catch (e: IOException) {
+ e.printStackTrace()
+
+ false
+ } finally {
+ stream?.close()
+ connection?.inputStream?.close()
+ connection?.disconnect()
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/kotlin/org/hmcore/webhook/WebhookCaller.kt b/src/main/kotlin/org/hmcore/webhook/WebhookCaller.kt
deleted file mode 100644
index 5faa7f0..0000000
--- a/src/main/kotlin/org/hmcore/webhook/WebhookCaller.kt
+++ /dev/null
@@ -1,38 +0,0 @@
-package org.hmcore.webhook
-
-import org.hmcore.WEBHOOKS
-import net.dv8tion.jda.api.entities.Message
-import org.hmcore.DiscordWebhook
-import org.hmcore.DiscordWebhook.EmbedObject
-import java.io.IOException
-
-object WebhookCaller {
- fun sendToGuildedNews(message: Message) {
- val webhook = DiscordWebhook(WEBHOOKS.blogPostsWebhookUrl)
- webhook.setContent(message.contentRaw)
- webhook.setTts(false)
-
- message.embeds.forEach { embed ->
- webhook.addEmbed(EmbedObject().apply {
- setAuthor(embed.author?.name, embed.author?.url, embed.author?.iconUrl)
- color = embed.color
- description = embed.description
- setFooter(embed.footer?.text, embed.footer?.iconUrl)
- title = embed.title
- setImage(embed.image?.url)
- setThumbnail(embed.thumbnail?.url)
- url = embed.url
-
- for (field in embed.fields) {
- addField(field.name, field.value, field.isInline)
- }
- })
- }
-
- try {
- webhook.execute()
- } catch (e: IOException) {
- e.printStackTrace()
- }
- }
-}
\ No newline at end of file
diff --git a/src/test/kotlin/org/hmcore/extensions/ColorTest.kt b/src/test/kotlin/org/hmcore/extensions/ColorTest.kt
index 97505f8..476316d 100644
--- a/src/test/kotlin/org/hmcore/extensions/ColorTest.kt
+++ b/src/test/kotlin/org/hmcore/extensions/ColorTest.kt
@@ -13,4 +13,13 @@ class ColorTest {
assertEquals(hex2Rgb("#00FF00"), Color.GREEN)
assertEquals(hex2Rgb("#0000FF"), Color.BLUE)
}
+
+ @Test
+ fun `color should convert to RGB correctly`() {
+ assertEquals(16777215, Color.WHITE.toRgb())
+ assertEquals(0, Color.BLACK.toRgb())
+ assertEquals(16711680, Color.RED.toRgb())
+ assertEquals(65280, Color.GREEN.toRgb())
+ assertEquals(255, Color.BLUE.toRgb())
+ }
}
\ No newline at end of file
diff --git a/src/test/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt b/src/test/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt
new file mode 100644
index 0000000..f594cf2
--- /dev/null
+++ b/src/test/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt
@@ -0,0 +1,30 @@
+package org.hmcore.webhook
+
+import org.junit.Test
+import kotlin.test.assertEquals
+
+class DiscordWebhookEmbed {
+ /*@Test TODO: Test against JSON Schema or something
+ fun `Webhook class should comply with Discord specification`() {
+ val exampleWebhook = """
+
+ """.trimIndent()
+ }*/
+
+ @Test
+ fun `Webhook should not throw and return false if supplied invalid URL`() {
+ assertEquals(false, DiscordWebhook().send("not a valid url"))
+ }
+
+ @Test
+ fun `Webhook should return false if connection throws`() {
+
+ }
+
+ @Test
+ fun `Webhook should send correctly`() {
+ DiscordWebhook(
+ content = "Test"
+ )
+ }
+}
\ No newline at end of file