|
@ -2,20 +2,21 @@ package de.yannicpunktdee.yoshibot.command.commands; |
|
|
|
|
|
|
|
|
import de.yannicpunktdee.yoshibot.command.YoshiCommand; |
|
|
import de.yannicpunktdee.yoshibot.command.YoshiCommand; |
|
|
import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; |
|
|
import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; |
|
|
import de.yannicpunktdee.yoshibot.utils.Logger; |
|
|
|
|
|
|
|
|
import de.yannicpunktdee.yoshibot.utils.GifSequenceWriter; |
|
|
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 java.io.BufferedReader; |
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO; |
|
|
|
|
|
import javax.imageio.stream.FileImageOutputStream; |
|
|
|
|
|
import javax.imageio.stream.ImageOutputStream; |
|
|
|
|
|
import java.awt.*; |
|
|
|
|
|
import java.awt.image.BufferedImage; |
|
|
import java.io.File; |
|
|
import java.io.File; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.io.InputStreamReader; |
|
|
|
|
|
import java.util.List; |
|
|
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 { |
|
|
|
|
|
|
|
@ -28,57 +29,61 @@ public class PatCommand extends YoshiCommand { |
|
|
if (!super.execute()) return false; |
|
|
if (!super.execute()) return false; |
|
|
|
|
|
|
|
|
List<Message.Attachment> attachments = context.getEvent().getMessage().getAttachments(); |
|
|
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 { |
|
|
|
|
|
|
|
|
if(attachments.size() != 1){ |
|
|
|
|
|
sendMessage("Um dieses Kommando auszuführen benötigt es EINE Bilddatei."); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
String outPath = Resources.getTempPath().replace('\\', '/') + UUID.randomUUID().toString() + ".gif"; |
|
|
|
|
|
|
|
|
Message.Attachment attachment = attachments.get(0); |
|
|
|
|
|
|
|
|
|
|
|
String inPath = |
|
|
|
|
|
Resources.getTempPath() |
|
|
|
|
|
+ UUID.randomUUID().toString() |
|
|
|
|
|
+ "." + attachment.getFileExtension(); |
|
|
|
|
|
File outFile = new File(Resources.getTempPath() + UUID.randomUUID().toString() + ".gif"); |
|
|
|
|
|
|
|
|
|
|
|
CompletableFuture<File> future = attachment.downloadToFile(inPath); |
|
|
|
|
|
future.exceptionally(e -> { |
|
|
|
|
|
sendMessage("Der Anhang konnte nicht gedownloaded werden."); |
|
|
|
|
|
return null; |
|
|
|
|
|
}); |
|
|
try { |
|
|
try { |
|
|
ProcessBuilder pb = new ProcessBuilder( |
|
|
|
|
|
"python3", |
|
|
|
|
|
Resources.getPatPath(), |
|
|
|
|
|
"--image", |
|
|
|
|
|
path, |
|
|
|
|
|
"--patfolder", |
|
|
|
|
|
Resources.getPatPngPath(), |
|
|
|
|
|
"--out", |
|
|
|
|
|
outPath); |
|
|
|
|
|
|
|
|
|
|
|
Process p = pb.start(); |
|
|
|
|
|
String error = new BufferedReader(new InputStreamReader(p.getErrorStream())).lines().collect( |
|
|
|
|
|
Collectors.joining()); |
|
|
|
|
|
if (error.length() > 0) { |
|
|
|
|
|
Logger.log(error, Logger.Type.ERROR); |
|
|
|
|
|
} |
|
|
|
|
|
p.waitFor(); |
|
|
|
|
|
System.out.println(outPath); |
|
|
|
|
|
context.getEvent().getTextChannel().sendFile(new File(outPath)).queue(); |
|
|
|
|
|
} catch (IOException | InterruptedException e) { |
|
|
|
|
|
|
|
|
future.get(); |
|
|
|
|
|
} catch (InterruptedException | ExecutionException e) { |
|
|
|
|
|
sendMessage("Die Bilddatei konnte nicht ordnungsgemäß erstellt werden."); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
BufferedImage inPicture= ImageIO.read(new File(inPath)); |
|
|
|
|
|
BufferedImage pat1Picture= ImageIO.read(new File(Resources.getPatPngPath() + "pat1.png")); |
|
|
|
|
|
BufferedImage pat2Picture= ImageIO.read(new File(Resources.getPatPngPath() + "pat2.png")); |
|
|
|
|
|
BufferedImage pat3Picture= ImageIO.read(new File(Resources.getPatPngPath() + "pat3.png")); |
|
|
|
|
|
BufferedImage frame1 = getOutFrame(pat1Picture, inPicture, 100, 100, 400, 400); |
|
|
|
|
|
ImageOutputStream output = new FileImageOutputStream(outFile); |
|
|
|
|
|
GifSequenceWriter writer = new GifSequenceWriter(output, frame1.getType(), 100, true); |
|
|
|
|
|
writer.writeToSequence(frame1); |
|
|
|
|
|
writer.writeToSequence(getOutFrame(pat2Picture, inPicture, 100, 70, 400, 430)); |
|
|
|
|
|
writer.writeToSequence(getOutFrame(pat3Picture, inPicture, 100, 50, 400, 450)); |
|
|
|
|
|
writer.close(); |
|
|
|
|
|
output.close(); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
sendMessage("Gif konnte nicht erstellt werden,"); |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
context.getEvent().getTextChannel().sendFile(outFile).queue(); |
|
|
|
|
|
|
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private BufferedImage getOutFrame(BufferedImage patHandGraphics, BufferedImage personGraphics, int x, int y, int width, int height){ |
|
|
|
|
|
BufferedImage outFrame = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB); |
|
|
|
|
|
Graphics2D g = outFrame.createGraphics(); |
|
|
|
|
|
g.setColor(Color.black); |
|
|
|
|
|
g.drawImage(personGraphics, x, y, width, height, null); |
|
|
|
|
|
g.drawImage(patHandGraphics, 0, 0, 500, 400, null); |
|
|
|
|
|
return outFrame; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |