Browse Source

[Added] ability to upload mp3

master
Paul Glaß 3 years ago
parent
commit
04b9edb7ec
4 changed files with 43 additions and 6 deletions
  1. +23
    -0
      app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java
  2. +4
    -0
      app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandContext.java
  3. +8
    -5
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java
  4. +8
    -1
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java

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

@ -9,11 +9,15 @@ import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.VoiceChannel; import net.dv8tion.jda.api.entities.VoiceChannel;
import java.awt.*; import java.awt.*;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
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;
/** /**
* Abstrakte Superklasse für alle Kommandos. * Abstrakte Superklasse für alle Kommandos.
@ -124,6 +128,25 @@ public abstract class YoshiCommand {
return null; 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; return file;
} }


+ 4
- 0
app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandContext.java View File

@ -266,6 +266,10 @@ public class YoshiCommandContext {
return !arguments.isEmpty(); 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. * Prüft, ob alle Key-Werte in der Argumentenliste vorhanden sind.
* *


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

@ -30,11 +30,11 @@ public class PlayCommand extends YoshiCommand {
public boolean execute() { public boolean execute() {
if (!super.execute()) return false; if (!super.execute()) return false;
if (context.containsArguments(new String[]{"add"})) {
if (context.containsArgument("add")) {
File download = downloadAttachmentToFile(Resources.getAudioPath(), context.getArgument("name")); File download = downloadAttachmentToFile(Resources.getAudioPath(), context.getArgument("name"));
if (download.isFile()) sendInfoMessage("Audio erfolgreich hinzugefügt."); if (download.isFile()) sendInfoMessage("Audio erfolgreich hinzugefügt.");
else sendErrorMessage("Audio konnte nicht hinzugefügt werden."); else sendErrorMessage("Audio konnte nicht hinzugefügt werden.");
} else if (context.containsArguments(new String[]{"list"})) {
} else if (context.containsArgument("list")) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
getAllFiles().forEach(name -> sb.append(name).append("\n")); getAllFiles().forEach(name -> sb.append(name).append("\n"));
EmbedBuilder eb = new EmbedBuilder(); EmbedBuilder eb = new EmbedBuilder();
@ -42,7 +42,7 @@ public class PlayCommand extends YoshiCommand {
eb.setColor(Color.cyan); eb.setColor(Color.cyan);
eb.setDescription(sb.toString()); eb.setDescription(sb.toString());
sendCustomMessage(eb.build()); sendCustomMessage(eb.build());
} else {
} else if (context.containsArgument("name")) {
String requestedFile = getBestMatch(context.getArgument("name"), getAllFiles()); String requestedFile = getBestMatch(context.getArgument("name"), getAllFiles());
File file = new File(Resources.getPathToAudioFile(requestedFile)); File file = new File(Resources.getPathToAudioFile(requestedFile));
if (!file.isFile()) { if (!file.isFile()) {
@ -59,6 +59,9 @@ public class PlayCommand extends YoshiCommand {
.sendMessage("Danke, " + context.getEvent().getMessage().getAuthor().getName() + ". Spiele '" + .sendMessage("Danke, " + context.getEvent().getMessage().getAuthor().getName() + ". Spiele '" +
requestedFile + "' in '" + vc.getName() + "' ab").queue(); requestedFile + "' in '" + vc.getName() + "' ab").queue();
YoshiBot.getInstance().playSound(file, vc); YoshiBot.getInstance().playSound(file, vc);
} else {
context.getEvent().getMessage().getTextChannel().sendMessage("Blyat, keine Ahnung was du willst. Gib mal " +
"Parameter").queue();
} }
return true; return true;
@ -114,7 +117,7 @@ public class PlayCommand extends YoshiCommand {
return Arrays.stream(Objects.requireNonNull(audioDirectory.listFiles())) return Arrays.stream(Objects.requireNonNull(audioDirectory.listFiles()))
.map(File::getName) .map(File::getName)
.filter(name -> name.endsWith(".opus")) .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());
} }
} }

+ 8
- 1
app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java View File

@ -96,7 +96,14 @@ public final class Resources {
} }
public static String getPathToAudioFile(String name) { 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() { private static boolean initTemp() {


Loading…
Cancel
Save