|
|
@ -1,12 +1,20 @@ |
|
|
|
package de.yannicpunktdee.yoshibot.command; |
|
|
|
|
|
|
|
import de.yannicpunktdee.yoshibot.audio.AudioLoadResultHandlerImpl; |
|
|
|
import de.yannicpunktdee.yoshibot.command.commands.SayCommand; |
|
|
|
import de.yannicpunktdee.yoshibot.main.YoshiBot; |
|
|
|
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 net.dv8tion.jda.api.entities.VoiceChannel; |
|
|
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; |
|
|
|
|
|
|
|
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; |
|
|
@ -14,6 +22,7 @@ import java.util.concurrent.ExecutionException; |
|
|
|
|
|
|
|
/** |
|
|
|
* Abstrakte Superklasse für alle Kommandos. |
|
|
|
* |
|
|
|
* @author Yannic Link |
|
|
|
*/ |
|
|
|
public abstract class YoshiCommand { |
|
|
@ -25,53 +34,93 @@ public abstract class YoshiCommand { |
|
|
|
*/ |
|
|
|
protected YoshiCommandContext context; |
|
|
|
|
|
|
|
public static String resourceToDelete = null; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Erzeugt ein neues Kommando, führt es aber noch nicht aus. Es wird ermittelt, ob die Argumentenkombination |
|
|
|
* valide ist und das isOk-Flag gesetzt. Im Fehlerfall wird eine Fehleremeldung spezifiziert. |
|
|
|
* Erzeugt ein neues Kommando, führt es aber noch nicht aus. Es wird ermittelt, ob die Argumentenkombination valide |
|
|
|
* ist und das isOk-Flag gesetzt. Im Fehlerfall wird eine Fehleremeldung spezifiziert. |
|
|
|
* |
|
|
|
* @param context Der Kontext mit dem das Kommando aufgerufen wurde. |
|
|
|
*/ |
|
|
|
public YoshiCommand(YoshiCommandContext context) { |
|
|
|
this.context = context; |
|
|
|
} |
|
|
|
|
|
|
|
public static String buildTTSAudio(String text) { |
|
|
|
String path = Resources.getPathToTempAudioFile(UUID.randomUUID().toString()); |
|
|
|
|
|
|
|
try { |
|
|
|
ProcessBuilder pb = new ProcessBuilder( |
|
|
|
"python3", |
|
|
|
Resources.getTtsPath(), |
|
|
|
"--text", |
|
|
|
text, |
|
|
|
"--lang", |
|
|
|
"de", |
|
|
|
"--out", |
|
|
|
path); |
|
|
|
|
|
|
|
Process p = pb.start(); |
|
|
|
BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream())); |
|
|
|
StringBuilder builder = new StringBuilder(); |
|
|
|
String line; |
|
|
|
while ((line = errorReader.readLine()) != null) { |
|
|
|
builder.append(line).append("\n"); |
|
|
|
} |
|
|
|
if (builder.toString().length() > 0) { |
|
|
|
Logger.logError(builder.toString()); |
|
|
|
} |
|
|
|
if (resourceToDelete != null) |
|
|
|
synchronized (resourceToDelete) { |
|
|
|
resourceToDelete = path; |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
return path; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Führt das Kommando aus. |
|
|
|
* |
|
|
|
* @return True, wenn Ausführung erfolgreich. False, wenn Ausführung fehlgeschlagen. Fehlermeldung wird in |
|
|
|
* errorMessage spezifiziert. |
|
|
|
*/ |
|
|
|
public boolean execute() { |
|
|
|
if(!context.containsArguments(requiredArguments)){ |
|
|
|
if (!context.containsArguments(requiredArguments)) { |
|
|
|
sendMessage("Fehlende Argumente"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
protected void sendMessage(String message) { |
|
|
|
protected final void sendMessage(String message) { |
|
|
|
context.getEvent().getTextChannel().sendMessage(message).queue(); |
|
|
|
} |
|
|
|
|
|
|
|
public void sendMessage(MessageEmbed messageEmbed){ |
|
|
|
|
|
|
|
protected final void sendMessage(MessageEmbed messageEmbed) { |
|
|
|
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()){ |
|
|
|
|
|
|
|
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){ |
|
|
|
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 -> { |
|
|
@ -84,12 +133,27 @@ public abstract class YoshiCommand { |
|
|
|
sendMessage("Ein Anhang konnte nicht gedownloaded werden."); |
|
|
|
return null; |
|
|
|
} |
|
|
|
if(!file.exists()) { |
|
|
|
if (!file.exists()) { |
|
|
|
sendMessage("Ein Anhang konnte nicht gedownloaded werden."); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return file; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean sayToChannel(String path, String channel, MessageReceivedEvent event) { |
|
|
|
List<VoiceChannel> channels = YoshiBot.getInstance().jda |
|
|
|
.getVoiceChannelsByName(channel, true); |
|
|
|
if (!(channels.size() > 0)) { |
|
|
|
event.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 true; |
|
|
|
} |
|
|
|
|
|
|
|
} |