From 04b9edb7ecec64df04be42d11e6e44fd73c30d8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Gla=C3=9F?= Date: Sun, 16 Jan 2022 22:20:49 +0100 Subject: [PATCH] [Added] ability to upload mp3 --- .../yoshibot/command/YoshiCommand.java | 23 +++++++++++++++++++ .../yoshibot/command/YoshiCommandContext.java | 4 ++++ .../command/commands/PlayCommand.java | 13 +++++++---- .../yoshibot/utils/Resources.java | 9 +++++++- 4 files changed, 43 insertions(+), 6 deletions(-) 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 397d9b3..5c71097 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java @@ -9,11 +9,15 @@ import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.VoiceChannel; import java.awt.*; +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; +import java.util.stream.Collectors; /** * Abstrakte Superklasse für alle Kommandos. @@ -124,6 +128,25 @@ public abstract class YoshiCommand { return null; } + if (file.getAbsolutePath().endsWith(".mp3")) { + String newFilePath = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 3) + "opus"; + Runtime rt = Runtime.getRuntime(); + try { + String command = "/usr/bin/ffmpeg -y -i " + file.getAbsolutePath() + " " + newFilePath; + Process pr = rt.exec(command); + String err = new BufferedReader(new InputStreamReader(pr.getErrorStream())).lines() + .collect(Collectors.joining()); + String out = new BufferedReader(new InputStreamReader(pr.getInputStream())).lines().collect( + Collectors.joining()); + int exit = pr.waitFor(); + if (!file.delete()) { + throw new IOException("Delete ging nich"); + } + return new File(newFilePath); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + } return file; } diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandContext.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandContext.java index 171f825..b8c5eb1 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandContext.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandContext.java @@ -266,6 +266,10 @@ public class YoshiCommandContext { return !arguments.isEmpty(); } + + public boolean containsArgument(String arg){ + return this.containsArguments(new String[]{arg}); + } /** * Prüft, ob alle Key-Werte in der Argumentenliste vorhanden sind. * 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 c22fcc0..e5318a3 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 @@ -30,11 +30,11 @@ public class PlayCommand extends YoshiCommand { public boolean execute() { if (!super.execute()) return false; - if (context.containsArguments(new String[]{"add"})) { + if (context.containsArgument("add")) { File download = downloadAttachmentToFile(Resources.getAudioPath(), context.getArgument("name")); if (download.isFile()) sendInfoMessage("Audio erfolgreich hinzugefügt."); else sendErrorMessage("Audio konnte nicht hinzugefügt werden."); - } else if (context.containsArguments(new String[]{"list"})) { + } else if (context.containsArgument("list")) { StringBuilder sb = new StringBuilder(); getAllFiles().forEach(name -> sb.append(name).append("\n")); EmbedBuilder eb = new EmbedBuilder(); @@ -42,7 +42,7 @@ public class PlayCommand extends YoshiCommand { eb.setColor(Color.cyan); eb.setDescription(sb.toString()); sendCustomMessage(eb.build()); - } else { + } else if (context.containsArgument("name")) { String requestedFile = getBestMatch(context.getArgument("name"), getAllFiles()); File file = new File(Resources.getPathToAudioFile(requestedFile)); if (!file.isFile()) { @@ -59,6 +59,9 @@ public class PlayCommand extends YoshiCommand { .sendMessage("Danke, " + context.getEvent().getMessage().getAuthor().getName() + ". Spiele '" + requestedFile + "' in '" + vc.getName() + "' ab").queue(); YoshiBot.getInstance().playSound(file, vc); + } else { + context.getEvent().getMessage().getTextChannel().sendMessage("Blyat, keine Ahnung was du willst. Gib mal " + + "Parameter").queue(); } return true; @@ -114,7 +117,7 @@ public class PlayCommand extends YoshiCommand { return Arrays.stream(Objects.requireNonNull(audioDirectory.listFiles())) .map(File::getName) .filter(name -> name.endsWith(".opus")) - .map(name -> name.substring(0, name.lastIndexOf(".opus"))) - .sorted().collect(Collectors.toList()); + .map(name -> name.substring(0, name.lastIndexOf("."))) + .sorted(String::compareToIgnoreCase).collect(Collectors.toList()); } } diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java b/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java index 28e89b4..cade7e4 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java @@ -96,7 +96,14 @@ public final class Resources { } public static String getPathToAudioFile(String name) { - return audioPath + name + ".opus"; + String filePathWithoutExtension = audioPath + name; + if (new File(filePathWithoutExtension + ".opus").isFile()) { + return filePathWithoutExtension + ".opus"; + } else if (new File(filePathWithoutExtension + ".mp3").isFile()) { + return filePathWithoutExtension + ".mp3"; + } else { + return ""; + } } private static boolean initTemp() {