Browse Source

Fixed patting

pull/2/head
Paul Glaß 4 years ago
parent
commit
cc4d15b3a7
5 changed files with 92 additions and 77 deletions
  1. +64
    -68
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java
  2. +28
    -9
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java
  3. BIN
      rsc/pats/pat1.png
  4. BIN
      rsc/pats/pat2.png
  5. BIN
      rsc/pats/pat3.png

+ 64
- 68
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java View File

@ -5,7 +5,6 @@ import de.yannicpunktdee.yoshibot.command.YoshiCommandContext;
import de.yannicpunktdee.yoshibot.utils.Logger; import de.yannicpunktdee.yoshibot.utils.Logger;
import de.yannicpunktdee.yoshibot.utils.Resources; import de.yannicpunktdee.yoshibot.utils.Resources;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -15,73 +14,70 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class PatCommand extends YoshiCommand { public class PatCommand extends YoshiCommand {
public PatCommand(YoshiCommandContext context) {
super(context);
}
@Override
public boolean execute() {
if(!super.execute()) return false;
List<Message.Attachment> 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<File> 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<Message.Attachment> 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<File> 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<String> 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;
}
} }

+ 28
- 9
app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java View File

@ -39,10 +39,10 @@ public final class Resources {
@Getter @Getter
private static String patPath; private static String patPath;
@Getter @Getter
private static String patPngPath;
@Getter
private static String imagePath; private static String imagePath;
private static Properties propertiesFile;
@Getter @Getter
private static String jda_builder_string; private static String jda_builder_string;
@ -62,6 +62,8 @@ public final class Resources {
@Getter @Getter
private static final Map<String, List<String>> feedDetails = new HashMap<>(); private static final Map<String, List<String>> feedDetails = new HashMap<>();
private static Properties propertiesFile;
public synchronized static boolean init(String resourcePathArg) { public synchronized static boolean init(String resourcePathArg) {
boolean isOk = initResources(resourcePathArg); boolean isOk = initResources(resourcePathArg);
@ -77,6 +79,7 @@ public final class Resources {
if (isOk) isOk = initChannelRestrict(); if (isOk) isOk = initChannelRestrict();
if (isOk) isOk = initTagFilter(); if (isOk) isOk = initTagFilter();
if (isOk) isOk = initPat(); if (isOk) isOk = initPat();
if (isOk) isOk = initPatPngPath();
if (isOk) isOk = initImages(); if (isOk) isOk = initImages();
if (isOk) Logger.log("Die Konfigurationen wurden erfolgreich geladen.", Type.INFO); if (isOk) Logger.log("Die Konfigurationen wurden erfolgreich geladen.", Type.INFO);
@ -120,12 +123,13 @@ public final class Resources {
private static boolean initTemp() { private static boolean initTemp() {
Logger.log("Versuche Temp-Verzeichnis zu finden.", Type.INFO); 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) { if (tempPath != null) {
return true; return true;
} }
File tempDir = new File(theoreticalTempPath);
if (tempDir.mkdir()) { if (tempDir.mkdir()) {
return verifyExists(tempDir.getAbsolutePath(), File::isDirectory) != null; return verifyExists(tempDir.getAbsolutePath(), File::isDirectory) != null;
@ -164,6 +168,11 @@ public final class Resources {
return true; return true;
} }
private static boolean initPatPngPath(){
patPngPath = verifyExists(resourcePath + "pats/", File::isDirectory);
return patPngPath != null;
}
private static boolean initGuildId() { private static boolean initGuildId() {
if (!propertiesFile.containsKey("guild_id")) { if (!propertiesFile.containsKey("guild_id")) {
Logger.log("Die Config.properties benötigt das Attribut guild_id.", Type.ERROR); 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); Logger.log("tags_general_filter erfolgreich geladen", Type.INFO);
return true; return true;
} }
private static boolean initPat(){
private static boolean initPat() {
patPath = verifyExists(resourcePath + "pat.py", File::isFile); patPath = verifyExists(resourcePath + "pat.py", File::isFile);
return patPath != null; return patPath != null;
} }
private static boolean initImages(){
private static boolean initImages() {
imagePath = verifyExists(resourcePath + "image/", File::isDirectory); 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<File, Boolean> checkIsValidFile) { private static String verifyExists(String filename, Function<File, Boolean> checkIsValidFile) {


BIN
rsc/pats/pat1.png View File

Before After
Width: 3000  |  Height: 3000  |  Size: 456 KiB

BIN
rsc/pats/pat2.png View File

Before After
Width: 3000  |  Height: 3000  |  Size: 414 KiB

BIN
rsc/pats/pat3.png View File

Before After
Width: 3000  |  Height: 3000  |  Size: 409 KiB

Loading…
Cancel
Save