From 1a196d9fb3cb50bdb7829f5c5619475765b573f1 Mon Sep 17 00:00:00 2001 From: floppydiskette Date: Thu, 5 Sep 2024 03:36:25 +0100 Subject: [PATCH 1/5] Config error logging changes --- src/main/java/net/hypr/doki/Config.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/hypr/doki/Config.java b/src/main/java/net/hypr/doki/Config.java index 72b5395..8585fce 100644 --- a/src/main/java/net/hypr/doki/Config.java +++ b/src/main/java/net/hypr/doki/Config.java @@ -6,7 +6,7 @@ import java.io.*; import java.nio.charset.StandardCharsets; //You can add more fields in this class, if your input json matches the structure -//You will need a valid config.json in the package com.freya02.bot for this to work +//You will need a valid config.json in the resources folder for this to work public class Config { @SuppressWarnings("unused") private String token; @SuppressWarnings("unused") private String prefix; @@ -24,14 +24,7 @@ public class Config { Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8); return new Gson().fromJson(reader, Config.class); } catch (IOException | NullPointerException e) { - throw new IOException(""" - config.json was not found in the root folder (of the project), did you forget to put it ? - Example structure: - - { - "token": "[your_bot_token_here]" - } - """, e); + throw new IOException("config.json was not found, did you forget to create it?", e); } } From 3001616fcb6e12a608857726779bddcf03ab07f0 Mon Sep 17 00:00:00 2001 From: floppydiskette Date: Thu, 5 Sep 2024 03:37:21 +0100 Subject: [PATCH 2/5] Increment user XP on record creation --- src/main/java/net/hypr/doki/listeners/LevellingListener.java | 3 ++- src/main/java/net/hypr/doki/utils/DBUtils.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/hypr/doki/listeners/LevellingListener.java b/src/main/java/net/hypr/doki/listeners/LevellingListener.java index 2568523..330cb46 100644 --- a/src/main/java/net/hypr/doki/listeners/LevellingListener.java +++ b/src/main/java/net/hypr/doki/listeners/LevellingListener.java @@ -64,7 +64,8 @@ public class LevellingListener extends ListenerAdapter { } else { log.info("No record of user ID {} in server {}, creating blank record", user.getId(), guild.getId()); try { - DBUtils.createUserRecord(user.getIdLong(), guild.getIdLong(), user.getName()); + UserRecord rec = DBUtils.createUserRecord(user.getIdLong(), guild.getIdLong(), user.getName()); + LevellingUtils.incrementXp(log, rec); } catch (SQLException e) { throw new RuntimeException(e); } diff --git a/src/main/java/net/hypr/doki/utils/DBUtils.java b/src/main/java/net/hypr/doki/utils/DBUtils.java index 7825b2a..48f04fc 100644 --- a/src/main/java/net/hypr/doki/utils/DBUtils.java +++ b/src/main/java/net/hypr/doki/utils/DBUtils.java @@ -53,7 +53,7 @@ public class DBUtils { * @param username The users username * @throws SQLException A SQL exception */ - public static void createUserRecord(long user_id, long server_id, String username) throws SQLException { + public static UserRecord createUserRecord(long user_id, long server_id, String username) throws SQLException { Logger log = Logging.getLogger(); log.info("Creating record (usr:{},srv:{},unm:{})", user_id, server_id, username); BasicDataSource dataSource = Doki.getDataSource(); @@ -67,6 +67,7 @@ public class DBUtils { stmt.setTimestamp(4, Timestamp.valueOf(LocalDateTime.now())); stmt.execute(); log.info("Record (usr:{},srv:{},unm:{}) created!", user_id, server_id, username); + return new UserRecord(user_id, server_id, username); } /** From 9b3b3ef32004f9043cee20c711bca01bf8dc4339 Mon Sep 17 00:00:00 2001 From: floppydiskette Date: Thu, 5 Sep 2024 03:48:06 +0100 Subject: [PATCH 3/5] Improve logging when config is missing --- src/main/java/net/hypr/doki/Config.java | 7 ++++++- src/main/java/net/hypr/doki/Doki.java | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/hypr/doki/Config.java b/src/main/java/net/hypr/doki/Config.java index 8585fce..c6b233e 100644 --- a/src/main/java/net/hypr/doki/Config.java +++ b/src/main/java/net/hypr/doki/Config.java @@ -1,6 +1,8 @@ package net.hypr.doki; +import com.freya02.botcommands.api.Logging; import com.google.gson.Gson; +import org.slf4j.Logger; import java.io.*; import java.nio.charset.StandardCharsets; @@ -19,12 +21,15 @@ public class Config { * @throws IOException if the config JSON could not be read */ public static Config readConfig() throws IOException { + Logger log = Logging.getLogger(); try (InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("config.json")) { assert in != null; Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8); + log.info("Loaded config"); return new Gson().fromJson(reader, Config.class); } catch (IOException | NullPointerException e) { - throw new IOException("config.json was not found, did you forget to create it?", e); + log.error("Failed to load config.json, does the file exist?"); + throw new IOException(e); } } diff --git a/src/main/java/net/hypr/doki/Doki.java b/src/main/java/net/hypr/doki/Doki.java index c0251fb..43d23cf 100644 --- a/src/main/java/net/hypr/doki/Doki.java +++ b/src/main/java/net/hypr/doki/Doki.java @@ -13,7 +13,7 @@ import org.slf4j.Logger; import java.io.IOException; public class Doki { - private static final Logger LOGGER = Logging.getLogger(); + private static final Logger log = Logging.getLogger(); private static JDA jda; private static Config config; private static final BasicDataSource dataSource = new BasicDataSource(); @@ -50,10 +50,10 @@ public class Doki { dataSource.setInitialSize(10); //Print some information about the bot - LOGGER.info("Bot connected as {}", jda.getSelfUser().getAsTag()); - LOGGER.info("The bot is present on these guilds :"); + log.info("Bot connected as {}", jda.getSelfUser().getAsTag()); + log.info("The bot is present in the following guilds:"); for (Guild guild : jda.getGuildCache()) { - LOGGER.info("\t- {} ({})", guild.getName(), guild.getId()); + log.info("\t- {} ({})", guild.getName(), guild.getId()); } new Doki(jda, config); @@ -68,7 +68,7 @@ public class Doki { .build(jda, "net.hypr.doki.commands"); //Registering listeners is taken care of by the lib jda.addEventListener(new LevellingListener()); } catch (Exception e) { - LOGGER.error("Unable to start the bot", e); + log.error("Failed to start the bot", e); System.exit(-1); } } From 319eefbd2a7a3c94300477e1b3f152e2d16d4e99 Mon Sep 17 00:00:00 2001 From: floppydiskette Date: Thu, 5 Sep 2024 03:50:30 +0100 Subject: [PATCH 4/5] Add artifact, ignore out folder --- .gitignore | 1 + .idea/artifacts/doki_jar.xml | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 .idea/artifacts/doki_jar.xml diff --git a/.gitignore b/.gitignore index 88f2c75..7cb2ea7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target/ +out/ !.mvn/wrapper/maven-wrapper.jar !**/src/main/**/target/ !**/src/test/**/target/ diff --git a/.idea/artifacts/doki_jar.xml b/.idea/artifacts/doki_jar.xml new file mode 100644 index 0000000..b4fd852 --- /dev/null +++ b/.idea/artifacts/doki_jar.xml @@ -0,0 +1,48 @@ + + + $PROJECT_DIR$/out/artifacts/doki_jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From c3272ad9aca03c8e77f714497748a0211ec3eaf5 Mon Sep 17 00:00:00 2001 From: floppydiskette Date: Thu, 5 Sep 2024 03:50:48 +0100 Subject: [PATCH 5/5] Add MANIFEST.MF --- src/main/resources/META-INF/MANIFEST.MF | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/resources/META-INF/MANIFEST.MF diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..f185663 --- /dev/null +++ b/src/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: net.hypr.doki.Doki +