use single configuration file for webhooks

This commit is contained in:
mangorifo
2023-04-19 00:33:46 +08:00
parent 7f74f5e2f3
commit 1e4410b70f
3 changed files with 22 additions and 22 deletions
+20 -20
View File
@@ -29,8 +29,7 @@ import java.util.Objects;
public class Main extends JavaPlugin {
public static File isLoggingConfig;
public static File notifyWebhookConfig;
public static File pubMessageWebhookConfig;
public static File webhookConfig;
private static boolean webhooksAreValidUrls;
public static void main(String... args) {
@@ -48,19 +47,19 @@ public class Main extends JavaPlugin {
}
}
isLoggingConfig = new File("./NoS/willLogCommands.bool");
notifyWebhookConfig = new File("./NoS/notifyWebhook.json");
pubMessageWebhookConfig = new File("./NoS/pubMessageWebhook.json");
webhookConfig = new File("./NoS/config_webhook.json");
try {
if (isLoggingConfig.createNewFile() || notifyWebhookConfig.createNewFile() || pubMessageWebhookConfig.createNewFile()) {
if (isLoggingConfig.createNewFile()) {
System.out.println("Config created: " + isLoggingConfig.getName());
FileWriter notifyWebhookConfigWriter = new FileWriter(notifyWebhookConfig);
FileWriter pubMessageWebhookConfigWriter = new FileWriter(pubMessageWebhookConfig);
notifyWebhookConfigWriter.write("{\"url\": \"default-ns\"}");
pubMessageWebhookConfigWriter.write("{\"url\": \"default-ns\"}");
pubMessageWebhookConfigWriter.close();
notifyWebhookConfigWriter.close();
//FileWriter notifyWebhookConfigWriter = new FileWriter(notifyWebhookConfig);
//FileWriter pubMessageWebhookConfigWriter = new FileWriter(pubMessageWebhookConfig);
//notifyWebhookConfigWriter.write("{\"url_notify\": \"default-ns\"}");
FileWriter linkConfigWriter = new FileWriter(webhookConfig);
linkConfigWriter.write("{\"url_publicmsg\": \"default-ns\", \"url_notify\": \"default-ns\"}");
linkConfigWriter.close();
//notifyWebhookConfigWriter.close();
} else {
System.out.println("Created config even while it already exists, skipping step...");
System.out.println("Tried to create config even while it already exists, skipping step...");
}
} catch (IOException e) {
throw new RuntimeException(e);
@@ -128,22 +127,22 @@ public class Main extends JavaPlugin {
}
public static String getPublicMessageWebhook() {
try {
BufferedReader brfmsg = new BufferedReader(new FileReader(pubMessageWebhookConfig));
BufferedReader brfmsg = new BufferedReader(new FileReader(webhookConfig));
JsonParser jsonReader = new JsonParser();
JsonElement MsgParse = jsonReader.parse(brfmsg.readLine());
String lineMsg = MsgParse.getAsJsonObject().get("url").getAsString();
String lineMsg = MsgParse.getAsJsonObject().get("url_publicmsg").getAsString();
// validate url being url
// but this won't validate the url being an actual webhook
// i can try but lazy :sob:
if (lineMsg.equals("default-ns")) {
System.out.printf("Please set the webhook URL in %s and restart the server", pubMessageWebhookConfig.getAbsolutePath());
System.out.printf("Please set the public message webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineMsg;
} else if (lineMsg.startsWith("https://") || lineMsg.startsWith("http://")) {
webhooksAreValidUrls = true;
return lineMsg;
} else {
System.out.printf("Please set the webhook URL in %s and restart the server", pubMessageWebhookConfig.getAbsolutePath());
System.out.printf("Please set the public message webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineMsg;
}
@@ -153,22 +152,22 @@ public class Main extends JavaPlugin {
}
public static String getNotifyWebhook() {
try {
BufferedReader brfntfy = new BufferedReader(new FileReader(notifyWebhookConfig));
BufferedReader brfntfy = new BufferedReader(new FileReader(webhookConfig));
JsonParser jsonReader = new JsonParser();
JsonElement NtfyParse = jsonReader.parse(brfntfy.readLine());
String lineNtfy = NtfyParse.getAsJsonObject().get("url").getAsString();
String lineNtfy = NtfyParse.getAsJsonObject().get("url_notify").getAsString();
// validate url being url
// but this won't validate the url being an actual webhook
// i can try but lazy :sob:
if (lineNtfy.equals("default-ns")) {
System.out.printf("Please set the webhook URL in %s and restart the server", notifyWebhookConfig.getAbsolutePath());
System.out.printf("Please set the webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineNtfy;
} else if (lineNtfy.startsWith("https://") || lineNtfy.startsWith("http://")) {
webhooksAreValidUrls = true;
return lineNtfy;
} else {
System.out.printf("Please set the webhook URL in %s and restart the server", notifyWebhookConfig.getAbsolutePath());
System.out.printf("Please set the webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineNtfy;
}
@@ -195,6 +194,7 @@ public class Main extends JavaPlugin {
}
return "err";
}
public void registerEvents() {
PlayerChat playerChat = new PlayerChat();
PlayerCommandPreprocess playerCommandPreprocess = new PlayerCommandPreprocess();