mirror of
https://github.com/bytedream/untisbot-discord.git
synced 2025-05-09 12:15:09 +02:00
Added update rich presence
This commit is contained in:
parent
c34f05e9d4
commit
f19369d338
@ -1,3 +1,8 @@
|
||||
/**
|
||||
* @author ByteDream
|
||||
* @version 1.1
|
||||
*/
|
||||
|
||||
package org.bytedream.untisbot;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
@ -21,6 +26,8 @@ import java.util.HashSet;
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static final String version = "1.1";
|
||||
|
||||
private static Logger logger;
|
||||
private static Connection connection;
|
||||
|
||||
|
@ -1,11 +1,27 @@
|
||||
package org.bytedream.untisbot.discord;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
import org.bytedream.untisbot.Crypt;
|
||||
import org.bytedream.untisbot.Main;
|
||||
import org.bytedream.untisbot.data.StoreType;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Base class to start the bot
|
||||
@ -16,6 +32,7 @@ import javax.security.auth.login.LoginException;
|
||||
public class Discord {
|
||||
|
||||
private final JDABuilder jdaBuilder;
|
||||
private JDA jda = null;
|
||||
|
||||
/**
|
||||
* Configures the bot to make it ready to launch
|
||||
@ -27,9 +44,49 @@ public class Discord {
|
||||
*/
|
||||
public Discord(String token, StoreType storeType, String encryptPassword, JSONObject languages) {
|
||||
jdaBuilder = JDABuilder.createDefault(token);
|
||||
updateRichPresence();
|
||||
jdaBuilder.addEventListeners(new DiscordCommandListener(storeType, new Crypt(encryptPassword), languages));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show rich presence if a bot update was released within then last 24 hours
|
||||
* @since 1.1
|
||||
*/
|
||||
private void updateRichPresence() {
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/repos/ByteDream/untisbot-discord/releases/tags/v" + Main.version).openConnection();
|
||||
connection.connect();
|
||||
|
||||
if (connection.getResponseCode() == 200) {
|
||||
JSONTokener jsonTokener = new JSONTokener(new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)));
|
||||
JSONObject releaseInfos = new JSONObject(jsonTokener);
|
||||
String releaseTime = releaseInfos.getString("published_at");
|
||||
LocalDateTime releaseDateTime = LocalDateTime.parse(releaseTime.substring(0, releaseTime.length() - 1));
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
if (ChronoUnit.DAYS.between(now, releaseDateTime) == 0) {
|
||||
if (jda != null) {
|
||||
jda.getPresence().setActivity(Activity.playing("update " + Main.version + " („\\(^_^)/“)"));
|
||||
} else {
|
||||
jdaBuilder.setActivity(Activity.playing("update " + Main.version + " („\\(^_^)/“)"));
|
||||
}
|
||||
new Timer().schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (jda != null) {
|
||||
jda.getPresence().setActivity(null);
|
||||
} else {
|
||||
jdaBuilder.setActivity(null);
|
||||
}
|
||||
}
|
||||
}, TimeUnit.DAYS.toMillis(1) - ChronoUnit.MILLIS.between(now, releaseDateTime));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the bot
|
||||
*
|
||||
@ -37,7 +94,7 @@ public class Discord {
|
||||
* @since 1.0
|
||||
*/
|
||||
public void start() throws LoginException {
|
||||
jdaBuilder.build();
|
||||
jda = jdaBuilder.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user