Refactor code to improve message forwarding to Kanboard
This commit extracts the method "forwardMessageToKanboard" from the "TagAdded" class to the new utility class "Kanboard", making the code base cleaner and more maintainable. It also introduces "ContextMenuForwardToKanboard" for better message management. Further modifications include alterations in "AltitudeBot", such as changes in permissions and implementing @Getter.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package com.alttd.util;
|
||||
|
||||
import com.alttd.config.SettingsConfig;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -9,23 +11,26 @@ import java.net.URISyntaxException;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Kanboard {
|
||||
|
||||
public static CompletableFuture<Boolean> forwardMessageToKanboard(String title, String body) {
|
||||
public static CompletableFuture<Boolean> forwardMessageToKanboard(String title, String body, String webLink) {
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request;
|
||||
String jsonPayload = getPayload(UUID.randomUUID().toString(), title, body, webLink);
|
||||
try {
|
||||
String jsonPayload = getPayload(UUID.randomUUID().toString(), title, body);
|
||||
|
||||
byte[] kanboardTokenBytes = String.join(":", "jsonrpc", SettingsConfig.KANBOARD_TOKEN).getBytes(StandardCharsets.UTF_8);
|
||||
String token = Base64.getEncoder().encodeToString(kanboardTokenBytes);
|
||||
request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://kanboard.alttd.com/jsonrpc.php"))
|
||||
.header("Content-Type", "application/json")
|
||||
.setHeader("Authorization", SettingsConfig.KANBOARD_TOKEN)
|
||||
.setHeader("Authorization", "Basic " + token)
|
||||
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
|
||||
.build();
|
||||
} catch (URISyntaxException e) {
|
||||
@@ -43,11 +48,11 @@ public class Kanboard {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (response.statusCode() == 200) {
|
||||
Logger.altitudeLogs.info(response.body());
|
||||
if (response.statusCode() == 200 && response.body().matches(".*\"result\":[0-9]+,.*")) {
|
||||
Logger.altitudeLogs.info(String.format("Response body: [%s]\nFor JsonPayload: [%s]", response.body(), jsonPayload));
|
||||
return true;
|
||||
} else {
|
||||
Logger.altitudeLogs.error(String.format("Invalid response [%s]", response.body()));
|
||||
Logger.altitudeLogs.error(String.format("Invalid response: [%s]\nPayload: [%s]", response.body(), jsonPayload));
|
||||
return false;
|
||||
}
|
||||
});
|
||||
@@ -55,31 +60,71 @@ public class Kanboard {
|
||||
|
||||
public static CompletableFuture<Boolean> forwardMessageToKanboard(Message message) {
|
||||
String body = message.getContentDisplay();
|
||||
if (messageIsSuggestion(message) && message.getGuildChannel() instanceof ThreadChannel threadChannel) {
|
||||
return forwardSuggestionToKanboard(message, threadChannel);
|
||||
}
|
||||
String[] split = body.split("\n");
|
||||
String title;
|
||||
if (split.length == 1) {
|
||||
title = "No title";
|
||||
} else {
|
||||
title = split[0];
|
||||
body = Arrays.stream(split).skip(1).collect(Collectors.joining("\n"));
|
||||
body = Arrays.stream(split).skip(1).filter(str -> !str.isBlank()).collect(Collectors.joining("\n"));
|
||||
}
|
||||
return forwardMessageToKanboard(title, body);
|
||||
return forwardMessageToKanboard(title, body, getWebLink(message));
|
||||
}
|
||||
|
||||
private static String getPayload(String id, String title, String body) {
|
||||
private static CompletableFuture<Boolean> forwardSuggestionToKanboard(Message message, ThreadChannel threadChannel) {
|
||||
String title = threadChannel.getName();
|
||||
String content = Arrays.stream(message.getContentDisplay().split("\n")).skip(1).filter(str -> !str.isBlank()).collect(Collectors.joining("\n"));
|
||||
return forwardMessageToKanboard(title, content, getWebLink(message));
|
||||
}
|
||||
|
||||
private static boolean messageIsSuggestion(Message message) {
|
||||
Member member = message.getMember();
|
||||
if (member == null)
|
||||
return false;
|
||||
|
||||
if (!member.getUser().equals(message.getJDA().getSelfUser())) {
|
||||
return false;
|
||||
}
|
||||
if (!message.getContentRaw().startsWith("**Suggestion by:")) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static String getPayload(String id, String title, String body, String webLink) {
|
||||
return String.format("""
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "createTask",
|
||||
"id": %s,
|
||||
"id": "%s",
|
||||
"params": {
|
||||
"title": "%s",
|
||||
"project_id": "3",
|
||||
"description": "%s",
|
||||
"description": "%s%s",
|
||||
"column_id": "0",
|
||||
"color_id": "red"
|
||||
}
|
||||
}""", id, title, body);
|
||||
}""",
|
||||
escapeSpecialJsonChars(id),
|
||||
escapeSpecialJsonChars(title),
|
||||
escapeSpecialJsonChars(body),
|
||||
escapeSpecialJsonChars(String.format("\n\n[Discord link](%s)", webLink)));
|
||||
}
|
||||
|
||||
private static String getWebLink(Message message) {
|
||||
return String.format("https://discord.com/channels/%d/%d/%d", message.getGuildIdLong(), message.getChannelIdLong(), message.getIdLong());
|
||||
}
|
||||
|
||||
private static String escapeSpecialJsonChars(String s) {
|
||||
return s.replace("\"", "\\\"")
|
||||
.replace("\\", "\\\\")
|
||||
.replace("\b", "\\b")
|
||||
.replace("\f", "\\f")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r")
|
||||
.replace("\t", "\\t");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user