|
|
@ -14,94 +14,183 @@ 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<String> greetings; |
|
|
|
private static List<String> 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<String, List<String>> 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<String, List<String>> 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('\\', '/') + "/yoshibot/"; |
|
|
|
|
|
|
|
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() { |
|
|
|
List<String> strings = Files.readAllLines(new File(propertiesFilePath)) |
|
|
|
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")) { |
|
|
@ -115,19 +204,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){ |
|
|
@ -138,9 +259,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", |
|
|
@ -164,61 +290,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; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |