Browse Source

Tidied up some stuff

pull/2/head
Paul Glaß 4 years ago
parent
commit
9d9e87b784
9 changed files with 168 additions and 214 deletions
  1. +3
    -2
      app/src/main/java/de/yannicpunktdee/yoshibot/audio/AudioSendHandlerImpl.java
  2. +1
    -1
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/JokeCommand.java
  3. +1
    -1
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java
  4. +11
    -12
      app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java
  5. +138
    -181
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java
  6. +1
    -1
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/SauceProvider.java
  7. +0
    -4
      rsc/Config.properties
  8. +6
    -6
      rsc/byebyes.txt
  9. +7
    -6
      rsc/greetings.txt

+ 3
- 2
app/src/main/java/de/yannicpunktdee/yoshibot/audio/AudioSendHandlerImpl.java View File

@ -8,7 +8,7 @@ import java.nio.ByteBuffer;
public class AudioSendHandlerImpl implements AudioSendHandler {
private AudioPlayer audioPlayer;
private final AudioPlayer audioPlayer;
private AudioFrame lastFrame;
@ -16,6 +16,7 @@ public class AudioSendHandlerImpl implements AudioSendHandler {
this.audioPlayer = audioPlayer;
}
//
@Override
public boolean canProvide() {
lastFrame = audioPlayer.provide();
@ -32,7 +33,7 @@ public class AudioSendHandlerImpl implements AudioSendHandler {
*/
@Override
public boolean isOpus() {
return false;
return true;
}
}

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

@ -37,7 +37,7 @@ public class JokeCommand extends YoshiCommand {
@Override public synchronized boolean execute() {
String message = "Jokus";
Random random = new Random();
Random random = YoshiBot.getInstance().getRandom();
int number = random.nextInt(3);
switch(number) {
case 0: message = jokeApi(); break;


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

@ -46,7 +46,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;


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

@ -14,6 +14,7 @@ import de.yannicpunktdee.yoshibot.listeners.DiscordEventListener;
import de.yannicpunktdee.yoshibot.utils.Logger;
import de.yannicpunktdee.yoshibot.utils.Resources;
import de.yannicpunktdee.yoshibot.utils.SauceProvider;
import lombok.Getter;
import lombok.SneakyThrows;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
@ -36,7 +37,7 @@ import java.util.concurrent.TimeUnit;
*
* @author Yannic Link
*/
public class YoshiBot {
public final class YoshiBot {
private CommandLine commandLineThread;
@ -52,16 +53,15 @@ public class YoshiBot {
* LavaPlayer AudioPlayerManager.
*/
public AudioPlayerManager audioPlayerManager;
public Guild guild;
public AudioPlayer audioPlayer;
private static YoshiBot instance = null;
private YoshiBot() {
}
@Getter
private final Random random = new Random();
/**
* Initialisiert alle dynamisch hinzugefügten und statischen Ressourcen. Startet aber nicht den Bot selbst.
@ -81,7 +81,7 @@ public class YoshiBot {
jdaBuilder = JDABuilder.createDefault(Resources.getJda_builder_string());
jdaBuilder.setAutoReconnect(true);
jdaBuilder.addEventListeners(new DiscordEventListener());
audioPlayerManager = new DefaultAudioPlayerManager();
@ -95,11 +95,11 @@ public class YoshiBot {
Logger.log("Konnte nicht auf jda warten. Thread unterbrochen.", Logger.Type.ERROR);
return;
}
jda.awaitReady();
guild = jda.getGuildById(Resources.getGuild_id());
audioPlayer = YoshiBot.getInstance().audioPlayerManager.createPlayer();
audioPlayer = audioPlayerManager.createPlayer();
audioPlayer.addListener(new AudioPlayerListener(guild.getAudioManager()));
guild.getAudioManager().setSendingHandler(new AudioSendHandlerImpl(audioPlayer));
@ -144,8 +144,7 @@ public class YoshiBot {
YoshiBot yoshiBot = YoshiBot.getInstance();
yoshiBot.jda.awaitReady();
List<String> text = Files.readAllLines(new File(Resources.getActivitiesPath()).toPath());
Random rand = new Random();
String activity = text.get(rand.nextInt(text.size()));
String activity = text.get(yoshiBot.random.nextInt(text.size()));
yoshiBot.jda.getPresence().setActivity(Activity.playing(activity));
Logger.log("Setze Aktivität auf " + activity, Logger.Type.INFO);
}


+ 138
- 181
app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java View File

@ -1,7 +1,9 @@
package de.yannicpunktdee.yoshibot.utils;
import de.yannicpunktdee.yoshibot.main.YoshiBot;
import de.yannicpunktdee.yoshibot.utils.Logger.Type;
import lombok.Getter;
import lombok.SneakyThrows;
import org.json.JSONArray;
import org.json.JSONObject;
@ -11,35 +13,51 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;
import java.util.stream.StreamSupport;
public final class Resources {
@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;
@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;
@Getter private static String jda_builder_string;
@Getter private static Long guild_id;
@Getter private static boolean greetings_and_byebyes_on;
@Getter
private static String jda_builder_string;
@Getter
private static Long guild_id;
@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;
@Getter private static String[] filteredTags;
@Getter private static final Map<String, List<String>> feedDetails = new HashMap<>();
@Getter
private static String[] restrict_commands_to_channel;
@Getter
private static String[] filteredTags;
@Getter
private static final Map<String, List<String>> feedDetails = new HashMap<>();
public synchronized static boolean init(String resourcePathArg) {
boolean isOk = initResources(resourcePathArg);
@ -60,139 +78,87 @@ public final class Resources {
return isOk;
}
private static boolean initResources(String resourcePathArg){
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;
resourcePath = (new File((resourcePathArg == null) ? "rsc" : resourcePathArg)).getAbsolutePath()
.replace('\\', '/') + "/";
return verifyExists(resourcePath, File::isDirectory) != null;
}
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);
@SneakyThrows
private static boolean initConfig() {
configPath = verifyExists(resourcePath + "Config.properties", File::isFile);
if (configPath == null) {
return false;
}
propertiesFile = new Properties();
try {
propertiesFile.load(new FileInputStream(configFile));
} catch (IOException e) {
Logger.log("Es ist ein Fehler beim Öffnen der Config.propeties aufgetreten.", Type.ERROR);
return false;
}
Logger.log("Config.properties erfolgreich geladen.", Type.INFO);
propertiesFile.load(new FileInputStream(configPath));
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;
private static boolean initAudio() {
audioPath = verifyExists(resourcePath + "audio/", File::isDirectory);
return audioPath != null;
}
public static String getPathToAudioFile(String name){
public static String getPathToAudioFile(String name) {
return audioPath + name + ".opus";
}
private static boolean initTemp(){
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;
}
File tempDir = new File(System.getProperty("java.io.tmpdir").replace('\\', '/') + "/yoshibot/");
tempPath = verifyExists(tempDir.getAbsolutePath(), File::isDirectory);
if (tempPath != null) {
return true;
}
if (tempDir.mkdir()) {
return verifyExists(tempDir.getAbsolutePath(), File::isDirectory) != null;
} else {
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){
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 initActivities() {
activitiesPath = verifyExists(resourcePath + "activities.txt", File::isFile);
return activitiesPath != null;
}
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 initSauceConfig() {
sauceConfigPath = verifyExists(resourcePath + "sauceConfig.json", File::isFile);
return sauceConfigPath != null;
}
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 initTTS() {
ttsPath = verifyExists(resourcePath + "tts.py", File::isFile);
return ttsPath != null;
}
@SneakyThrows
private static boolean initJdaBuilderString() {
List<String> strings = Files.readAllLines(new File(propertiesFilePath))
jda_builder_string = propertiesFile.getProperty("jda_builder_string");
if (verifyExists(resourcePath + "PrivateJdaBuilderString.txt", File::isFile) == null) {
return false;
}
jda_builder_string = Files.readAllLines(new File(resourcePath + "PrivateJdaBuilderString.txt").toPath()).get(0);
Logger.log("jda_builder_string erfolgreich geladen", Type.INFO);
return true;
}
private static boolean initGuildId(){
private static boolean initGuildId() {
if (!propertiesFile.containsKey("guild_id")) {
Logger.log("Die Config.properties benötigt das Attribut guild_id.", Type.ERROR);
return false;
@ -200,72 +166,52 @@ public final class Resources {
String raw = propertiesFile.getProperty("guild_id");
try {
guild_id = Long.parseLong(raw);
} catch(NumberFormatException e){
} catch (NumberFormatException e) {
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(){
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);
@SneakyThrows
private static boolean initGreetingsAndByebyes() {
greetingsPath = verifyExists(resourcePath + "greetings.txt", File::isFile);
if (greetingsPath == null) {
return false;
}
Logger.log("Byebyes-Datei wurde gefunden.", Type.INFO);
if(propertiesFile.containsKey("greetings_and_byebyes_on")) {
byebyesPath = verifyExists(resourcePath + "byebyes.txt", File::isFile);
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(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);
if (!greetings_and_byebyes_on) return true;
} else greetings_and_byebyes_on = true;
greetings = Files.readAllLines(Paths.get(greetingsPath));
byebyes = Files.readAllLines(Paths.get(byebyesPath));
return true;
}
public static String getRandomGreeting(String name){
if(!greetings_and_byebyes_on) return null;
return greetings.get((new Random()).nextInt(greetings.size())).replace("#", name);
public static String getRandomGreeting(String name) {
return greetings_and_byebyes_on ? String.format(getRandomFrom(greetings), name) : null;
}
public static String getRandomByebye(String name) {
return greetings_and_byebyes_on ? String.format(getRandomFrom(byebyes), name) : null;
}
public static String getRandomByebye(String name){
if(!greetings_and_byebyes_on) return null;
return byebyes.get(new Random().nextInt(byebyes.size())).replace("#", name);
private static String getRandomFrom(List<String> pool) {
return pool.get(YoshiBot.getInstance().getRandom().nextInt(pool.size()));
}
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(
@ -292,9 +238,20 @@ public final class Resources {
} catch (IOException e) {
return false;
}
Logger.log("tags_general_filter erfolgreich geladen", Type.INFO);
return true;
}
private static String verifyExists(String filename, Function<File, Boolean> checkIsValidFile) {
String[] split = filename.split("/");
Logger.log(String.format("Versuche %s zu finden.", split[split.length - 1]), Type.INFO);
if (checkIsValidFile.apply(new File(filename))) {
return filename;
} else {
Logger.log(String.format("%s konnte nicht gefunden werden", filename), Type.ERROR);
return null;
}
}
}

+ 1
- 1
app/src/main/java/de/yannicpunktdee/yoshibot/utils/SauceProvider.java View File

@ -51,7 +51,7 @@ public class SauceProvider {
public static MessageEmbed getRandomSauce(String tags) {
tags = tagsForRest(tags);
tags += "+" + String.join("+", Resources.getFilteredTags());
Random rand = new Random();
Random rand = YoshiBot.getInstance().getRandom();
String url = BASE_URL + "posts?tags=" + String.join("+", tags);
Logger.log("Soße angefordert für tags " + tags, Logger.Type.INFO);
JSONObject baseObj = getParsedSauceData(url);


+ 0
- 4
rsc/Config.properties View File

@ -1,8 +1,4 @@
guild_id=801554100814741524
# audio_source_directory=C:/Users/linky/workspace/Yoshi_Bot_Audio/
restrict_commands_to_channel=bot-muell schrein-auf-den-bot
path_to_tts=/root/yoshibot/rsc/tts.py
path_to_sauce_config=/root/yoshibot/rsc/sauceConfig.json
path_to_activities=/root/yoshibot/rsc/activities.txt
greetings_and_byebyes_on=true
path_to_secret=/home/gits/

+ 6
- 6
rsc/byebyes.txt View File

@ -1,6 +1,6 @@
Verpiss dich #.
Bye bye #.
Tschö #.
Geh kacken #.
# verlässt uns unu.
# ist kurz Halle Peißen.
Verpiss dich %s.
Bye bye %s.
Tschö %s.
Geh kacken %s.
%s verlässt uns unu.
%s ist kurz Halle Peißen.

+ 7
- 6
rsc/greetings.txt View File

@ -1,6 +1,7 @@
Sei gegrüßt Genosse #.
Uwu # ist uns beigetreten.
Gelobt sei #.
Hurra hurra # ist da.
# ist gekommen um uns zu erleuchten.
# was ist deine Weisheit?
Sei gegrüßt Genosse %s.
Uwu %s ist uns beigetreten.
Gelobt sei %s.
Hurra hurra %s ist da.
%s ist gekommen um uns zu erleuchten.
%s was ist deine Weisheit?
Es erscheine: %s!

Loading…
Cancel
Save