Created the start of what's needed to auto upload messages to Kanboard
Added event listener for tags being added, still missing the tag id Added code to forward mesage to kanboard, missing the correct link and title/body Error handling that only outputs in console, stil needs to notify the user who adds the tags
This commit is contained in:
parent
c7120cc26c
commit
093571d312
|
|
@ -16,10 +16,12 @@ public class SettingsConfig extends AbstractConfig {
|
|||
|
||||
// SETTINGS
|
||||
public static String TOKEN = "token";
|
||||
public static String KANBOARD_TOKEN = "kanboard-token";
|
||||
public static boolean DEBUG = false;
|
||||
|
||||
private void loadSettings() {
|
||||
TOKEN = settingsConfig.getString("settings.token", TOKEN);
|
||||
KANBOARD_TOKEN = settingsConfig.getString("settings.kanboard-token", KANBOARD_TOKEN);
|
||||
DEBUG = settingsConfig.getBoolean("settings.debug", DEBUG);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,12 +36,13 @@ public class JDAListener extends ListenerAdapter {
|
|||
Logger.altitudeLogs.info("JDA ready to register commands.");
|
||||
LockedChannel lockedChannel = new LockedChannel();
|
||||
ButtonManager buttonManager = new ButtonManager();
|
||||
TagAdded tagAdded = new TagAdded();
|
||||
AppealRepost appealRepost = new AppealRepost(buttonManager);
|
||||
ModalManager modalManager = new ModalManager(buttonManager);
|
||||
ContextMenuManager contextMenuManager = new ContextMenuManager(modalManager);
|
||||
SelectMenuManager selectMenuManager = new SelectMenuManager();
|
||||
CommandManager commandManager = new CommandManager(jda, modalManager, contextMenuManager, lockedChannel, selectMenuManager, buttonManager);
|
||||
jda.addEventListener(buttonManager, modalManager, commandManager, contextMenuManager, lockedChannel, appealRepost, selectMenuManager);
|
||||
jda.addEventListener(buttonManager, tagAdded, modalManager, commandManager, contextMenuManager, lockedChannel, appealRepost, selectMenuManager);
|
||||
PollQueries.loadPolls(buttonManager);
|
||||
new Timer().scheduleAtFixedRate(new PollTimerTask(jda, Logger.altitudeLogs), TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(5));
|
||||
startSchedulers();
|
||||
|
|
|
|||
70
src/main/java/com/alttd/listeners/TagAdded.java
Normal file
70
src/main/java/com/alttd/listeners/TagAdded.java
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package com.alttd.listeners;
|
||||
|
||||
import com.alttd.config.SettingsConfig;
|
||||
import com.alttd.util.Logger;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
|
||||
import net.dv8tion.jda.api.events.channel.forum.ForumTagAddEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
public class TagAdded extends ListenerAdapter {
|
||||
|
||||
public void tagAdded(ForumTagAddEvent event) {
|
||||
if (event.getTag().getIdLong() != 0L) {//TODO add tag id
|
||||
return;
|
||||
}
|
||||
if (!(event.getChannel() instanceof ThreadChannel threadChannel)) {
|
||||
return;
|
||||
}
|
||||
threadChannel.retrieveStartMessage().queue(this::forwardMessageToKanboard, error -> {
|
||||
Logger.altitudeLogs.error(error);
|
||||
});
|
||||
}
|
||||
|
||||
//TODO move to its own util class
|
||||
public void forwardMessageToKanboard(Message message) {
|
||||
String contentDisplay = message.getContentDisplay();
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request;
|
||||
try {
|
||||
String jsonPayload = String.format("{"
|
||||
+ "\"title\": \"%s\","
|
||||
+ "\"description\": \"%s\","
|
||||
+ "\"project_id\": \"%d\""
|
||||
+ "}", "New Task" /*TODO config*/, contentDisplay, 3/*TODO config*/);
|
||||
|
||||
request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://kanboard.alttd.com/jsonrpc.php")) //TODO correct URL
|
||||
.header("Content-Type", "application/json")
|
||||
.setHeader("Authorization", SettingsConfig.KANBOARD_TOKEN)
|
||||
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
|
||||
.build();
|
||||
} catch (URISyntaxException e) {
|
||||
Logger.altitudeLogs.error(e);
|
||||
//TODO handle better
|
||||
return;
|
||||
}
|
||||
|
||||
HttpResponse<String> response;
|
||||
try {
|
||||
response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
} catch (InterruptedException | IOException e) {
|
||||
Logger.altitudeLogs.error(e);
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.statusCode() == 200) {
|
||||
Logger.altitudeLogs.info(response.body());
|
||||
} else {
|
||||
Logger.altitudeLogs.error(String.format("Invalid response [%s]", response.body()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user