diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java index aa192a4..8bb57dd 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java @@ -1,7 +1,17 @@ package de.yannicpunktdee.yoshibot.command; +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.Message.Attachment; import net.dv8tion.jda.api.entities.MessageEmbed; +import java.io.File; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + /** * Abstrakte Superklasse für alle Kommandos. * @author Yannic Link @@ -46,4 +56,40 @@ public abstract class YoshiCommand { context.getEvent().getTextChannel().sendMessage(messageEmbed).queue(); } + protected File downloadAttachmentToFile(String directoryPath, String name){ + if(directoryPath == null) directoryPath = Resources.getTempPath(); + if(name == null) name = UUID.randomUUID().toString(); + + if(!(new File(directoryPath)).isDirectory()){ + Logger.logError("Das Download-Verzeichnis wurde nicht gefunden."); + sendMessage("Der Anhang konnte nicht gedownloaded werden."); + return null; + } + + List attachments = context.getEvent().getMessage().getAttachments(); + if(attachments.size() == 0){ + return null; + } + Attachment attachment = attachments.get(0); + + File file = new File(directoryPath + name + "." + attachment.getFileExtension()); + CompletableFuture future = attachment.downloadToFile(file); + future.exceptionally(e -> { + sendMessage("Ein Anhang konnte nicht gedownloaded werden."); + return null; + }); + try { + future.get(); + } catch (InterruptedException | ExecutionException e) { + sendMessage("Ein Anhang konnte nicht gedownloaded werden."); + return null; + } + if(!file.exists()) { + sendMessage("Ein Anhang konnte nicht gedownloaded werden."); + return null; + } + + return file; + } + } 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 714acd2..47805f9 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 @@ -27,34 +27,11 @@ public class PatCommand extends YoshiCommand { @Override public boolean execute() { if (!super.execute()) return false; - - List attachments = context.getEvent().getMessage().getAttachments(); - if(attachments.size() != 1){ - sendMessage("Um dieses Kommando auszuführen benötigt es EINE Bilddatei."); - return false; - } - 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 future = attachment.downloadToFile(inPath); - future.exceptionally(e -> { - sendMessage("Der Anhang konnte nicht gedownloaded werden."); - return null; - }); - try { - future.get(); - } catch (InterruptedException | ExecutionException e) { - sendMessage("Die Bilddatei konnte nicht ordnungsgemäß erstellt werden."); - return false; - } - try { - BufferedImage inPicture= ImageIO.read(new File(inPath)); + BufferedImage inPicture= ImageIO.read(downloadAttachmentToFile(null, null)); 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")); diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java index 40176b3..3d4f2e7 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java @@ -42,29 +42,7 @@ public class PlayCommand extends YoshiCommand { YoshiBot yoshiBot = YoshiBot.getInstance(); if (context.containsArguments(new String[]{"add"})) { - List attachments = context.getEvent().getMessage().getAttachments(); - if (attachments.size() != 1) { - sendMessage("Falsche Anzahl an Anhängen"); - return false; - } - - String path = Resources.getPathToAudioFile(context.getArgument("name")); - if ((new File(path)).exists()) { - sendMessage("Ein Soundeffekt mit diesem Namen existiert bereits."); - return false; - } - CompletableFuture future = attachments.get(0).downloadToFile(path); - future.exceptionally(e -> { - sendMessage("Der Anhang konnte nicht gedownloaded werden."); - return null; - }); - try { - future.get(); - sendMessage("Sound erfolgreich hinzugef\u00fcgt."); - } catch (InterruptedException | ExecutionException e) { - sendMessage("Die Sounddatei konnte nicht ordnungsgemäß erstellt werden."); - return false; - } + downloadAttachmentToFile(Resources.getAudioPath(), context.getArgument("name")); } else if (context.containsArguments(new String[]{"list"})) { File audioDirectory = new File(Resources.getAudioPath()); StringBuilder sb = new StringBuilder();