|
|
@ -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<Attachment> 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<File> 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; |
|
|
|
} |
|
|
|
|
|
|
|
} |