diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java index 46151a1..56826af 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java @@ -5,7 +5,6 @@ import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; import de.yannicpunktdee.yoshibot.utils.Logger; import de.yannicpunktdee.yoshibot.utils.Resources; import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.TextChannel; import java.io.BufferedReader; import java.io.File; @@ -15,73 +14,70 @@ import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class PatCommand extends YoshiCommand { - - public PatCommand(YoshiCommandContext context) { - super(context); - } - - @Override - public boolean execute() { - if(!super.execute()) return false; - - List attachments = context.getEvent().getMessage().getAttachments(); - String path = ""; - if(context.containsArguments(new String[]{"name"})){ - path = Resources.getImagePath() + context.getArgument("name") + ".png"; - File image = new File(path); - if(!image.exists()){ - sendMessage("Bilddatei existiert nicht."); - return false; - } - }else if(attachments.size() == 1){ - path = Resources.getTempPath() + UUID.randomUUID().toString() + ".png"; - CompletableFuture future = attachments.get(0).downloadToFile(path); - future.exceptionally(e -> { - sendMessage("Der Anhang konnte nicht gedownloaded werden."); - return null; - }); - try { - future.get(); - sendMessage("Bild erfolgreich heruntergeladen."); - } catch (InterruptedException | ExecutionException e) { - sendMessage("Die Bilddatei konnte nicht ordnungsgemäß erstellt werden."); - return false; - } - }else{ - return false; - } - - String outPath = Resources.getTempPath().replace('\\', '/') + "/" + UUID.randomUUID().toString() + ".gif"; - try { - ProcessBuilder pb = new ProcessBuilder( - "python", - Resources.getPatPath(), - "--image", - path, - "--patfolder", - Resources.getImagePath(), - "--out", - outPath); - - Process p = pb.start(); - BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream())); - StringBuilder builder = new StringBuilder(); - String line; - while ((line = errorReader.readLine()) != null) { - builder.append(line).append("\n"); - } - if (builder.toString().length() > 0) { - Logger.log(builder.toString(), Logger.Type.ERROR); - } - p.waitFor(); - System.out.println(outPath); - context.getEvent().getTextChannel().sendFile(new File(outPath)).queue(); - } catch (IOException | InterruptedException e) { - return false; - } - - return true; - } + + public PatCommand(YoshiCommandContext context) { + super(context); + } + + @Override + public boolean execute() { + if (!super.execute()) return false; + + List attachments = context.getEvent().getMessage().getAttachments(); + String path = ""; + if (context.containsArguments(new String[]{"name"})) { + path = Resources.getImagePath() + context.getArgument("name") + ".png"; + File image = new File(path); + if (!image.exists()) { + sendMessage("Bilddatei existiert nicht."); + return false; + } + } else if (attachments.size() == 1) { + path = Resources.getTempPath() + UUID.randomUUID().toString() + ".png"; + CompletableFuture future = attachments.get(0).downloadToFile(path); + future.exceptionally(e -> { + sendMessage("Der Anhang konnte nicht gedownloaded werden."); + return null; + }); + try { + future.get(); + sendMessage("Bild erfolgreich heruntergeladen."); + } catch (InterruptedException | ExecutionException e) { + sendMessage("Die Bilddatei konnte nicht ordnungsgemäß erstellt werden."); + return false; + } + } else { + return false; + } + + String outPath = Resources.getTempPath().replace('\\', '/') + UUID.randomUUID().toString() + ".gif"; + try { + ProcessBuilder pb = new ProcessBuilder( + "python3", + Resources.getPatPath(), + "--image", + path, + "--patfolder", + Resources.getPatPngPath(), + "--out", + outPath); + + Process p = pb.start(); + Stream error = new BufferedReader(new InputStreamReader(p.getErrorStream())).lines(); + if (error.count() > 0) { + Logger.log(error.collect(Collectors.joining()), Logger.Type.ERROR); + } + p.waitFor(); + System.out.println(outPath); + context.getEvent().getTextChannel().sendFile(new File(outPath)).queue(); + } catch (IOException | InterruptedException e) { + return false; + } + + return true; + } } diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java b/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java index b1501d7..6190f67 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java @@ -39,10 +39,10 @@ public final class Resources { @Getter private static String patPath; @Getter + private static String patPngPath; + @Getter private static String imagePath; - private static Properties propertiesFile; - @Getter private static String jda_builder_string; @@ -62,6 +62,8 @@ public final class Resources { @Getter private static final Map> feedDetails = new HashMap<>(); + private static Properties propertiesFile; + public synchronized static boolean init(String resourcePathArg) { boolean isOk = initResources(resourcePathArg); @@ -77,6 +79,7 @@ public final class Resources { if (isOk) isOk = initChannelRestrict(); if (isOk) isOk = initTagFilter(); if (isOk) isOk = initPat(); + if (isOk) isOk = initPatPngPath(); if (isOk) isOk = initImages(); if (isOk) Logger.log("Die Konfigurationen wurden erfolgreich geladen.", Type.INFO); @@ -120,12 +123,13 @@ public final class Resources { private static boolean initTemp() { Logger.log("Versuche Temp-Verzeichnis zu finden.", Type.INFO); - File tempDir = new File(System.getProperty("java.io.tmpdir").replace('\\', '/') + "/yoshibot/"); + String theoreticalTempPath = System.getProperty("java.io.tmpdir").replace('\\', '/') + "/yoshibot/"; - tempPath = verifyExists(tempDir.getAbsolutePath(), File::isDirectory); + tempPath = verifyExists(theoreticalTempPath, File::isDirectory); if (tempPath != null) { return true; } + File tempDir = new File(theoreticalTempPath); if (tempDir.mkdir()) { return verifyExists(tempDir.getAbsolutePath(), File::isDirectory) != null; @@ -164,6 +168,11 @@ public final class Resources { return true; } + private static boolean initPatPngPath(){ + patPngPath = verifyExists(resourcePath + "pats/", File::isDirectory); + return patPngPath != null; + } + private static boolean initGuildId() { if (!propertiesFile.containsKey("guild_id")) { Logger.log("Die Config.properties benötigt das Attribut guild_id.", Type.ERROR); @@ -248,15 +257,25 @@ public final class Resources { Logger.log("tags_general_filter erfolgreich geladen", Type.INFO); return true; } - - private static boolean initPat(){ + + private static boolean initPat() { patPath = verifyExists(resourcePath + "pat.py", File::isFile); return patPath != null; } - - private static boolean initImages(){ + + private static boolean initImages() { imagePath = verifyExists(resourcePath + "image/", File::isDirectory); - return imagePath != null; + if (imagePath != null) { + return true; + } + if (new File(resourcePath + "image/").mkdir()) { + imagePath = verifyExists(resourcePath + "image/", File::isDirectory); + Logger.log("Bildordner erzeugt", Type.INFO); + return true; + } else { + Logger.log("Konnte Bildordner nicht erzeugen!", Type.ERROR); + return false; + } } private static String verifyExists(String filename, Function checkIsValidFile) { diff --git a/rsc/pats/pat1.png b/rsc/pats/pat1.png new file mode 100644 index 0000000..c283cfd Binary files /dev/null and b/rsc/pats/pat1.png differ diff --git a/rsc/pats/pat2.png b/rsc/pats/pat2.png new file mode 100644 index 0000000..66494f0 Binary files /dev/null and b/rsc/pats/pat2.png differ diff --git a/rsc/pats/pat3.png b/rsc/pats/pat3.png new file mode 100644 index 0000000..150223e Binary files /dev/null and b/rsc/pats/pat3.png differ