|
@ -0,0 +1,87 @@ |
|
|
|
|
|
package de.yannicpunktdee.yoshibot.command.commands; |
|
|
|
|
|
|
|
|
|
|
|
import de.yannicpunktdee.yoshibot.command.YoshiCommand; |
|
|
|
|
|
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; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.io.InputStreamReader; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
import java.util.concurrent.CompletableFuture; |
|
|
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
} |
|
|
|
|
|
} |