mirror of
https://github.com/HMCore/Orbot.git
synced 2025-12-10 21:06:18 +00:00
Change Webhook
This commit is contained in:
50
.idea/workspace.xml
generated
50
.idea/workspace.xml
generated
@@ -4,9 +4,17 @@
|
||||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="1aabf22b-2f57-46ac-9973-367d8668ffd3" name="Default Changelist" comment="Add more tests">
|
||||
<change afterPath="$PROJECT_DIR$/src/test/kotlin/org/hmcore/extensions/FileTest.kt" afterDir="false" />
|
||||
<list default="true" id="1aabf22b-2f57-46ac-9973-367d8668ffd3" name="Default Changelist" comment="Add file tests">
|
||||
<change afterPath="$PROJECT_DIR$/src/main/kotlin/org/hmcore/extensions/Message.kt" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/test/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/org/hmcore/DiscordWebhook.java" beforeDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/org/hmcore/TwitterJob.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/hmcore/TwitterJob.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/kotlin/org/hmcore/Channels.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/org/hmcore/Channels.kt" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/kotlin/org/hmcore/extensions/Color.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/org/hmcore/extensions/Color.kt" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/kotlin/org/hmcore/webhook/WebhookCaller.kt" beforeDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/test/kotlin/org/hmcore/extensions/ColorTest.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/kotlin/org/hmcore/extensions/ColorTest.kt" afterDir="false" />
|
||||
</list>
|
||||
<list id="44283a45-f406-407f-bce2-a31bb9bfc0cc" name="Changes by Valentin" comment="Changes by Valentin" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
@@ -178,7 +186,7 @@
|
||||
<recent name="org" />
|
||||
</key>
|
||||
</component>
|
||||
<component name="RunManager" selected="JUnit.All Tests">
|
||||
<component name="RunManager" selected="Gradle.DiscordWebhookEmbed.Webhook should not throw and return false if supplied invalid URL">
|
||||
<configuration default="true" type="ArquillianJUnit" factoryName="" nameIsGenerated="true">
|
||||
<option name="arquillianRunConfiguration">
|
||||
<value>
|
||||
@@ -211,6 +219,27 @@
|
||||
<DebugAllEnabled>false</DebugAllEnabled>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="DiscordWebhookEmbed.Webhook should not throw and return false if supplied invalid URL" type="GradleRunConfiguration" factoryName="Gradle" temporary="true">
|
||||
<ExternalSystemSettings>
|
||||
<option name="executionName" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="externalSystemIdString" value="GRADLE" />
|
||||
<option name="scriptParameters" value="--tests "org.hmcore.webhook.DiscordWebhookEmbed.Webhook should not throw and return false if supplied invalid URL"" />
|
||||
<option name="taskDescriptions">
|
||||
<list />
|
||||
</option>
|
||||
<option name="taskNames">
|
||||
<list>
|
||||
<option value=":test" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="vmOptions" />
|
||||
</ExternalSystemSettings>
|
||||
<ExternalSystemDebugServerProcess>false</ExternalSystemDebugServerProcess>
|
||||
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
|
||||
<DebugAllEnabled>false</DebugAllEnabled>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="EmbedTest" type="GradleRunConfiguration" factoryName="Gradle" temporary="true">
|
||||
<ExternalSystemSettings>
|
||||
<option name="executionName" />
|
||||
@@ -284,28 +313,21 @@
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
<configuration name="Main" type="JetRunConfigurationType" temporary="true" nameIsGenerated="true">
|
||||
<option name="MAIN_CLASS_NAME" value="org.wulkanat.Main" />
|
||||
<module name="HytaleUpdateBot.main" />
|
||||
<method v="2">
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
<list>
|
||||
<item itemvalue="Gradle.ColorTest" />
|
||||
<item itemvalue="Gradle.EmbedTest" />
|
||||
<item itemvalue="JUnit.All Tests" />
|
||||
<item itemvalue="Kotlin.Main" />
|
||||
<item itemvalue="Gradle.FileTest" />
|
||||
<item itemvalue="Gradle.FileTest.Ensure exists should create a file if supplied with null but not write text" />
|
||||
<item itemvalue="JUnit.All Tests" />
|
||||
<item itemvalue="Gradle.DiscordWebhookEmbed.Webhook should not throw and return false if supplied invalid URL" />
|
||||
</list>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Gradle.DiscordWebhookEmbed.Webhook should not throw and return false if supplied invalid URL" />
|
||||
<item itemvalue="Gradle.FileTest.Ensure exists should create a file if supplied with null but not write text" />
|
||||
<item itemvalue="Gradle.FileTest" />
|
||||
<item itemvalue="Gradle.EmbedTest" />
|
||||
<item itemvalue="Gradle.ColorTest" />
|
||||
<item itemvalue="Kotlin.Main" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
@@ -485,7 +507,7 @@
|
||||
</watches-manager>
|
||||
</component>
|
||||
<component name="com.intellij.coverage.CoverageDataManagerImpl">
|
||||
<SUITE FILE_PATH="coverage/HytaleUpdateBot$All_Tests.ic" NAME="All Tests Coverage Results" MODIFIED="1622283817250" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="idea" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" />
|
||||
<SUITE FILE_PATH="coverage/HytaleUpdateBot$All_Tests.ic" NAME="All Tests Coverage Results" MODIFIED="1622286395486" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="idea" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" />
|
||||
</component>
|
||||
<component name="se.expertsystem.intellij.aop.aspectj.AspectJProjectComponent">
|
||||
<setting name="isActivated" value="false" />
|
||||
|
||||
@@ -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<EmbedObject> 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<JSONObject> 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<EmbedObject.Field> 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<JSONObject> 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<Field> 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<Field> 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<String, Object> 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<Map.Entry<String, Object>> entrySet = map.entrySet();
|
||||
builder.append("{");
|
||||
|
||||
int i = 0;
|
||||
for (Map.Entry<String, Object> 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 + "\"";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<ServiceChannel> = refreshServiceChannelsFromDisk()
|
||||
|
||||
fun sentToAll(messageEmbed: Message) {
|
||||
WebhookCaller.sendToGuildedNews(messageEmbed)
|
||||
messageEmbed.toWebhook().send(WEBHOOKS.blogPostsWebhookUrl)
|
||||
|
||||
Main.jdas.forEach { jda ->
|
||||
for (channel_pair in channels) {
|
||||
try {
|
||||
|
||||
@@ -8,4 +8,7 @@ fun hex2Rgb(colorStr: String): Color {
|
||||
Integer.valueOf(colorStr.substring(3, 5), 16),
|
||||
Integer.valueOf(colorStr.substring(5, 7), 16)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun Color.toRgb() =
|
||||
(((red shl 8) + green) shl 8) + blue
|
||||
44
src/main/kotlin/org/hmcore/extensions/Message.kt
Normal file
44
src/main/kotlin/org/hmcore/extensions/Message.kt
Normal file
@@ -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
|
||||
}
|
||||
92
src/main/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt
Normal file
92
src/main/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt
Normal file
@@ -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<Field> = mutableListOf(),
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class DiscordWebhook(
|
||||
var content: String? = null,
|
||||
var username: String? = null,
|
||||
var avatarUrl: String? = null,
|
||||
var tts: Boolean = false,
|
||||
var embeds: MutableList<DiscordWebhookEmbed> = 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
30
src/test/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt
Normal file
30
src/test/kotlin/org/hmcore/webhook/DiscordWebhookEmbed.kt
Normal file
@@ -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"
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user