Players Online On Website, Changed packaging a little & added SettingsLoader (#44)

This commit is contained in:
Daniel Ginovker
2019-10-09 17:30:43 -04:00
committed by GitHub
parent 1f49bff69d
commit b97a843428
12 changed files with 96 additions and 41 deletions
@@ -0,0 +1,37 @@
package redone.integrations;
import redone.game.players.PlayerHandler;
import java.io.IOException;
import java.net.URL;
public class PlayersOnlineWebsite {
static String password;
private static void setWebsitePlayersOnline(int amount) throws IOException {
URL url;
url = new URL("https://2006rebotted.tk/playersonline.php?pass=" + password + "&amount=" + amount);
url.openStream().close();
System.out.println("Test!");
}
private static int count = 50;
public static void addUpdatePlayersOnlineTask() {
if (!password.equals("")) {
if (count == 0) {
try {
setWebsitePlayersOnline(PlayerHandler.getPlayerCount());
count = 50;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
count--;
}
} else {
System.out.println("No Players Online On Website Password Set So Task Stopped");
}
}
}
@@ -0,0 +1,40 @@
package redone.integrations;
import org.json.JSONObject;
import redone.integrations.discord.JavaCord;
import java.io.*;
import java.util.stream.Collectors;
public class SettingsLoader {
private static void initialize() {
JSONObject main = new JSONObject();
main
.put("bot-token", "")
.put("powpass", "");
try {
BufferedWriter br = new BufferedWriter(new FileWriter("data/secrets.json"));
br.write(main.toString());
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void loadSettings() throws IOException {
if (!new File("data/secrets.json").exists()) {
initialize();
System.out.println("Please open \"data/secrets.json\" file and enter your discord token bot there!");
System.out.println("Please open \"data/secrets.json\" file and enter your Players Online On Website Password there!");
} else {
BufferedReader br = new BufferedReader(new FileReader("data/secrets.json"));
String out = br.lines().collect(Collectors.joining("\n"));
JSONObject obj = new JSONObject(out);
JavaCord.token = obj.getString("bot-token");
PlayersOnlineWebsite.password = obj.getString("powpass");
}
}
}
@@ -0,0 +1,69 @@
package redone.integrations.discord;
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.message.MessageBuilder;
import org.javacord.api.util.logging.ExceptionLogger;
import redone.game.players.PlayerHandler;
import java.io.IOException;
import static redone.integrations.SettingsLoader.loadSettings;
/**
* @author Patrity || https://www.rune-server.ee/members/patrity/
*/
public class JavaCord {
private static String serverName = "2006-ReBotted";
public static String token;
private static DiscordApi api = null;
public static void init() throws IOException {
loadSettings();
if (token != null && !token.equals("")) { //If the token was loaded by loadSettings:
new DiscordApiBuilder().setToken(token).login().thenAccept(api -> {
JavaCord.api = api;
//System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
api.addMessageCreateListener(event -> {
if (event.getMessageContent().equalsIgnoreCase("::players")) {
if (PlayerHandler.getPlayerCount() > 1) {
event.getChannel().sendMessage("There are currently " + PlayerHandler.getPlayerCount() + " players online.");
} else {
event.getChannel().sendMessage("There is currently " + PlayerHandler.getPlayerCount() + " player online.");
}
}
if (event.getMessageContent().equalsIgnoreCase("::online")) {
event.getChannel().sendMessage(":tada: " + serverName + " is Online! :tada:");
}
if (event.getMessageContent().startsWith("::movehome")) {
if (event.getMessageAuthor().isServerAdmin()) {
System.out.println("perms");
} else {
event.getChannel().sendMessage("You do not have permission to preform this command");
}
}
});
})
// Log any exceptions that happened
.exceptionally(ExceptionLogger.get());
} else {
System.out.println("Discord Token Not Set So Bot Not Loaded");
}
}
public static void sendMessage(String channel, String msg) {
try {
new MessageBuilder()
.append(msg)
.send((TextChannel) api.getTextChannelsByNameIgnoreCase(channel).toArray()[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
}