Skip to content
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ loader_version=0.16.10
tserver_mcver=1.21.1

# Mod Properties
mod_version=1.1.0+fabric.1.21.1
mod_version=1.1.1+fabric.1.21.1
maven_group=com.github.pinmacaroon.dchook
archives_base_name=dchook

Expand Down
24 changes: 19 additions & 5 deletions src/main/java/com/github/pinmacaroon/dchook/Hook.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ public class Hook implements DedicatedServerModInitializer {
"^https:\\/\\/(ptb\\.|canary\\.)?discord\\.com\\/api\\/webhooks\\/\\d+\\/.+$"
);

public static URI WEBHOOK_URI;

public static volatile Bot BOT;
public static boolean BOT_ENABLED = false;

private boolean IS_THREAD = false;
private static MinecraftServer MINECRAFT_SERVER;

public static MinecraftServer getGameServer() {
Expand All @@ -73,11 +76,18 @@ public void onInitializeServer() {

if(ModConfigs.FUNCTIONS_BOT_ENABLED) bottedStart();
else botlessStart();

try {

if(ModConfigs.IS_THREAD) {
IS_THREAD = true;
WEBHOOK_URI = URI.create(ModConfigs.WEBHOOK_URL + "?thread_id=" + ModConfigs.THREAD_ID);
} else {
WEBHOOK_URI = URI.create(ModConfigs.WEBHOOK_URL);
}

try {
HttpRequest get_webhook = HttpRequest.newBuilder()
.GET()
.uri(URI.create(ModConfigs.WEBHOOK_URL))
.uri(WEBHOOK_URI)
.build();

HttpResponse<String> response = HTTPCLIENT.send(get_webhook, HttpResponse.BodyHandlers.ofString());
Expand All @@ -96,7 +106,11 @@ public void onInitializeServer() {
Thread.onSpinWait();
}
BOT.setGUILD_ID(body.get("guild_id").getAsLong());
BOT.setCHANNEL_ID(body.get("channel_id").getAsLong());
if (IS_THREAD) {
BOT.setCHANNEL_ID(Long.parseLong(ModConfigs.THREAD_ID));
} else {
BOT.setCHANNEL_ID(body.get("channel_id").getAsLong());
}
});
bot_rutime_thread.start();
}
Expand Down Expand Up @@ -129,4 +143,4 @@ private void bottedStart(){
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class ModConfigs {
public static SimpleConfig CONFIG;
public static String WEBHOOK_URL;
public static String THREAD_ID;
public static String MESSAGES_SERVER_STARTING;
public static String MESSAGES_SERVER_STOPPED;
public static String MESSAGES_SERVER_STARTED;
Expand All @@ -16,6 +17,7 @@ public class ModConfigs {
public static String MESSAGES_BOT_LIST;
public static String MESSAGES_BOT_MODS_LIST;
public static String MESSAGES_BOT_MODS_NONE;
public static boolean IS_THREAD;
public static boolean FUNCTIONS_ALLOWOOCMESSAGES;
public static boolean MESSAGES_SERVER_STARTING_ALLOWED;
public static boolean MESSAGES_SERVER_STOPPED_ALLOWED;
Expand Down Expand Up @@ -52,6 +54,10 @@ private static void createConfigs() {

configs.addDocumentationLine("Configure Discord connection related parameters:");
configs.addKeyValuePair(new Pair<>("webhook.url", "https://discord.com/api/webhooks/000/ABCDEF"), "url of webhook");
configs.addDocumentationLine("Configure if webhook points to a thread");
configs.addKeyValuePair(new Pair<>("webhook.thread", false), "is channel a thread?");
configs.addDocumentationLine("Configure Thread ID if true. DON'T ADD ?thread_id PARAMETER TO THE WEBHOOK AS THE MOD AUTOMATICALLY INSERTS IT!");
configs.addKeyValuePair(new Pair<>("webhook.thread.id", "01234567890123456789"), "id of thread");
configs.addBlankLine();

configs.addDocumentationLine("Configure messages sent:");
Expand All @@ -78,6 +84,8 @@ private static void createConfigs() {

private static void assignConfigs() {
WEBHOOK_URL = CONFIG.getOrDefault("webhook.url", "");
IS_THREAD = CONFIG.getOrDefault("webhook.thread", false);
THREAD_ID = CONFIG.getOrDefault("webhook.thread.id", "");
MESSAGES_SERVER_STARTING = CONFIG.getOrDefault("messages.server.starting", "messages.server.starting");
MESSAGES_SERVER_STARTED = CONFIG.getOrDefault("messages.server.started", "messages.server.started");
MESSAGES_SERVER_STOPPED = CONFIG.getOrDefault("messages.server.stopped", "messages.server.stopped");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.net.http.HttpResponse;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.ArrayList;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, this is part of another PR

import java.util.concurrent.ExecutionException;

public class EventListeners {
Expand All @@ -28,7 +29,7 @@ public static void registerEventListeners(){

HttpRequest post = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(Hook.GSON.toJson(request_body)))
.uri(URI.create(ModConfigs.WEBHOOK_URL))
.uri(Hook.WEBHOOK_URI)
.header("Content-Type", "application/json")
.build();

Expand All @@ -48,7 +49,7 @@ public static void registerEventListeners(){

HttpRequest post = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(Hook.GSON.toJson(request_body)))
.uri(URI.create(ModConfigs.WEBHOOK_URL))
.uri(Hook.WEBHOOK_URI)
.header("Content-Type", "application/json")
.build();

Expand All @@ -60,7 +61,7 @@ public static void registerEventListeners(){
}

if (ModConfigs.FUNCTIONS_PROMOTIONS_ENABLED) {
PromotionProvider.sendPromotion(URI.create(ModConfigs.WEBHOOK_URL));
PromotionProvider.sendPromotion(Hook.WEBHOOK_URI);
}
});

Expand All @@ -72,7 +73,7 @@ public static void registerEventListeners(){

HttpRequest post = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(Hook.GSON.toJson(request_body)))
.uri(URI.create(ModConfigs.WEBHOOK_URL))
.uri(Hook.WEBHOOK_URI)
.header("Content-Type", "application/json")
.build();

Expand All @@ -94,7 +95,7 @@ public static void registerEventListeners(){

HttpRequest post = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(Hook.GSON.toJson(request_body)))
.uri(URI.create(ModConfigs.WEBHOOK_URL))
.uri(Hook.WEBHOOK_URI)
.header("Content-Type", "application/json")
.build();

Expand Down Expand Up @@ -126,7 +127,7 @@ public static void registerEventListeners(){

HttpRequest post = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(Hook.GSON.toJson(request_body)))
.uri(URI.create(ModConfigs.WEBHOOK_URL))
.uri(Hook.WEBHOOK_URI)
.header("Content-Type", "application/json")
.build();

Expand All @@ -146,7 +147,7 @@ public static void registerEventListeners(){

HttpRequest post = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(Hook.GSON.toJson(request_body)))
.uri(URI.create(ModConfigs.WEBHOOK_URL))
.uri(Hook.WEBHOOK_URI)
.header("Content-Type", "application/json")
.build();

Expand Down
Loading