Code Optimierung

This commit is contained in:
Ocame
2025-08-31 19:34:53 +02:00
parent c3764aab26
commit 4075203e6f
10 changed files with 24 additions and 30 deletions

View File

@ -378,7 +378,7 @@ public class FanFiktionReader {
Document sendToFav = fetcher.fetchHtml(URL + "/?a=q&a1=v&t=addstory&lbi=popup-addtofav-body&ar=1&action=i", postData);
if(sendToFav.getElementsByClass("successbox").isEmpty()) {
LoggerUtil.log("error", "Reader", "Story konnte nicht hinzugefügt werden");
new InfoNotifier(jda).setMessagetext("Story " + ffid + "konnte nicht hinzugefügt werden. \n" + sendToFav.toString()).sendOutput();
new InfoNotifier(jda).setMessagetext("Story " + ffid + "konnte nicht hinzugefügt werden. \n" + sendToFav).sendOutput();
}
}
catch (IOException | InterruptedException e) {

View File

@ -33,6 +33,7 @@ public class Main {
TimeUnit.SECONDS.sleep(10);
}
catch (InterruptedException e) {
LoggerUtil.log("error", "Main", "Sleeptimer erzuegt Fehler" + e.getMessage());
}
FanFiktionReader ffr = new FanFiktionReader(jda);
@ -56,7 +57,7 @@ public class Main {
delay[0] = ffr.getTime();
LoggerUtil.log("info", "Main", "neue Zeit: " + (Long.toString(Math.round(delay[0] / 60 * 10)/10) ));
LoggerUtil.log("info", "Main", "neue Zeit: " + (Long.toString(Math.round((float) delay[0] / 60 * 10)/10) ));
// Plane die Aufgabe erneut mit dem neuen Intervall
scheduledFuture.get().cancel(false);

View File

@ -2,7 +2,6 @@ package de.ocame.ffdiscordbot;
import java.awt.*;
import java.sql.*;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;

View File

@ -9,7 +9,6 @@ import org.json.JSONObject;
import java.io.*;
import java.net.*;
import java.util.Objects;
class SocketServerRunnable implements Runnable {
@ -54,7 +53,6 @@ class SocketServerRunnable implements Runnable {
}
catch (IOException | NullPointerException e) {
LoggerUtil.log("error", "SocketRunnable", "Fehler bei der Kommunikation mit dem Client: " + e.getMessage());
e.printStackTrace();
}
}
@ -65,7 +63,7 @@ class SocketServerRunnable implements Runnable {
}
private Boolean channelCheckAction(JSONObject jsonData, PrintWriter out) {
JSONObject jsonResponse= new JSONObject();
Long channelId = null;
long channelId;
Object channelIdObj = jsonData.get("channelId");
if(channelIdObj.equals("")) {
@ -76,8 +74,7 @@ class SocketServerRunnable implements Runnable {
if (channelIdObj instanceof Number) {
channelId = ((Number) channelIdObj).longValue();
}
else if (channelIdObj instanceof String) {
String channelIdStr = (String) channelIdObj;
else if (channelIdObj instanceof String channelIdStr) {
channelId = Long.parseLong(channelIdStr);
}
else {
@ -85,8 +82,6 @@ class SocketServerRunnable implements Runnable {
return true;
}
String response = "";
try {
if(jda.getNewsChannelById(channelId) != null) {
NewsChannel textchannel = jda.getNewsChannelById(channelId);

View File

@ -12,7 +12,6 @@ import java.util.regex.Pattern;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
public class AchievementManager {

View File

@ -11,7 +11,6 @@ import net.dv8tion.jda.api.requests.RestAction;
import java.io.FileWriter;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CompletableFuture;
@ -19,7 +18,7 @@ import java.util.concurrent.CompletableFuture;
public class ChatReader extends ListenerAdapter {
RestAction<?> latestUpdate = null;
private Set<String> ChannelWhitelist;
private final Set<String> ChannelWhitelist;
public ChatReader() {
ChannelWhitelist = new HashSet<>();
@ -63,7 +62,7 @@ public class ChatReader extends ListenerAdapter {
if(!ChannelWhitelist.contains(event.getChannel().getId())) {
return;
}
System.out.println(event.getReaction().toString());
System.out.println(event.getReaction());
}
// =========================================================================================
@ -90,7 +89,7 @@ public class ChatReader extends ListenerAdapter {
event.getChannel().getIterableHistory()
.forEachAsync(message -> {
messages.add(message);
System.out.println(message.getType().toString());
System.out.println(message.getType());
handleProgressUpdate(progressMessage, filePath, messages.size());
return messages.size() < MAX_DESIRED;
})
@ -152,7 +151,7 @@ public class ChatReader extends ListenerAdapter {
private void writeMessageToFile(FileWriter fileWriter, Message message) {
try {
fileWriter.write("Time: " + message.getTimeCreated().toString() + "\n");
fileWriter.write("Time: " + message.getTimeCreated() + "\n");
fileWriter.write("Author: " + message.getAuthor().getId() + "\n");
fileWriter.write("Message ID: " + message.getId() + "\n");
fileWriter.write("Content: " + message.getContentRaw() + "\n");
@ -168,7 +167,7 @@ public class ChatReader extends ListenerAdapter {
reaction.retrieveUsers().queue(users -> {
StringBuilder reactionUsers = new StringBuilder();
for (User user : users) {
if (reactionUsers.length() > 0) reactionUsers.append("|");
if (!reactionUsers.isEmpty()) reactionUsers.append("|");
reactionUsers.append(user.getId());
}
reactionFuture.complete("Reaction: " + emojiName + ": " + reactionUsers + "\n");

View File

@ -2,6 +2,7 @@ package de.ocame.ffdiscordbot.achievements;
import de.ocame.ffdiscordbot.ConfigLoader;
import de.ocame.ffdiscordbot.LoggerUtil;
import org.slf4j.LoggerFactory;
import java.sql.*;
@ -32,7 +33,8 @@ public class MySQLStorage {
} catch (SQLException e) {
LoggerUtil.log("error", "MySQL", "Fehler beim Herstellen der Verbindung zur MySQL-Datenbank: " + e.getMessage());
e.printStackTrace();
//e.printStackTrace();
LoggerFactory.getLogger("MySQL").error("Stack: ", e);
}
return connection;
@ -142,7 +144,7 @@ public class MySQLStorage {
String sql = """
INSERT INTO Achievement (userId)
VALUES (?)
ON DUPLICATE KEY UPDATE
ON DUPLICATE KEY UPDATE
username = VALUES(username);
""";
try (Connection connection = getConnection(); PreparedStatement stmt = connection.prepareStatement(sql)) {

View File

@ -107,14 +107,14 @@ public class DefaultHtmlFetcher implements HtmlFetcher {
}
private static class SerializableCookie {
private String uri;
private String name;
private String value;
private String path;
private String domain;
private long maxAge;
private boolean secure;
private boolean httpOnly;
private final String uri;
private final String name;
private final String value;
private final String path;
private final String domain;
private final long maxAge;
private final boolean secure;
private final boolean httpOnly;
SerializableCookie(HttpCookie cookie) {
this.uri = cookie.getDomain();

View File

@ -11,8 +11,8 @@ import java.util.Map;
// Handles notifications about new chapters
public class ChapterNotifier extends BaseNotifier<ChapterNotifier> {
private DataChapter chapter;
private DataStory story;
private final DataChapter chapter;
private final DataStory story;
public ChapterNotifier(JDA jda, DataChapter chapter, DataStory story) {
super(jda);

View File

@ -6,7 +6,6 @@ import net.dv8tion.jda.api.JDA;
import java.time.Instant;
import java.util.Map;
import java.util.Properties;
public class InfoNotifier extends BaseNotifier<InfoNotifier> {