Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add very simple discord integration #5

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.apache.commons.lang3.tuple.Pair;

import com.kuba6000.ae2webintegration.discord.DiscordManager;
import com.kuba6000.ae2webintegration.mixins.AE2.CraftingCPUClusterAccessor;
import com.kuba6000.ae2webintegration.mixins.AE2.CraftingLinkAccessor;

Expand Down Expand Up @@ -241,13 +242,28 @@ public static void completeCrafting(ICraftingCPU cpu) {
info.isDone = true;
info.timeDone = System.currentTimeMillis();
trackingInfos.put(nextFreeTrackingInfoID++, info);
double took = info.timeDone - info.timeStarted;
took /= 1000d;
DiscordManager.postMessageNonBlocking(
new DiscordManager.DiscordEmbed(
"AE2 Job Tracker",
"Crafting for `" + info.finalOutput.getItemStack()
.getDisplayName()
+ " x"
+ info.finalOutput.getStackSize()
+ "` "
+ (info.wasCancelled ? "cancelled" : "completed")
+ "!\nIt took "
+ took
+ "s",
info.wasCancelled ? 15548997 : 5763719));
}

public static void cancelCrafting(ICraftingCPU cpu) {
JobTrackingInfo info = trackingInfoMap.get(cpu);
if (info == null) return;
completeCrafting(cpu);
info.wasCancelled = true;
completeCrafting(cpu);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kuba6000.ae2webintegration;

import com.kuba6000.ae2webintegration.commands.ReloadCommandHandler;
import com.kuba6000.ae2webintegration.discord.DiscordManager;
import com.kuba6000.ae2webintegration.utils.VersionChecker;

import cpw.mods.fml.common.FMLCommonHandler;
Expand Down Expand Up @@ -44,6 +45,9 @@ public void serverStarting(FMLServerStartingEvent event) {

public void serverStarted(FMLServerStartedEvent event) {
AE2Controller.init();
DiscordManager.init();
DiscordManager.postMessageNonBlocking(
new DiscordManager.DiscordEmbed("AE2 Web Integration", "Discord integration started!"));
}

public void serverStopping(FMLServerStoppingEvent event) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/kuba6000/ae2webintegration/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ public class Config {
.toString();
public static int AE_PORT = 2324;

// discord
public static String DISCORD_WEBHOOK = "";
public static String DISCORD_ROLE_ID = "";

public static void synchronizeConfiguration() {
Configuration configuration = new Configuration(configFile);
AE_PORT = configuration
.getInt("port", Configuration.CATEGORY_GENERAL, AE_PORT, 1, 65535, "Port for the hosted website");
AE_PASSWORD = configuration
.getString("password", Configuration.CATEGORY_GENERAL, AE_PASSWORD, "Password for the hosted website");

DISCORD_WEBHOOK = configuration
.getString("discord_webhook", "discord", "", "Discord webhook url (OPTIONAL, leave empty to ignore)");
DISCORD_ROLE_ID = configuration
.getString("discord_role_id", "discord", "", "Role to ping on message (OPTIONAL, leave empty to ignore)");

if (configuration.hasChanged()) {
configuration.save();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.kuba6000.ae2webintegration.discord;

import static com.kuba6000.ae2webintegration.AE2WebIntegration.MODID;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.concurrent.ConcurrentLinkedQueue;

import javax.net.ssl.HttpsURLConnection;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.kuba6000.ae2webintegration.Config;

public class DiscordManager extends Thread {

private static final Logger LOG = LogManager.getLogger(MODID + " - DISCORD INTEGRATION");

private static DiscordManager thread;

private static ConcurrentLinkedQueue<DiscordEmbed> toPush = new ConcurrentLinkedQueue<>();

public static void init() {
if (thread != null) return;
thread = new DiscordManager();
thread.start();
}

public static void postMessageNonBlocking(DiscordEmbed message) {
toPush.offer(message);
}

public static class DiscordEmbed {

String title;
String description;
int color;

public DiscordEmbed(String title, String description, int color) {
this.title = title;
this.description = description;
this.color = color;
}

public DiscordEmbed(String title, String description) {
this(title, description, 1752220);
}
}

private static void postMessage(DiscordEmbed message) {
if (Config.DISCORD_WEBHOOK.isEmpty()) return;

String roleID = Config.DISCORD_ROLE_ID;

JsonObject json = new JsonObject();
json.addProperty("username", "AE2 Web Integration");
json.addProperty("content", !roleID.isEmpty() ? "<@&" + roleID + ">" : "");
JsonArray embeds = new JsonArray();
JsonObject embed = new JsonObject();
embed.addProperty("title", message.title);
embed.addProperty("description", message.description);
embed.addProperty("color", message.color);
embeds.add(embed);
json.add("embeds", embeds);
json.add("attachments", new JsonArray());

URL url = null;
try {
url = new URL(Config.DISCORD_WEBHOOK);

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("User-Agent", "AE2-Web-Integration");
connection.setDoOutput(true);
connection.setRequestMethod("POST");

OutputStream stream = connection.getOutputStream();
stream.write(
json.toString()
.getBytes());
stream.flush();
stream.close();

int code;
if ((code = connection.getResponseCode()) != 200) {
LOG.error("Error, response code: {}", code);
}
} catch (IOException e) {
// throw new RuntimeException(e);
}
}

@Override
public void run() {
while (true) {
if (toPush.peek() != null) {
DiscordEmbed message;
while ((message = toPush.poll()) != null) {
postMessage(message);
}
}

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// throw new RuntimeException(e);
}
}
}
}
Loading