Browse Source

Jo, bissel gefixt

pull/2/head
Paul Glaß 4 years ago
parent
commit
5590356e68
4 changed files with 55 additions and 49 deletions
  1. +7
    -6
      app/src/main/java/de/yannicpunktdee/yoshibot/audio/AudioLoadResultHandlerImpl.java
  2. +18
    -19
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java
  3. +23
    -23
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SayCommand.java
  4. +7
    -1
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java

+ 7
- 6
app/src/main/java/de/yannicpunktdee/yoshibot/audio/AudioLoadResultHandlerImpl.java View File

@ -6,27 +6,28 @@ import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import de.yannicpunktdee.yoshibot.main.YoshiBot;
import de.yannicpunktdee.yoshibot.utils.Logger;
public class AudioLoadResultHandlerImpl implements AudioLoadResultHandler {
@Override
public void trackLoaded(AudioTrack track) {
YoshiBot.getInstance().audioPlayer.playTrack(track);
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
System.out.println("Kann aktuell noch keine Playlists abspielen");
}
@Override
public void noMatches() {
System.out.println("Nothing found");
Logger.log("Nothing found", Logger.Type.INFO);
}
@Override
public void loadFailed(FriendlyException exception) {
System.out.println("Loading failed");
}
}

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

@ -1,22 +1,20 @@
package de.yannicpunktdee.yoshibot.command.commands;
import java.awt.*;
import java.io.File;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import de.yannicpunktdee.yoshibot.audio.AudioLoadResultHandlerImpl;
import de.yannicpunktdee.yoshibot.command.YoshiCommand;
import de.yannicpunktdee.yoshibot.command.YoshiCommandContext;
import de.yannicpunktdee.yoshibot.utils.Resources;
import de.yannicpunktdee.yoshibot.main.YoshiBot;
import lombok.NonNull;
import de.yannicpunktdee.yoshibot.utils.Resources;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.VoiceChannel;
import java.awt.*;
import java.io.File;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class PlayCommand extends YoshiCommand {
protected final String[] requiredArguments = new String[]{"name"};
@ -27,6 +25,8 @@ public class PlayCommand extends YoshiCommand {
}
public static boolean play(String fileName, VoiceChannel vc) {
if (!new File(fileName).isFile()) return false;
YoshiBot yoshiBot = YoshiBot.getInstance();
vc.getGuild().getAudioManager().openAudioConnection(vc);
@ -65,12 +65,12 @@ public class PlayCommand extends YoshiCommand {
sendMessage("Die Sounddatei konnte nicht ordnungsgemäß erstellt werden.");
return false;
}
} else if (context.containsArguments(new String[]{"list"})){
} else if (context.containsArguments(new String[]{"list"})) {
File audioDirectory = new File(Resources.getAudioPath());
StringBuilder sb = new StringBuilder();
for(File f : audioDirectory.listFiles()){
for (File f : audioDirectory.listFiles()) {
String fName = f.getName();
if(!fName.endsWith(".opus")) continue;
if (!fName.endsWith(".opus")) continue;
sb.append(fName.substring(0, fName.lastIndexOf(".opus")));
sb.append("\n");
}
@ -79,7 +79,7 @@ public class PlayCommand extends YoshiCommand {
eb.setColor(Color.blue);
eb.setDescription(sb.toString());
sendMessage(eb.build());
}else {
} else {
VoiceChannel vc;
if (context.getEvent().getMember() == null ||
!context.getEvent().getMember().getVoiceState().inVoiceChannel()) {
@ -97,14 +97,13 @@ public class PlayCommand extends YoshiCommand {
} else {
vc = context.getEvent().getMember().getVoiceState().getChannel();
}
String fileName = Resources.getPathToAudioFile(context.getArgument("name"));
if(fileName == null) {
context.getEvent().getTextChannel().sendMessage("Audio konnte nicht gefunden werden.").queue();
return false;
}
play(fileName, vc);
if (!play(fileName, vc)) {
sendMessage(String.format("Konnte keine Audiodatei namens '%s.opus' finden!",
context.getArgument("name")));
}
}
return true;


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

@ -15,20 +15,20 @@ import java.util.List;
import java.util.UUID;
public class SayCommand extends YoshiCommand {
public static String resourceToDelete = null;
protected final String[] requiredArguments = {"text", "channel"};
public SayCommand(YoshiCommandContext context) {
super(context);
}
public static String buildTTSAudio(String text){
public static String buildTTSAudio(String text) {
String path = Resources.getPathToTempAudioFile(UUID.randomUUID().toString());
try {
ProcessBuilder pb = new ProcessBuilder(
"python3",
@ -39,7 +39,7 @@ public class SayCommand extends YoshiCommand {
"de",
"--out",
path);
Process p = pb.start();
BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuilder builder = new StringBuilder();
@ -50,36 +50,36 @@ public class SayCommand extends YoshiCommand {
if (builder.toString().length() > 0) {
Logger.log(builder.toString(), Logger.Type.ERROR);
}
int exitCode = p.waitFor();
if(resourceToDelete != null)
synchronized (resourceToDelete){
if (resourceToDelete != null)
synchronized (resourceToDelete) {
resourceToDelete = path;
}
} catch (IOException | InterruptedException e) {
} catch (IOException e) {
e.printStackTrace();
return null;
}
return path;
}
@Override
public boolean execute() {
if(!super.execute()) return false;
if (!super.execute()) return false;
String path = buildTTSAudio(context.getArgument("text"));
List<VoiceChannel> channels = YoshiBot.getInstance().jda.getVoiceChannelsByName(context.getArgument("channel"), true);
if(!(channels.size() > 0)) {
List<VoiceChannel> channels = YoshiBot.getInstance().jda
.getVoiceChannelsByName(context.getArgument("channel"), true);
if (!(channels.size() > 0)) {
context.getEvent().getTextChannel().sendMessage("Der Kanalname konnte nicht gefunden werden.").queue();
return false;
}
VoiceChannel vc = channels.get(0);
YoshiBot.getInstance().audioPlayerManager.loadItem(path, new AudioLoadResultHandlerImpl());
vc.getGuild().getAudioManager().openAudioConnection(vc);
return false;
}
}

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

@ -137,6 +137,12 @@ public final class Resources {
}
public static String getPathToTempAudioFile(String name) {
File tempDirFile = new File(tempPath);
if (!tempDirFile.isDirectory()) {
if (!new File(tempPath).mkdir()) {
Logger.log("TempPath konnte nicht erstellt werden", Type.ERROR);
}
}
return tempPath + name + ".opus";
}
@ -165,7 +171,7 @@ public final class Resources {
return true;
}
private static boolean initPatPngPath(){
private static boolean initPatPngPath() {
patPngPath = verifyExists(resourcePath + "pats/", File::isDirectory);
return patPngPath != null;
}


Loading…
Cancel
Save