From 5dd9f4bf933c9fdc44ceef79608db918e28dd4e1 Mon Sep 17 00:00:00 2001 From: PinkGoosik Date: Mon, 25 Sep 2023 22:59:20 +0500 Subject: [PATCH] update jda --- build.gradle | 2 +- src/main/java/ru/pinkgoosik/kitsun/Bot.java | 28 ++++++++++------- .../kitsun/feature/ServerLogger.java | 6 ++-- .../pinkgoosik/kitsun/util/DurationUtils.java | 31 +++++++++++++++++++ 4 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 src/main/java/ru/pinkgoosik/kitsun/util/DurationUtils.java diff --git a/build.gradle b/build.gradle index 841f367..fd76b9e 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ repositories { dependencies { // https://github.com/discord-jda/JDA/releases - implementation("net.dv8tion:JDA:5.0.0-beta.11") { + implementation("net.dv8tion:JDA:5.0.0-beta.13") { exclude module: "opus-java" } implementation "dev.masecla:Modrinth4J:2.2.0" // https://github.com/masecla22/Modrinth4J diff --git a/src/main/java/ru/pinkgoosik/kitsun/Bot.java b/src/main/java/ru/pinkgoosik/kitsun/Bot.java index 9243dc0..e2e729e 100644 --- a/src/main/java/ru/pinkgoosik/kitsun/Bot.java +++ b/src/main/java/ru/pinkgoosik/kitsun/Bot.java @@ -21,19 +21,20 @@ public class Bot { public static Cached secrets = Cached.of("secrets", Secrets.class, () -> Secrets.DEFAULT); public static JDA jda; - public static void main(String[] args) throws Exception { + public static void main(String[] args) { CosmeticsData.fetch(); Bot.init(); } - public static void init() throws InterruptedException { - String token = secrets.get().discordToken; - if(token.isBlank()) { - LOGGER.error("Discord token is blank, please modify secrets.json"); - System.exit(0); - } + public static void init() { + try { + String token = secrets.get().discordToken; + if(token.isBlank()) { + LOGGER.error("Discord token is blank, please modify secrets.json"); + System.exit(0); + } - var jda = JDABuilder.createDefault(token) + var jda = JDABuilder.createDefault(token) .addEventListeners((EventListener) event -> { if(event instanceof ReadyEvent readyEvent) { DiscordEventsListener.onConnect(readyEvent); @@ -42,11 +43,16 @@ public static void init() throws InterruptedException { .addEventListeners(new DiscordEventsListener()) .enableIntents(Arrays.asList(GatewayIntent.values())); - if(!secrets.get().activity.isBlank()) { - jda.setActivity(Activity.playing(secrets.get().activity)); + if(!secrets.get().activity.isBlank()) { + jda.setActivity(Activity.playing(secrets.get().activity)); + } + + jda.build().awaitReady(); + } + catch (Exception e) { + System.out.println("Bot can't be started due to an exception: " + e); } - jda.build().awaitReady(); } public static Guild getGuild(String id) { diff --git a/src/main/java/ru/pinkgoosik/kitsun/feature/ServerLogger.java b/src/main/java/ru/pinkgoosik/kitsun/feature/ServerLogger.java index 2d084e1..87fd5cd 100644 --- a/src/main/java/ru/pinkgoosik/kitsun/feature/ServerLogger.java +++ b/src/main/java/ru/pinkgoosik/kitsun/feature/ServerLogger.java @@ -1,6 +1,5 @@ package ru.pinkgoosik.kitsun.feature; - import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Message; @@ -12,10 +11,11 @@ import org.jetbrains.annotations.Nullable; import ru.pinkgoosik.kitsun.Bot; import ru.pinkgoosik.kitsun.event.DiscordEventsListener; +import ru.pinkgoosik.kitsun.util.DurationUtils; import ru.pinkgoosik.kitsun.util.KitsunColors; +import java.time.Duration; import java.time.Instant; -import java.time.temporal.ChronoUnit; import java.util.function.Consumer; public class ServerLogger { @@ -102,7 +102,7 @@ public void onVoiceChannelDelete(AutoChannelsManager.Session session, @Nullable if(session != null) { Instant created = Instant.parse(session.created); Instant now = Instant.now(); - embed.addField(new MessageEmbed.Field("Lasted", "**" + (int) ChronoUnit.MINUTES.between(created, now) + "** min", false)); + embed.addField(new MessageEmbed.Field("Lasted", DurationUtils.format(Duration.between(created, now)), false)); } embed.setColor(KitsunColors.getRed()); diff --git a/src/main/java/ru/pinkgoosik/kitsun/util/DurationUtils.java b/src/main/java/ru/pinkgoosik/kitsun/util/DurationUtils.java new file mode 100644 index 0000000..13313ab --- /dev/null +++ b/src/main/java/ru/pinkgoosik/kitsun/util/DurationUtils.java @@ -0,0 +1,31 @@ +package ru.pinkgoosik.kitsun.util; + +import org.jetbrains.annotations.Nullable; + +import java.time.Duration; +import java.time.format.DateTimeParseException; + +public class DurationUtils { + + @Nullable + public static Duration parse(String str) { + try { + return Duration.parse("pt" + str); + } + catch (DateTimeParseException e) { + System.out.println("String cant be parsed"); + } + return null; + } + + public static String format(Duration duration) { + long seconds = duration.getSeconds(); + long absSeconds = Math.abs(seconds); + String positive = String.format( + "%dh%02dm%02ds", + absSeconds / 3600, + (absSeconds % 3600) / 60, + absSeconds % 60); + return seconds < 0 ? "-" + positive : positive; + } +}