Browse Source

Resource-Handling angepasst.

paul
Yannic Link 4 years ago
parent
commit
f8d008d06e
6 changed files with 94 additions and 31 deletions
  1. +2
    -2
      .gitignore
  2. +5
    -3
      README.md
  3. +1
    -2
      app/src/main/java/de/yannicpunktdee/yoshibot/main/Main.java
  4. +55
    -24
      app/src/main/java/de/yannicpunktdee/yoshibot/main/Resources.java
  5. +21
    -0
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/Logger.java
  6. +10
    -0
      rsc/Ordnerstruktur.txt

+ 2
- 2
.gitignore View File

@ -192,5 +192,5 @@ gradle-app.setting
.classpath .classpath
app/src/main/resources/*
!app/src/main/resources/.gitkeep
rsc/*
!rsc/Ordnerstruktur.txt

+ 5
- 3
README.md View File

@ -4,7 +4,8 @@ Ein in Java geschriebener Discordbot, der lustige Sachen kann.
##Einrichtung ##Einrichtung
Ordner app/src/main/resources anlegen und darein die Config.properties anlegen. Diese sollte folgende Werte enthalten:
Ordner rsc wie in Ordnerstruktur.txt beschrieben strukturieren und Config.properties anlegen. Diese sollte folgende
Werte enthalten:
- jda_builder_string -> Client Secret - jda_builder_string -> Client Secret
- audio_source_directory -> Verzeichnis, in dem die Audio-Dateien liegen, auch unter Windows mit / anstatt \ - audio_source_directory -> Verzeichnis, in dem die Audio-Dateien liegen, auch unter Windows mit / anstatt \
@ -14,5 +15,6 @@ Ordner app/src/main/resources anlegen und darein die Config.properties anlegen.
##Export ##Export
Zum Exportieren der Applikation führe den Befehl "gradlew clean build" im Root Verzeichnis aus. Die fertige Jar liegt Zum Exportieren der Applikation führe den Befehl "gradlew clean build" im Root Verzeichnis aus. Die fertige Jar liegt
in "app/build/libs". Füge dieser noch die Config.properties hinzu und starte sie mit "java-jar app.jar" aus. Achte auch
hierbei darauf, dass der Audio-Datei-Pfad existiert.
in "app/build/libs". Füge dieser noch im selben Verzeichnis den rsc-Ordner mitsamt Inhalt hinzu. Für Pfadangaben in der
Config.properties achte darauf, dass diese Ordner auch existieren.
Starte die Applikation mit "java-jar app.jar".

+ 1
- 2
app/src/main/java/de/yannicpunktdee/yoshibot/main/Main.java View File

@ -1,5 +1,6 @@
package de.yannicpunktdee.yoshibot.main; package de.yannicpunktdee.yoshibot.main;
import java.io.File;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
@ -15,8 +16,6 @@ public class Main {
* @throws URISyntaxException * @throws URISyntaxException
*/ */
public static void main(String[] args) throws URISyntaxException { public static void main(String[] args) throws URISyntaxException {
System.out.println("Starte Applikation.\n");
YoshiBot.init((args.length > 0)? args[0] : null); YoshiBot.init((args.length > 0)? args[0] : null);
try { try {


+ 55
- 24
app/src/main/java/de/yannicpunktdee/yoshibot/main/Resources.java View File

@ -1,12 +1,16 @@
package de.yannicpunktdee.yoshibot.main; package de.yannicpunktdee.yoshibot.main;
import de.yannicpunktdee.yoshibot.utils.Logger;
import de.yannicpunktdee.yoshibot.utils.Logger.Type;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Properties; import java.util.Properties;
public class Resources { public class Resources {
private static final String default_propertiesFilePath = "Config.properties";
private static final String default_propertiesFilePath = "./rsc/Config.properties";
private static String propertiesFilePath = default_propertiesFilePath; private static String propertiesFilePath = default_propertiesFilePath;
private static Properties propertiesFile; private static Properties propertiesFile;
@ -19,45 +23,71 @@ public class Resources {
private static String restrict_commands_to_channel = default_restrict_commands_to_channel; private static String restrict_commands_to_channel = default_restrict_commands_to_channel;
public synchronized static void init(String pathToConfig) {
if(pathToConfig != null) propertiesFilePath = pathToConfig;
if(!(new File(propertiesFilePath)).exists()) propertiesFilePath = default_propertiesFilePath;
public synchronized static boolean init(String pathToConfig) {
Logger.log("Lade Config.properties ...", Type.INFO);
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);
return false;
}
propertiesFile = new Properties(); propertiesFile = new Properties();
try { try {
propertiesFile.load(Resources.class.getClassLoader().getResourceAsStream(propertiesFilePath));
System.out.println("Properties-Datei erfolgreich geladen.");
propertiesFile.load(
new FileInputStream(default_propertiesFilePath)
);
Logger.log("Config-Datei erfolgreich geladen.", Type.INFO);
} catch (IOException e) { } catch (IOException e) {
System.err.println("Es wurde keine Config-Datei gefunden. Benutze Standards.");
return;
Logger.log("Es ist ein Fehler beim Öffnen der Config.propeties aufgetreten.", Type.ERROR);
return false;
} }
initJdaBuilderString();
initAudio();
initChannelRestrict();
boolean isOk = true;
if(isOk) isOk &= initJdaBuilderString();
if(isOk) isOk &= initAudio();
if(isOk) isOk &= initChannelRestrict();
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 void initJdaBuilderString() {
private static boolean initJdaBuilderString() {
if(!propertiesFile.containsKey("jda_builder_string")) { if(!propertiesFile.containsKey("jda_builder_string")) {
System.err.println("Es wurde kein jda_builder_string gefunden.");
YoshiBot.stop();
} else jda_builder_string = propertiesFile.getProperty("jda_builder_string");
Logger.log("Die Config.properties benötigt das Attribut jda_builder_string.", Type.ERROR);
return false;
}
jda_builder_string = propertiesFile.getProperty("jda_builder_string");
return true;
} }
public static String getJdaBuilderString() { public static String getJdaBuilderString() {
return jda_builder_string; return jda_builder_string;
} }
private static void initAudio() {
if(!propertiesFile.containsKey("audio_source_directory")) return;
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);
}
String dir = propertiesFile.getProperty("audio_source_directory");
File file = new File(dir);
File file = new File(audio_source_directory);
if(!file.exists() || !file.isDirectory()){ if(!file.exists() || !file.isDirectory()){
System.err.println("Das Audio-Verzeichnis wurde nicht gefunden");
return;
Logger.log("Das Audio-Verzeichnis wurde nicht gefunden.", Type.ERROR);
return false;
} }
audio_source_directory = dir;
if(file.listFiles().length < 1)
Logger.log("Das Audio-Verzeichnis ist leer.", Type.WARNING);
return true;
} }
public static String getAudioFilePath(String name) { public static String getAudioFilePath(String name) {
name = audio_source_directory + (audio_source_directory.endsWith("/")? "" : "/") + name + ".opus"; name = audio_source_directory + (audio_source_directory.endsWith("/")? "" : "/") + name + ".opus";
@ -68,9 +98,10 @@ public class Resources {
return audio_source_directory; return audio_source_directory;
} }
private static void initChannelRestrict() {
private static boolean initChannelRestrict() {
if(propertiesFile.containsKey("restrict_commands_to_channel")) if(propertiesFile.containsKey("restrict_commands_to_channel"))
restrict_commands_to_channel = propertiesFile.getProperty("restrict_commands_to_channel"); restrict_commands_to_channel = propertiesFile.getProperty("restrict_commands_to_channel");
return true;
} }
public static String getRestrictCommandsToChannel() { public static String getRestrictCommandsToChannel() {
return restrict_commands_to_channel; return restrict_commands_to_channel;


+ 21
- 0
app/src/main/java/de/yannicpunktdee/yoshibot/utils/Logger.java View File

@ -0,0 +1,21 @@
package de.yannicpunktdee.yoshibot.utils;
public class Logger {
public static enum Type {
INFO,
WARNING,
ERROR
}
public static void log(String message, Type type){
StringBuilder sb = new StringBuilder();
sb.append("[Yoshi::");
sb.append(((type == null)? Type.INFO : type).toString());
sb.append("] ");
sb.append(message);
System.out.println(sb.toString());
}
}

+ 10
- 0
rsc/Ordnerstruktur.txt View File

@ -0,0 +1,10 @@
rsc
|-- .gitkeep
|-- Ordnerstruktur.txt
|-- Config.properties
|-- audio
|-- temp
|-- temp_1.opus
|-- temp_2.opus
|-- audio_1.opus
|-- audio_2.opus

Loading…
Cancel
Save