mirror of
https://github.com/2006-Scape/2006Scape.git
synced 2026-07-07 00:32:06 +00:00
Players Online On Website, Changed packaging a little & added SettingsLoader (#44)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user