Skip to content

Commit

Permalink
update jda
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGoosik committed Sep 25, 2023
1 parent 309a37a commit 5dd9f4b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/ru/pinkgoosik/kitsun/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ public class Bot {
public static Cached<Secrets> 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);
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ru/pinkgoosik/kitsun/feature/ServerLogger.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/ru/pinkgoosik/kitsun/util/DurationUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 5dd9f4b

Please sign in to comment.