From 6748b19c0b4c3eb92895529487059472aba753fa Mon Sep 17 00:00:00 2001 From: Yannic Link Date: Fri, 2 Apr 2021 01:35:42 +0200 Subject: [PATCH] Resources-Klasse verbessert und vereinheitlicht und Usages angepasst. --- .../command/commands/ListCommand.java | 6 +- .../command/commands/PlayCommand.java | 4 +- .../yoshibot/command/commands/SayCommand.java | 12 +- .../listeners/DiscordEventListener.java | 14 +- .../de/yannicpunktdee/yoshibot/main/Main.java | 9 +- .../yoshibot/main/YoshiBot.java | 8 +- .../yoshibot/utils/Resources.java | 322 +++++++++++------- 7 files changed, 219 insertions(+), 156 deletions(-) diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/ListCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/ListCommand.java index d9691dc..40631ae 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/ListCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/ListCommand.java @@ -18,11 +18,7 @@ public class ListCommand extends YoshiCommand { public boolean execute() { if(!super.execute()) return false; - File audioDirectory = new File(Resources.getAudioSourceDirectory()); - if(!audioDirectory.exists()){ - sendMessage("Es existiert kein Audiodatei-Verzeichnis."); - return false; - } + File audioDirectory = new File(Resources.getAudioPath()); StringBuilder sb = new StringBuilder(); for(File f : audioDirectory.listFiles()){ String fName = f.getName(); 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 c4dbd43..048d52a 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 @@ -45,7 +45,7 @@ public class PlayCommand extends YoshiCommand { return false; } - String path = Resources.buildAudioFilePath(context.getArgument("name")); + String path = Resources.getPathToAudioFile(context.getArgument("name")); if((new File(path)).exists()){ sendMessage("Ein Soundeffekt mit diesem Namen existiert bereits."); return false; @@ -79,7 +79,7 @@ public class PlayCommand extends YoshiCommand { vc = context.getEvent().getMember().getVoiceState().getChannel(); } - String fileName = Resources.getAudioFilePath(context.getArgument("name")); + String fileName = Resources.getPathToAudioFile(context.getArgument("name")); if(fileName == null) { context.getEvent().getTextChannel().sendMessage("Audio konnte nicht gefunden werden.").queue(); return false; diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SayCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SayCommand.java index 26c4457..da4342d 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SayCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SayCommand.java @@ -28,20 +28,12 @@ public class SayCommand extends YoshiCommand { } public static String buildTTSAudio(String text){ - String path = Resources.buildTempAudioFilePath(UUID.randomUUID().toString()); - - File test = new File(System.getProperty("java.io.tmpdir") + "/yoshibot"); - if (!test.exists()) { - if (!test.mkdir()) { - Logger.log("Tempordner konnte nicht erstellt werden!", Logger.Type.ERROR); - return null; - } - } + String path = Resources.getPathToTempAudioFile(UUID.randomUUID().toString()); try { ProcessBuilder pb = new ProcessBuilder( "python", - Resources.getTts_path(), + Resources.getTtsPath(), "--text", text, "--lang", diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/listeners/DiscordEventListener.java b/app/src/main/java/de/yannicpunktdee/yoshibot/listeners/DiscordEventListener.java index d01804f..f3fb099 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/listeners/DiscordEventListener.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/listeners/DiscordEventListener.java @@ -3,24 +3,16 @@ package de.yannicpunktdee.yoshibot.listeners; import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; import de.yannicpunktdee.yoshibot.command.commands.PlayCommand; import de.yannicpunktdee.yoshibot.command.commands.SayCommand; -import de.yannicpunktdee.yoshibot.utils.Logger; -import de.yannicpunktdee.yoshibot.utils.Resources; import de.yannicpunktdee.yoshibot.main.YoshiBot; +import de.yannicpunktdee.yoshibot.utils.Resources; import net.dv8tion.jda.api.entities.ChannelType; -import net.dv8tion.jda.api.entities.VoiceChannel; -import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; -import net.dv8tion.jda.api.events.guild.member.GuildMemberLeaveEvent; -import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent; import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent; -import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; import java.util.Arrays; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** * Lauscht auf eingehende Nachrichten und leitet diese an die YoshiBot.executeCommand-Methode weiter, falls es sich um @@ -39,10 +31,10 @@ public class DiscordEventListener extends ListenerAdapter { if (event.getAuthor().isBot()) return; - boolean inErlaubtemKanal = Arrays.stream(Resources.getRestrictCommandsToChannel()) + boolean inErlaubtemKanal = Arrays.stream(Resources.getRestrict_commands_to_channel()) .anyMatch(channel -> channel.equalsIgnoreCase(event.getTextChannel().getName())); - if (Resources.getRestrictCommandsToChannel() != null && !inErlaubtemKanal) + if (Resources.getRestrict_commands_to_channel() != null && !inErlaubtemKanal) return; String raw = event.getMessage().getContentRaw().trim(); diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/main/Main.java b/app/src/main/java/de/yannicpunktdee/yoshibot/main/Main.java index 05883d8..54ecd7c 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/main/Main.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/main/Main.java @@ -1,5 +1,7 @@ package de.yannicpunktdee.yoshibot.main; +import de.yannicpunktdee.yoshibot.utils.Logger; + import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -22,7 +24,12 @@ public class Main { public static void main(String[] args) throws URISyntaxException { YoshiBot yoshiBot = YoshiBot.getInstance(); - yoshiBot.init((args.length > 0)? args[0] : null); + if(!yoshiBot.init((args.length > 0)? args[0] : null)){ + Logger.log("Es ist ein Fehler beim Initialisieren der Ressourcen aufgetreten.", Logger.Type.ERROR); + return; + } + + Logger.log("Ressourcen erfolgreich initialisiert. Starte Yoshi Bot ...", Logger.Type.INFO); try { yoshiBot.start(); diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java b/app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java index 7a4e338..ad32731 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java @@ -66,8 +66,8 @@ public class YoshiBot { /** * Initialisiert alle dynamisch hinzugefügten und statischen Ressourcen. Startet aber nicht den Bot selbst. */ - public void init(String configPath) { - Resources.init(configPath); + public boolean init(String configPath) { + return Resources.init(configPath); } /** @@ -78,7 +78,7 @@ public class YoshiBot { public void start() throws LoginException { System.out.println("Starte YoshiBot."); - jdaBuilder = JDABuilder.createDefault(Resources.getJdaBuilderString()); + jdaBuilder = JDABuilder.createDefault(Resources.getJda_builder_string()); jdaBuilder.setAutoReconnect(true); jdaBuilder.addEventListeners(new DiscordEventListener()); @@ -141,7 +141,7 @@ public class YoshiBot { private static void setRandomActivity() { YoshiBot yoshiBot = YoshiBot.getInstance(); yoshiBot.jda.awaitReady(); - List text = Files.readAllLines(new File(Resources.getActivityPath()).toPath()); + List text = Files.readAllLines(new File(Resources.getActivitiesPath()).toPath()); Random rand = new Random(); String activity = text.get(rand.nextInt(text.size())); yoshiBot.jda.getPresence().setActivity(Activity.playing(activity)); 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 c7c9215..650d1e6 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java @@ -14,84 +14,175 @@ import java.util.*; import java.util.stream.StreamSupport; public final class Resources { - - private static final String default_propertiesFilePath = "rsc/Config.properties"; - private static String propertiesFilePath = default_propertiesFilePath; + + @Getter private static String resourcePath; + @Getter private static String configPath; + @Getter private static String audioPath; + @Getter private static String tempPath; + @Getter private static String activitiesPath; + @Getter private static String greetingsPath; + @Getter private static String byebyesPath; + @Getter private static String sauceConfigPath; + @Getter private static String ttsPath; + private static Properties propertiesFile; - - private static String jda_builder_string; - @Getter - private static Long guild_id; + @Getter private static String jda_builder_string; + + @Getter private static Long guild_id; - private static final boolean default_greetings_and_byebyes_on = true; - @Getter - private static boolean greetings_and_byebyes_on = default_greetings_and_byebyes_on; + @Getter private static boolean greetings_and_byebyes_on; private static List greetings; private static List byebyes; + + @Getter private static String[] restrict_commands_to_channel; - private static final String default_audio_source_directory = "rsc/audio/"; - private static String audio_source_directory = default_audio_source_directory; - - private static final String[] default_restrict_commands_to_channel = null; - private static String[] restrict_commands_to_channel = default_restrict_commands_to_channel; - - @Getter - private static String[] filteredTags; - @Getter - private static final Map> feedDetails = new HashMap<>(); - - @Getter - private static String tts_path; - - @Getter - private static String sauceConfigPath; - - @Getter - private static String activityPath; - + @Getter private static String[] filteredTags; + @Getter private static final Map> feedDetails = new HashMap<>(); + - public synchronized static boolean init(String pathToConfig) { - Logger.log("Lade Config.properties ...", Type.INFO); + public synchronized static boolean init(String resourcePathArg) { + boolean isOk = initResources(resourcePathArg); + if (isOk) isOk = initConfig(); + if (isOk) isOk = initAudio(); + if (isOk) isOk = initTemp(); + if (isOk) isOk = initActivities(); + if (isOk) isOk = initGreetingsAndByebyes(); + if (isOk) isOk = initSauceConfig(); + if (isOk) isOk = initTTS(); + if (isOk) isOk = initJdaBuilderString(); + if (isOk) isOk = initGuildId(); + if (isOk) isOk = initChannelRestrict(); + if (isOk) isOk = initTagFilter(); - if (pathToConfig != null) { - if (!(new File(pathToConfig)).exists()) { - Logger.log("Der in den Argumenten angegebene Pfad zur Config.properties existiert nicht.", Type.ERROR); - return false; - } - propertiesFilePath = pathToConfig; - } else if (!(new File(propertiesFilePath)).exists()) { - Logger.log("Es wurde keine Config-Datei über den Pfad \"" + propertiesFilePath + "\" gefunden.", - Type.ERROR); + if (isOk) Logger.log("Die Konfigurationen wurden erfolgreich geladen.", Type.INFO); + else Logger.log("Die Konfiguration konnte nicht geladen werden", Type.ERROR); + + return isOk; + } + + private static boolean initResources(String resourcePathArg){ + Logger.log("Versuche Resource-Verzeichnis zu finden.", Type.INFO); + + resourcePath = (new File((resourcePathArg == null)? "rsc" : resourcePathArg)).getAbsolutePath() + .replace('\\', '/') + "/"; + + File resDir = new File(resourcePath); + if(!resDir.exists() || !resDir.isDirectory()){ + Logger.log("Das angegebene Resource-Verzeichnis \"" + resourcePath + + "\" wurde nicht gefunden.", Type.WARNING); return false; } - + + Logger.log("Resource-Verzeichnis \"" + resourcePath + "\" wurde gefunden.", Type.INFO); + return true; + } + + private static boolean initConfig(){ + Logger.log("Lade Config.properties.", Type.INFO); + + configPath = resourcePath + "Config.properties"; + + File configFile = new File(configPath); + if(!configFile.exists() || !configFile.isFile()){ + Logger.log("Die Datei Config.properties wurde nicht gefunden.", Type.ERROR); + return false; + } + propertiesFile = new Properties(); try { - propertiesFile.load( - new FileInputStream(propertiesFilePath) - ); - Logger.log("Config-Datei erfolgreich geladen.", Type.INFO); + propertiesFile.load(new FileInputStream(configFile)); } catch (IOException e) { Logger.log("Es ist ein Fehler beim Öffnen der Config.propeties aufgetreten.", Type.ERROR); return false; } - - tts_path = propertiesFile.getProperty("path_to_tts"); - sauceConfigPath = propertiesFile.getProperty("path_to_sauce_config"); - activityPath = propertiesFile.getProperty("path_to_activities"); - - boolean isOk = initJdaBuilderString(); - if (isOk) isOk = initGuildId(); - if (isOk) isOk = initGreetingsAndByebyes(); - if (isOk) isOk = initChannelRestrict(); - if (isOk) isOk = initAudio(); - initTagFilter(); - - if (isOk) Logger.log("Die Konfigurationen wurden erfolgreich geladen.", Type.INFO); - else Logger.log("Die Konfiguration konnte nicht geladen werden", Type.ERROR); - - return isOk; + + Logger.log("Config.properties erfolgreich geladen.", Type.INFO); + return true; + } + + private static boolean initAudio(){ + Logger.log("Versuche Audio-Verzeichnis zu finden.", Type.INFO); + + audioPath = resourcePath + "audio/"; + + File audioDir = new File(audioPath); + if(!audioDir.exists() || !audioDir.isDirectory()){ + Logger.log("Das Audio-Verzeichnis wurde nicht gefunden.", Type.ERROR); + return false; + } + + Logger.log("Audio-Verzeichnis wurde gefunden.", Type.INFO); + return true; + } + public static String getPathToAudioFile(String name){ + return audioPath + name + ".opus"; + } + + private static boolean initTemp(){ + Logger.log("Versuche Temp-Verzeichnis zu finden.", Type.INFO); + + tempPath = System.getProperty("java.io.tmpdir").replace('\\', '/') + "/"; + + File tempDir = new File(tempPath); + if(!tempDir.exists() || !tempDir.isDirectory()){ + Logger.log("Das Temp-Verzeichnis wurde nicht gefunden. Erstelle Verzeichnis.", Type.WARNING); + if(!tempDir.mkdir()){ + Logger.log("Temp-Verzeichnis konnte nicht erstellt werden.", Type.ERROR); + return false; + } + } + + Logger.log("Temp-Verzeichnis wurde gefunden.", Type.INFO); + return true; + } + public static String getPathToTempAudioFile(String name){ + return tempPath + name + ".opus"; + } + + private static boolean initActivities(){ + Logger.log("Versuche Activities-Datei zu finden.", Type.INFO); + + activitiesPath = resourcePath + "activities.txt"; + + File activitiesFile = new File(activitiesPath); + if(!activitiesFile.exists() || !activitiesFile.isFile()){ + Logger.log("Die Activities-Datei wurde nicht gefunden.", Type.ERROR); + return false; + } + + Logger.log("Activities-Datei wurde gefunden.", Type.INFO); + return true; + } + + private static boolean initSauceConfig(){ + Logger.log("Versuche SauceConfig-Datei zu finden.", Type.INFO); + + sauceConfigPath = resourcePath + "sauceConfig.json"; + + File sauceConfigFile = new File(sauceConfigPath); + if(!sauceConfigFile.exists() || !sauceConfigFile.isFile()){ + Logger.log("Die SauceConfig-Datei wurde nicht gefunden.", Type.ERROR); + return false; + } + + Logger.log("SauceConfig-Datei wurde gefunden.", Type.INFO); + return true; + } + + private static boolean initTTS(){ + Logger.log("Versuche TTS-Datei zu finden.", Type.INFO); + + ttsPath = resourcePath + "tts.py"; + + File ttsFile = new File(ttsPath); + if(!ttsFile.exists() || !ttsFile.isFile()){ + Logger.log("Die TTS-Datei wurde nicht gefunden.", Type.ERROR); + return false; + } + + Logger.log("TTS-Datei wurde gefunden.", Type.INFO); + return true; } private static boolean initJdaBuilderString() { @@ -100,11 +191,9 @@ public final class Resources { return false; } jda_builder_string = propertiesFile.getProperty("jda_builder_string"); + Logger.log("jda_builder_string erfolgreich geladen", Type.INFO); return true; } - public static String getJdaBuilderString() { - return jda_builder_string; - } private static boolean initGuildId(){ if (!propertiesFile.containsKey("guild_id")) { @@ -118,19 +207,51 @@ public final class Resources { Logger.log("Die angegebene guild_id ist keine Ganzzahl", Type.ERROR); return false; } + Logger.log("guild_id erfolgreich geladen", Type.INFO); return true; } private static boolean initGreetingsAndByebyes(){ - if(propertiesFile.containsKey("greetings_and_byebyes_on")) - greetings_and_byebyes_on = propertiesFile.getProperty("greetings_and_byebyes_on").equals("true"); + Logger.log("Versuche Greetings-Datei zu finden.", Type.INFO); + + greetingsPath = resourcePath + "greetings.txt"; + + File greetingsFile = new File(greetingsPath); + if(!greetingsFile.exists() || !greetingsFile.isFile()){ + Logger.log("Die Greetings-Datei wurde nicht gefunden.", Type.ERROR); + return false; + } + + Logger.log("Greetings-Datei wurde gefunden.", Type.INFO); + + Logger.log("Versuche Byebyes-Datei zu finden.", Type.INFO); + + byebyesPath = resourcePath + "byebyes.txt"; + + File byebyesFile = new File(byebyesPath); + if(!byebyesFile.exists() || !byebyesFile.isFile()){ + Logger.log("Die Byebyes-Datei wurde nicht gefunden.", Type.ERROR); + return false; + } + + Logger.log("Byebyes-Datei wurde gefunden.", Type.INFO); + + if(propertiesFile.containsKey("greetings_and_byebyes_on")) { + greetings_and_byebyes_on = Boolean.parseBoolean(propertiesFile.getProperty("greetings_and_byebyes_on")); + } + else greetings_and_byebyes_on = true; + if(!greetings_and_byebyes_on) return true; + try { - greetings = Files.readAllLines(Paths.get("rsc/greetings.txt")); - byebyes = Files.readAllLines(Paths.get("rsc/byebyes.txt")); + greetings = Files.readAllLines(Paths.get(greetingsPath)); + byebyes = Files.readAllLines(Paths.get(byebyesPath)); } catch (IOException e) { + Logger.log("Es konnte nicht aus greetings.txt bzw byebyes.txt gelesen werden.", Type.ERROR); return false; } + + Logger.log("greetings_and_byebyes_on erfolgreich geladen", Type.INFO); return true; } public static String getRandomGreeting(String name){ @@ -141,9 +262,14 @@ public final class Resources { if(!greetings_and_byebyes_on) return null; return byebyes.get(new Random().nextInt(byebyes.size())).replace("#", name); } - - - private static void initTagFilter() { + + private static boolean initChannelRestrict() { + if (propertiesFile.containsKey("restrict_commands_to_channel")) + restrict_commands_to_channel = propertiesFile.getProperty("restrict_commands_to_channel").split("\\s+"); + return true; + } + + private static boolean initTagFilter() { try { JSONObject configBase = new JSONObject( String.join("\n", @@ -167,61 +293,11 @@ public final class Resources { feedDetails.put(feedConfig.getString("channel"), tags); } } catch (IOException e) { - e.printStackTrace(); - } - } - - private static boolean initAudio() { - if (propertiesFile.containsKey("audio_source_directory")) { - audio_source_directory = propertiesFile.getProperty("audio_source_directory"); - } else { - Logger.log("Die Config.properties spezifiziert kein audio_source_directory. Lade default.", Type.WARNING); - } - - File file = new File(audio_source_directory); - if (!file.exists() || !file.isDirectory()) { - Logger.log("Das Audio-Verzeichnis wurde nicht gefunden.", Type.ERROR); return false; } - - if (file.listFiles().length < 1) - Logger.log("Das Audio-Verzeichnis ist leer.", Type.WARNING); - - return true; - } - public static String buildAudioFilePath(String name) { - return audio_source_directory + (audio_source_directory.endsWith("/") ? "" : "/") + name + ".opus"; - } - public static String getAudioFilePath(String name) { - name = audio_source_directory + (audio_source_directory.endsWith("/") ? "" : "/") + name + ".opus"; - if ((new File(name)).exists()) return name; - else return null; - } - - public static String getTempAudioFilePath(String name) { - name = audio_source_directory + (audio_source_directory.endsWith("/") ? "" : "/") + "temp/" + name + ".opus"; - if ((new File(name)).exists()) return name; - else return null; - } - - public static String buildTempAudioFilePath(String name) { - return System.getProperty("java.io.tmpdir") + "/yoshibot/" + name; - } - - public static String getAudioSourceDirectory() { - return audio_source_directory; - } - - private static boolean initChannelRestrict() { - if (propertiesFile.containsKey("restrict_commands_to_channel")) - restrict_commands_to_channel = propertiesFile.getProperty("restrict_commands_to_channel").split(" "); + Logger.log("tags_general_filter erfolgreich geladen", Type.INFO); return true; } - public static String[] getRestrictCommandsToChannel() { - return restrict_commands_to_channel; - } - - }