Browse Source

Merge branch 'master' of yannicpunktdee.de:yannic/YoshiBot

pull/2/head
Paul Glaß 4 years ago
parent
commit
2fb639a80f
7 changed files with 222 additions and 164 deletions
  1. +1
    -5
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/ListCommand.java
  2. +3
    -3
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java
  3. +3
    -11
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SayCommand.java
  4. +3
    -11
      app/src/main/java/de/yannicpunktdee/yoshibot/listeners/DiscordEventListener.java
  5. +9
    -7
      app/src/main/java/de/yannicpunktdee/yoshibot/main/Main.java
  6. +4
    -4
      app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java
  7. +199
    -123
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java

+ 1
- 5
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/ListCommand.java View File

@ -18,11 +18,7 @@ public class ListCommand extends YoshiCommand {
public boolean execute() { public boolean execute() {
if(!super.execute()) return false; 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(); StringBuilder sb = new StringBuilder();
for(File f : audioDirectory.listFiles()){ for(File f : audioDirectory.listFiles()){
String fName = f.getName(); String fName = f.getName();


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

@ -81,9 +81,9 @@ public class PlayCommand extends YoshiCommand {
} else { } else {
vc = context.getEvent().getMember().getVoiceState().getChannel(); vc = context.getEvent().getMember().getVoiceState().getChannel();
} }
String fileName = Resources.getAudioFilePath(context.getArgument("name"));
if (fileName == null) {
String fileName = Resources.getPathToAudioFile(context.getArgument("name"));
if(fileName == null) {
context.getEvent().getTextChannel().sendMessage("Audio konnte nicht gefunden werden.").queue(); context.getEvent().getTextChannel().sendMessage("Audio konnte nicht gefunden werden.").queue();
return false; return false;
} }


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

@ -28,20 +28,12 @@ public class SayCommand extends YoshiCommand {
} }
public static String buildTTSAudio(String text){ 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 { try {
ProcessBuilder pb = new ProcessBuilder( ProcessBuilder pb = new ProcessBuilder(
"python",
Resources.getTts_path(),
"python3",
Resources.getTtsPath(),
"--text", "--text",
text, text,
"--lang", "--lang",


+ 3
- 11
app/src/main/java/de/yannicpunktdee/yoshibot/listeners/DiscordEventListener.java View File

@ -3,24 +3,16 @@ package de.yannicpunktdee.yoshibot.listeners;
import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; import de.yannicpunktdee.yoshibot.command.YoshiCommandContext;
import de.yannicpunktdee.yoshibot.command.commands.PlayCommand; import de.yannicpunktdee.yoshibot.command.commands.PlayCommand;
import de.yannicpunktdee.yoshibot.command.commands.SayCommand; 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.main.YoshiBot;
import de.yannicpunktdee.yoshibot.utils.Resources;
import net.dv8tion.jda.api.entities.ChannelType; 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.GuildVoiceJoinEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent; 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.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; 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 * 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; 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())); .anyMatch(channel -> channel.equalsIgnoreCase(event.getTextChannel().getName()));
if (Resources.getRestrictCommandsToChannel() != null && !inErlaubtemKanal)
if (Resources.getRestrict_commands_to_channel() != null && !inErlaubtemKanal)
return; return;
String raw = event.getMessage().getContentRaw().trim(); String raw = event.getMessage().getContentRaw().trim();


+ 9
- 7
app/src/main/java/de/yannicpunktdee/yoshibot/main/Main.java View File

@ -1,12 +1,9 @@
package de.yannicpunktdee.yoshibot.main; package de.yannicpunktdee.yoshibot.main;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.UUID;
import de.yannicpunktdee.yoshibot.utils.Logger;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.net.URISyntaxException;
/** /**
* Main-Klasse und Startpunkt für die Bot-Applikation. * Main-Klasse und Startpunkt für die Bot-Applikation.
@ -22,7 +19,12 @@ public class Main {
public static void main(String[] args) throws URISyntaxException { public static void main(String[] args) throws URISyntaxException {
YoshiBot yoshiBot = YoshiBot.getInstance(); 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 { try {
yoshiBot.start(); yoshiBot.start();


+ 4
- 4
app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java View File

@ -66,8 +66,8 @@ public class YoshiBot {
/** /**
* Initialisiert alle dynamisch hinzugefügten und statischen Ressourcen. Startet aber nicht den Bot selbst. * 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);
} }
/** /**
@ -79,7 +79,7 @@ public class YoshiBot {
public void start() throws LoginException { public void start() throws LoginException {
System.out.println("Starte YoshiBot."); System.out.println("Starte YoshiBot.");
jdaBuilder = JDABuilder.createDefault(Resources.getJdaBuilderString());
jdaBuilder = JDABuilder.createDefault(Resources.getJda_builder_string());
jdaBuilder.setAutoReconnect(true); jdaBuilder.setAutoReconnect(true);
jdaBuilder.addEventListeners(new DiscordEventListener()); jdaBuilder.addEventListeners(new DiscordEventListener());
@ -143,7 +143,7 @@ public class YoshiBot {
private static void setRandomActivity() { private static void setRandomActivity() {
YoshiBot yoshiBot = YoshiBot.getInstance(); YoshiBot yoshiBot = YoshiBot.getInstance();
yoshiBot.jda.awaitReady(); yoshiBot.jda.awaitReady();
List<String> text = Files.readAllLines(new File(Resources.getActivityPath()).toPath());
List<String> text = Files.readAllLines(new File(Resources.getActivitiesPath()).toPath());
Random rand = new Random(); Random rand = new Random();
String activity = text.get(rand.nextInt(text.size())); String activity = text.get(rand.nextInt(text.size()));
yoshiBot.jda.getPresence().setActivity(Activity.playing(activity)); yoshiBot.jda.getPresence().setActivity(Activity.playing(activity));


+ 199
- 123
app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java View File

@ -14,94 +14,183 @@ import java.util.*;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
public final class Resources { 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 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> greetings;
private static List<String> byebyes; 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; 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(); propertiesFile = new Properties();
try { try {
propertiesFile.load(
new FileInputStream(propertiesFilePath)
);
Logger.log("Config-Datei erfolgreich geladen.", Type.INFO);
propertiesFile.load(new FileInputStream(configFile));
} catch (IOException e) { } catch (IOException e) {
Logger.log("Es ist ein Fehler beim Öffnen der Config.propeties aufgetreten.", Type.ERROR); Logger.log("Es ist ein Fehler beim Öffnen der Config.propeties aufgetreten.", Type.ERROR);
return false; 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() { private static boolean initJdaBuilderString() {
List<String> strings = Files.readAllLines(new File(propertiesFilePath)) List<String> strings = Files.readAllLines(new File(propertiesFilePath))
jda_builder_string = propertiesFile.getProperty("jda_builder_string"); jda_builder_string = propertiesFile.getProperty("jda_builder_string");
Logger.log("jda_builder_string erfolgreich geladen", Type.INFO);
return true; return true;
} }
public static String getJdaBuilderString() {
return jda_builder_string;
}
private static boolean initGuildId(){ private static boolean initGuildId(){
if (!propertiesFile.containsKey("guild_id")) { if (!propertiesFile.containsKey("guild_id")) {
@ -115,19 +204,51 @@ public final class Resources {
Logger.log("Die angegebene guild_id ist keine Ganzzahl", Type.ERROR); Logger.log("Die angegebene guild_id ist keine Ganzzahl", Type.ERROR);
return false; return false;
} }
Logger.log("guild_id erfolgreich geladen", Type.INFO);
return true; return true;
} }
private static boolean initGreetingsAndByebyes(){ 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; if(!greetings_and_byebyes_on) return true;
try { 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) { } catch (IOException e) {
Logger.log("Es konnte nicht aus greetings.txt bzw byebyes.txt gelesen werden.", Type.ERROR);
return false; return false;
} }
Logger.log("greetings_and_byebyes_on erfolgreich geladen", Type.INFO);
return true; return true;
} }
public static String getRandomGreeting(String name){ public static String getRandomGreeting(String name){
@ -138,9 +259,14 @@ public final class Resources {
if(!greetings_and_byebyes_on) return null; if(!greetings_and_byebyes_on) return null;
return byebyes.get(new Random().nextInt(byebyes.size())).replace("#", name); 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 { try {
JSONObject configBase = new JSONObject( JSONObject configBase = new JSONObject(
String.join("\n", String.join("\n",
@ -164,61 +290,11 @@ public final class Resources {
feedDetails.put(feedConfig.getString("channel"), tags); feedDetails.put(feedConfig.getString("channel"), tags);
} }
} catch (IOException e) { } 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; 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; return true;
} }
public static String[] getRestrictCommandsToChannel() {
return restrict_commands_to_channel;
}
} }

Loading…
Cancel
Save