Browse Source

Try making sauce more stable

master
Paul Glaß 4 years ago
parent
commit
f4b400f50c
2 changed files with 24 additions and 21 deletions
  1. +2
    -3
      app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java
  2. +22
    -18
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/SauceProvider.java

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

@ -117,7 +117,7 @@ public final class YoshiBot {
commandLineThread = new CommandLine();
commandLineThread.start();
new SauceProvider(300);
SauceProvider.init(300);
//RedditProvider.init();
@ -183,8 +183,7 @@ public final class YoshiBot {
}
}
if (maxMembers < 1) joinVoiceChannel(null);
else if (maxMembers > 0 && maxVoiceChannel != null)
joinVoiceChannel(maxVoiceChannel);
else joinVoiceChannel(maxVoiceChannel);
}
public boolean playSound(File file, VoiceChannel vc) {


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

@ -14,26 +14,30 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class SauceProvider {
public final class SauceProvider {
private static final String BASE_URL = "https://r34-json.herokuapp.com/";
private int lastKnownSauce = -1;
private static int lastKnownSauce = -1;
private boolean isSauceInit = false;
private static boolean isSauceInit = false;
private static MessageEmbed notFoundEmbed = null;
public SauceProvider(int timer) {
lastKnownSauce = this.getNewestIndex();
ScheduledExecutorService sauceScheduler = Executors.newScheduledThreadPool(4);
sauceScheduler.scheduleAtFixedRate(this::provideSauce, 0, timer, TimeUnit.SECONDS);
new Thread(this::initSauceProviding).start();
private static final ScheduledExecutorService sauceScheduler = Executors.newScheduledThreadPool(4);
public static void init(int secondsPerTime) {
init(secondsPerTime, TimeUnit.SECONDS);
}
public static void init(int timer, TimeUnit timeUnit) {
init(timer, timeUnit, getNewestIndex());
}
public SauceProvider(int timer, int lastKnownSauce) {
this(timer);
this.lastKnownSauce = lastKnownSauce;
public static void init(int timer, TimeUnit timeUnit, int lastKnownSauce) {
sauceScheduler.scheduleAtFixedRate(SauceProvider::provideSauce, 0, timer, timeUnit);
SauceProvider.lastKnownSauce = lastKnownSauce;
new Thread(SauceProvider::initSauceProviding).start();
}
public static MessageEmbed getSauce(int index) {
@ -76,8 +80,8 @@ public class SauceProvider {
return eb.build();
}
private void provideSauce() {
if (!isSauceInit) return;
private static void provideSauce() {
if (!isSauceInit || lastKnownSauce < 0) return;
for (Map.Entry<String, List<String>> feed : Resources.getFeedDetails().entrySet()) {
String url = BASE_URL + "posts?tags=" + String.join("+", feed.getValue())
+ "+" + String.join("+", Resources.getFilteredTags());
@ -106,10 +110,10 @@ public class SauceProvider {
}
}
lastKnownSauce = this.getNewestIndex();
lastKnownSauce = getNewestIndex();
}
private void initSauceProviding() {
private static void initSauceProviding() {
YoshiBot yoshiBot = YoshiBot.getInstance();
try {
yoshiBot.jda.awaitReady();
@ -119,15 +123,15 @@ public class SauceProvider {
for (Map.Entry<String, List<String>> entry : Resources.getFeedDetails().entrySet()) {
List<TextChannel> channels = yoshiBot.jda.getTextChannelsByName(entry.getKey(), true);
if (channels.size() > 0) {
this.isSauceInit = true;
this.provideSauce();
isSauceInit = true;
provideSauce();
} else {
Logger.logError("Konnte keine Kanaäle finden für die Soße");
}
}
}
private int getNewestIndex() {
private static int getNewestIndex() {
JSONObject result = getParsedSauceData("https://r34-json.herokuapp.com/posts?limit=1&q=index");
int id = result.getJSONArray("posts").getJSONObject(0).getInt("id");
Logger.logDebug("Neuste Soßen-ID: " + id);


Loading…
Cancel
Save