diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..e28d6f3 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/README.md b/README.md index 9e9f30e..e6070d2 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,11 @@ - +
-A multi-purpose Discord bot written in Java. +A multipurpose Discord bot written in Java. -## Requirements -- Java 22 (May work on older versions but only tested on 22) +## Tested Environments +- Java 21+ (May work on older versions but untested) - Maven ### Tested Operating Systems - macOS 14.4.1 +- Windows 10 (22H2) \ No newline at end of file diff --git a/assets/logo.svg b/assets/logo.svg index 6c5b5fe..3e7db09 100644 --- a/assets/logo.svg +++ b/assets/logo.svg @@ -1,5 +1,6 @@ + diff --git a/src/main/java/net/hypr/doki/Config.java b/src/main/java/net/hypr/doki/Config.java index 066da07..72b5395 100644 --- a/src/main/java/net/hypr/doki/Config.java +++ b/src/main/java/net/hypr/doki/Config.java @@ -4,7 +4,6 @@ import com.google.gson.Gson; import java.io.*; import java.nio.charset.StandardCharsets; -import java.nio.file.Path; //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 diff --git a/src/main/java/net/hypr/doki/Doki.java b/src/main/java/net/hypr/doki/Doki.java index bf95ceb..c0251fb 100644 --- a/src/main/java/net/hypr/doki/Doki.java +++ b/src/main/java/net/hypr/doki/Doki.java @@ -29,7 +29,7 @@ public class Doki { public static String getPrefix() { return config.getPrefix(); } public static BasicDataSource getDataSource() { return dataSource; } - public static Doki start() throws IOException, InterruptedException { + public static void start() throws IOException, InterruptedException { config = Config.readConfig(); Config.DBConfig dbConfig = config.getDbConfig(); @@ -56,12 +56,13 @@ public class Doki { LOGGER.info("\t- {} ({})", guild.getName(), guild.getId()); } - return new Doki(jda, config); + new Doki(jda, config); } public static void main(String[] args) { try { - jda = start().getJDA(); + start(); + jda = getJDA(); CommandsBuilder.newBuilder(437970062922612737L) .textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix(getPrefix())) .build(jda, "net.hypr.doki.commands"); //Registering listeners is taken care of by the lib diff --git a/src/main/java/net/hypr/doki/commands/Leaderboard.java b/src/main/java/net/hypr/doki/commands/levelling/Leaderboard.java similarity index 98% rename from src/main/java/net/hypr/doki/commands/Leaderboard.java rename to src/main/java/net/hypr/doki/commands/levelling/Leaderboard.java index 31f7197..3f8d523 100644 --- a/src/main/java/net/hypr/doki/commands/Leaderboard.java +++ b/src/main/java/net/hypr/doki/commands/levelling/Leaderboard.java @@ -1,4 +1,4 @@ -package net.hypr.doki.commands; +package net.hypr.doki.commands.levelling; import com.freya02.botcommands.api.annotations.CommandMarker; import com.freya02.botcommands.api.prefixed.CommandEvent; diff --git a/src/main/java/net/hypr/doki/commands/Rank.java b/src/main/java/net/hypr/doki/commands/levelling/Rank.java similarity index 98% rename from src/main/java/net/hypr/doki/commands/Rank.java rename to src/main/java/net/hypr/doki/commands/levelling/Rank.java index 2a48361..a9a66be 100644 --- a/src/main/java/net/hypr/doki/commands/Rank.java +++ b/src/main/java/net/hypr/doki/commands/levelling/Rank.java @@ -1,4 +1,4 @@ -package net.hypr.doki.commands; +package net.hypr.doki.commands.levelling; import com.freya02.botcommands.api.annotations.CommandMarker; import com.freya02.botcommands.api.prefixed.BaseCommandEvent; diff --git a/src/main/java/net/hypr/doki/commands/About.java b/src/main/java/net/hypr/doki/commands/utils/About.java similarity index 98% rename from src/main/java/net/hypr/doki/commands/About.java rename to src/main/java/net/hypr/doki/commands/utils/About.java index dadd972..ec455d7 100644 --- a/src/main/java/net/hypr/doki/commands/About.java +++ b/src/main/java/net/hypr/doki/commands/utils/About.java @@ -1,4 +1,4 @@ -package net.hypr.doki.commands; +package net.hypr.doki.commands.utils; import com.freya02.botcommands.api.annotations.CommandMarker; import com.freya02.botcommands.api.prefixed.CommandEvent; diff --git a/src/main/java/net/hypr/doki/commands/Ping.java b/src/main/java/net/hypr/doki/commands/utils/Ping.java similarity index 95% rename from src/main/java/net/hypr/doki/commands/Ping.java rename to src/main/java/net/hypr/doki/commands/utils/Ping.java index d2e58e1..2234ee6 100644 --- a/src/main/java/net/hypr/doki/commands/Ping.java +++ b/src/main/java/net/hypr/doki/commands/utils/Ping.java @@ -1,4 +1,4 @@ -package net.hypr.doki.commands; +package net.hypr.doki.commands.utils; import com.freya02.botcommands.api.annotations.CommandMarker; import com.freya02.botcommands.api.prefixed.CommandEvent; diff --git a/src/main/java/net/hypr/doki/commands/WhoIs.java b/src/main/java/net/hypr/doki/commands/utils/WhoIs.java similarity index 98% rename from src/main/java/net/hypr/doki/commands/WhoIs.java rename to src/main/java/net/hypr/doki/commands/utils/WhoIs.java index 70133f2..ca02f86 100644 --- a/src/main/java/net/hypr/doki/commands/WhoIs.java +++ b/src/main/java/net/hypr/doki/commands/utils/WhoIs.java @@ -1,4 +1,4 @@ -package net.hypr.doki.commands; +package net.hypr.doki.commands.utils; import com.freya02.botcommands.api.annotations.CommandMarker; import com.freya02.botcommands.api.prefixed.BaseCommandEvent; diff --git a/src/main/java/net/hypr/doki/listeners/LevellingListener.java b/src/main/java/net/hypr/doki/listeners/LevellingListener.java index aa57713..2568523 100644 --- a/src/main/java/net/hypr/doki/listeners/LevellingListener.java +++ b/src/main/java/net/hypr/doki/listeners/LevellingListener.java @@ -19,17 +19,26 @@ public class LevellingListener extends ListenerAdapter { @Override public void onMessageReceived(MessageReceivedEvent event) { Logger log = Logging.getLogger(); + /* Ignore the message if one of the following conditions is met: - Message is from self user - - Message is from bot + - Message is from a bot - Message is command (starts with bot prefix) + - Message is a Direct Message to the bot */ + try { + event.getGuild(); + } catch (IllegalStateException ex) { + log.debug("Ignoring direct message with ID {}", event.getMessageId()); + return; + } + if ( Doki.getJDA().getSelfUser().getId().equals(event.getAuthor().getId()) || event.getAuthor().isBot() || event.getMessage().getContentStripped().startsWith(Doki.getPrefix()) ) { - log.debug("Ignoring self/bot message with ID " + event.getMessageId()); + log.debug("Ignoring self/bot message with ID {}", event.getMessageId()); return; } @@ -47,13 +56,13 @@ public class LevellingListener extends ListenerAdapter { // it has been over an hour since the user last sent a message that affected XP LevellingUtils.incrementXp(log, rec); } else { - log.debug("Ignoring message ID " + event.getMessageId() + " as not enough time has passed"); + log.debug("Ignoring message ID {} as not enough time has passed", event.getMessageId()); } } catch (SQLException e) { throw new RuntimeException(e); } } else { - log.info("No record of user ID " + user.getId() + " in server " + guild.getId() + ", creating blank record"); + 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()); } catch (SQLException e) { diff --git a/src/main/java/net/hypr/doki/utils/DBUtils.java b/src/main/java/net/hypr/doki/utils/DBUtils.java index fad6012..7825b2a 100644 --- a/src/main/java/net/hypr/doki/utils/DBUtils.java +++ b/src/main/java/net/hypr/doki/utils/DBUtils.java @@ -36,9 +36,9 @@ public class DBUtils { // Find out if the user is already in the DB try { - log.debug("Searching DB for (usr" + user_id + ",srv:" + server_id + ")"); + log.debug("Searching DB for (usr{},srv:{})", user_id, server_id); Set foundIds = qr.query("SELECT user_id FROM users WHERE user_id = " + user_id + " AND server_id = " + server_id, resultSetHandler); - log.debug("Matching records: " + foundIds.size()); + log.debug("Matching records: {}", foundIds.size()); return !foundIds.isEmpty(); } catch (SQLException ignored) { log.debug("An SQL error occurred"); @@ -55,7 +55,7 @@ public class DBUtils { */ public static void createUserRecord(long user_id, long server_id, String username) throws SQLException { Logger log = Logging.getLogger(); - log.info("Creating record (usr:" + user_id + ",srv:" + server_id + ",unm:" + username + ")"); + log.info("Creating record (usr:{},srv:{},unm:{})", user_id, server_id, username); BasicDataSource dataSource = Doki.getDataSource(); Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement( @@ -66,7 +66,7 @@ public class DBUtils { stmt.setString(3, username); stmt.setTimestamp(4, Timestamp.valueOf(LocalDateTime.now())); stmt.execute(); - log.info("Record (usr:" + user_id + ",srv:" + server_id + ",unm:" + username + ") created!"); + log.info("Record (usr:{},srv:{},unm:{}) created!", user_id, server_id, username); } /** @@ -81,7 +81,7 @@ public class DBUtils { */ public static void updateUserRecord(long user_id, long server_id, Timestamp now, int xp, int totalXp, int level) throws SQLException { Logger log = Logging.getLogger(); - log.info("Updating record (usr:" + user_id + ",srv:" + server_id + ")"); + log.info("Updating record (usr:{},srv:{})", user_id, server_id); BasicDataSource dataSource = Doki.getDataSource(); Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement( @@ -94,7 +94,7 @@ public class DBUtils { stmt.setLong(5, user_id); stmt.setLong(6, server_id); stmt.executeQuery(); - log.info("Updated record (usr:" + user_id + ",srv:" + server_id + ")!"); + log.info("Updated record (usr:{},srv:{})!", user_id, server_id); } /** diff --git a/src/main/java/net/hypr/doki/utils/LevellingUtils.java b/src/main/java/net/hypr/doki/utils/LevellingUtils.java index d8483c3..03ca881 100644 --- a/src/main/java/net/hypr/doki/utils/LevellingUtils.java +++ b/src/main/java/net/hypr/doki/utils/LevellingUtils.java @@ -27,9 +27,9 @@ public class LevellingUtils { totalXp, levelNew ); - logger.info("Incremented " + userRecord.username + "'s XP in " + userRecord.server_id + " (" + userRecord.xp + " -> " + xpNew + ")"); + logger.info("Incremented {}'s XP in {} ({} -> {})", userRecord.username, userRecord.server_id, userRecord.xp, xpNew); if (levelNew > userRecord.level) { - logger.info("Incremented " + userRecord.username + "'s level in " + userRecord.server_id + " (" + userRecord.level + " -> " + levelNew + ")"); + logger.info("Incremented {}'s level in {} ({} -> {})", userRecord.username, userRecord.server_id, userRecord.level, levelNew); } } }