Browse Source

Download von Bildern in Super-Methode von YoshiCommand verschoben.

pull/2/head
Yannic Link 4 years ago
parent
commit
ac1da67400
3 changed files with 48 additions and 47 deletions
  1. +46
    -0
      app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java
  2. +1
    -24
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java
  3. +1
    -23
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java

+ 46
- 0
app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java View File

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

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

@ -27,34 +27,11 @@ public class PatCommand extends YoshiCommand {
@Override
public boolean execute() {
if (!super.execute()) return false;
List<Message.Attachment> 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<File> 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"));


+ 1
- 23
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java View File

@ -42,29 +42,7 @@ public class PlayCommand extends YoshiCommand {
YoshiBot yoshiBot = YoshiBot.getInstance();
if (context.containsArguments(new String[]{"add"})) {
List<Message.Attachment> 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<File> 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();


Loading…
Cancel
Save