Cleaning up bot site updaters.

This commit is contained in:
NovaFox161
2018-02-13 13:23:04 -06:00
parent 61200a22db
commit 50bf6555f6
9 changed files with 76 additions and 36 deletions

View File

@@ -39,6 +39,12 @@
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<!--discordbots.net API library-->
<dependency>
<groupId>com.github.DiscordBotList</groupId>
<artifactId>Java-Wrapper</artifactId>
<version>v1.0</version>
</dependency>
<!--Google Calendar API-->
<dependency>
<groupId>com.google.api-client</groupId>

View File

@@ -6,7 +6,7 @@ import com.cloudcraftgaming.discal.api.network.google.Authorization;
import com.cloudcraftgaming.discal.api.object.BotSettings;
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
import com.cloudcraftgaming.discal.bot.internal.consolecommand.ConsoleCommandExecutor;
import com.cloudcraftgaming.discal.bot.internal.network.discordpw.UpdateListData;
import com.cloudcraftgaming.discal.bot.internal.network.discordpw.UpdateDisPwData;
import com.cloudcraftgaming.discal.bot.listeners.ReadyEventListener;
import com.cloudcraftgaming.discal.bot.module.command.*;
import com.cloudcraftgaming.discal.web.utils.SparkUtils;
@@ -41,7 +41,7 @@ public class Main {
if (client == null)
throw new NullPointerException("Failed to log in! Client cannot be null!");
UpdateListData.init();
UpdateDisPwData.init();
Authorization.getAuth().init();

View File

@@ -10,7 +10,7 @@ import java.util.Properties;
public enum BotSettings {
SQL_HOST, SQL_USER, SQL_PASSWORD,
SQL_DB, SQL_PORT, SQL_PREFIX, TOKEN, SECRET, ID,
LANG_PATH, PW_TOKEN, GOOGLE_CLIENT_ID,
LANG_PATH, PW_TOKEN, DBO_TOKEN, GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET, RUN_API, PORT;
private String val;

View File

@@ -0,0 +1,38 @@
package com.cloudcraftgaming.discal.bot.internal.network.discordbots;
import com.cloudcraftgaming.discal.Main;
import com.cloudcraftgaming.discal.api.object.BotSettings;
import org.discordbots.api.client.DiscordBotListAPI;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Nova Fox on 2/13/18.
* Website: www.cloudcraftgaming.com
* For Project: DisCal-Discord-Bot
*/
public class UpdateDisBotData {
private static DiscordBotListAPI api;
private static Timer timer;
public static void init() {
api = new DiscordBotListAPI.Builder().token(BotSettings.DBO_TOKEN.get()).build();
timer = new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
updateStats();
}
}, 60 * 60 * 1000);
}
public static void shutdown() {
timer.cancel();
}
private static void updateStats() {
api.setStats(BotSettings.ID.get(), Main.client.getGuilds().size());
}
}

View File

@@ -1,18 +0,0 @@
package com.cloudcraftgaming.discal.bot.internal.network.discordpw;
import java.util.TimerTask;
/**
* Created by Nova Fox on 3/28/2017.
* Website: www.cloudcraftgaming.com
* For Project: DisCal
*/
public class TimedUpdate extends TimerTask {
/**
* The action to be performed by this timer task.
*/
@Override
public void run() {
UpdateListData.updateSiteBotMeta();
}
}

View File

@@ -8,34 +8,43 @@ import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import org.json.JSONObject;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Nova Fox on 1/13/2017.
* Website: www.cloudcraftgaming.com
* For Project: DisCal
*/
public class UpdateListData {
public class UpdateDisPwData {
private static Timer timer;
/**
* Initiates the data updater with a valid token.
*
*/
public static void init() {
timer = new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
updateSiteBotMeta();
}
}, 60 * 60 * 1000);
}
/**
* Updates the site meta on bots.discord.pw
*/
public static void updateSiteBotMeta() {
public static void shutdown() {
timer.cancel();
}
private static void updateSiteBotMeta() {
try {
Integer serverCount = Main.client.getGuilds().size();
JSONObject json = new JSONObject().put("server_count", serverCount);
//noinspection unused
HttpResponse<JsonNode> response = Unirest.post("https://bots.discord.pw/api/bots/265523588918935552/stats").header("Authorization", BotSettings.PW_TOKEN.get()).header("Content-Type", "application/json").body(json).asJson();
} catch (Exception e) {
//Handle issue.
System.out.println("Failed to update Discord PW list metadata!");
ExceptionHandler.sendException(null, "Failed to update Discord PW list.", e, UpdateListData.class);
ExceptionHandler.sendException(null, "Failed to update Discord PW list.", e, UpdateDisPwData.class);
e.printStackTrace();
}
}

View File

@@ -3,6 +3,8 @@ package com.cloudcraftgaming.discal.bot.internal.service;
import com.cloudcraftgaming.discal.Main;
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
import com.cloudcraftgaming.discal.bot.internal.network.discordbots.UpdateDisBotData;
import com.cloudcraftgaming.discal.bot.internal.network.discordpw.UpdateDisPwData;
import sx.blah.discord.util.DiscordException;
import java.io.File;
@@ -106,6 +108,8 @@ public class ApplicationHandler {
//No need to print, exiting anyway.
e.printStackTrace();
}
UpdateDisBotData.shutdown();
UpdateDisPwData.shutdown();
TimeManager.getManager().shutdown();
DatabaseManager.getManager().disconnectFromMySQL();
System.exit(0);

View File

@@ -1,6 +1,5 @@
package com.cloudcraftgaming.discal.bot.internal.service;
import com.cloudcraftgaming.discal.bot.internal.network.discordpw.TimedUpdate;
import com.cloudcraftgaming.discal.bot.module.announcement.AnnouncementTask;
import com.cloudcraftgaming.discal.bot.module.misc.StatusChanger;
@@ -38,7 +37,6 @@ public class TimeManager {
public void init() {
Timer timer = new Timer(true);
timer.schedule(new StatusChanger(), 10 * 1000, 10 * 1000);
timer.schedule(new TimedUpdate(), 60 * 60 * 1000, 60 * 60 * 1000);
timers.add(timer);
@@ -53,7 +51,7 @@ public class TimeManager {
/**
* Gracefully shuts down the TimeManager and exits all timer threads preventing errors.
*/
public void shutdown() {
void shutdown() {
for (Timer t : timers) {
t.cancel();
}

View File

@@ -1,7 +1,8 @@
package com.cloudcraftgaming.discal.bot.listeners;
import com.cloudcraftgaming.discal.api.message.MessageManager;
import com.cloudcraftgaming.discal.bot.internal.network.discordpw.UpdateListData;
import com.cloudcraftgaming.discal.bot.internal.network.discordbots.UpdateDisBotData;
import com.cloudcraftgaming.discal.bot.internal.network.discordpw.UpdateDisPwData;
import com.cloudcraftgaming.discal.bot.internal.service.TimeManager;
import sx.blah.discord.api.events.EventSubscriber;
import sx.blah.discord.handle.impl.events.ReadyEvent;
@@ -16,7 +17,9 @@ public class ReadyEventListener {
@EventSubscriber
public void onReadyEvent(ReadyEvent event) {
TimeManager.getManager().init();
UpdateListData.updateSiteBotMeta();
UpdateDisBotData.init();
UpdateDisPwData.init();
MessageManager.reloadLangs();
}