diff --git a/src/main/java/me/polymarsdev/sokobot/Bot.java b/src/main/java/me/polymarsdev/sokobot/Bot.java index d95914a..8ea80d8 100644 --- a/src/main/java/me/polymarsdev/sokobot/Bot.java +++ b/src/main/java/me/polymarsdev/sokobot/Bot.java @@ -4,6 +4,7 @@ import me.polymarsdev.sokobot.listener.CommandListener; import me.polymarsdev.sokobot.listener.GameListener; import me.polymarsdev.sokobot.util.GameUtil; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Guild; @@ -32,6 +33,8 @@ public class Bot { private static final boolean enableDatabase = false; private static final Database.DBType dbType = Database.DBType.SQLite; + public static boolean debug = false; + private static ShardManager shardManager; private static Database database = null; @@ -96,7 +99,14 @@ public static void main(String[] args) throws LoginException { private static void processCommand(String cmd) { if (cmd.equalsIgnoreCase("help")) { - System.out.println("Commands:\nstop - Shuts down the bot and exits the program"); + System.out.println("Commands:\nstop - Shuts down the bot and exits the program\ndebug - Toggle debug mode"); + return; + } + if (cmd.equalsIgnoreCase("debug")) { + debug = !debug; + String response = debug ? "on" : "off"; + System.out.println("[INFO] Turned " + response + " debug mode"); + Bot.debug("Make sure to turn off debug mode after necessary information has been collected."); return; } if (cmd.equalsIgnoreCase("stop")) { @@ -113,6 +123,33 @@ private static void processCommand(String cmd) { System.out.println("Unknown command. Please use \"help\" for a list of commands."); } + /* + Debug Info for Developer information + > Limit update to 10 seconds minimum because of JDA shard checks + */ + private static long lastDebugInfoUpdate = -1L; + private static String debugInfo = ""; + + private static void updateDebugInfo() { + long now = System.currentTimeMillis(); + if (now - lastDebugInfoUpdate < 10000) return; + lastDebugInfoUpdate = now; + int a = enableDatabase ? 1 : 0; + int b = enableDatabase ? database.isConnected() ? 1 : 0 : 0; + int c = 0; + int d = shardManager.getShardsTotal(); + for (JDA shard : shardManager.getShards()) if (shard.getStatus() == JDA.Status.CONNECTED) c++; + debugInfo = a + b + c + d + ""; + } + + // Print a message when debug is on + public static void debug(String log) { + if (debug) { + updateDebugInfo(); + System.out.println("[DEBUG " + debugInfo + "] " + log); + } + } + public static ShardManager getShardManager() { return shardManager; } diff --git a/src/main/java/me/polymarsdev/sokobot/commands/GameInputCommand.java b/src/main/java/me/polymarsdev/sokobot/commands/GameInputCommand.java index aab59a6..2b89c45 100644 --- a/src/main/java/me/polymarsdev/sokobot/commands/GameInputCommand.java +++ b/src/main/java/me/polymarsdev/sokobot/commands/GameInputCommand.java @@ -29,6 +29,7 @@ public void execute(CommandEvent event) { } else game = GameUtil.getGame(user.getIdLong()); // String userInput = this.getName().toLowerCase(); + Bot.debug("Processing game input: " + userInput); if (userInput.equals("play")) { if (!game.gameActive) { if (args.length > 0 && EmojiManager.isEmoji(args[0])) game.setPlayerEmote(args[0]); diff --git a/src/main/java/me/polymarsdev/sokobot/commands/InfoCommand.java b/src/main/java/me/polymarsdev/sokobot/commands/InfoCommand.java index df47eb2..41f4f0d 100644 --- a/src/main/java/me/polymarsdev/sokobot/commands/InfoCommand.java +++ b/src/main/java/me/polymarsdev/sokobot/commands/InfoCommand.java @@ -14,6 +14,7 @@ public InfoCommand() { @Override public void execute(CommandEvent event) { + Bot.debug("Received info command (or bot mention)"); Guild guild = event.getGuild(); EmbedBuilder info = new EmbedBuilder(); final String prefix = Bot.getPrefix(guild); @@ -30,16 +31,17 @@ public void execute(CommandEvent event) { + "Java HashMaps:tm:, multiple users can use the bot at the same time without interfering with one " + "another.\n:white_small_square:**Custom prefixes**\nTo prevent Sokobot from conflicting with other " + "bots, admins can choose any single-character prefix to preface Sokobot's commands.", false); - info.addField("Commands", - ("``" + prefix + "play`` can be used to start a game if you are not " + "currently in " - + "one.\n``" + prefix + "stop`` can be used to stop your active game at any " - + "time.\n``" + prefix + "info`` provides some useful details about the bot and " - + "rules of " + "the game.\n``" + Bot.getPrefix(guild) - + "prefix [character]`` can be used to " + "change the prefix the " + "bot responds to."), - false); - info.addField("Add to your server", - "https://top.gg/bot/713635251703906336\nSokobot is currently in " + Bot.getShardManager() - .getGuilds().size() + " servers.", false); + info.addField( + "Commands", + ("``" + prefix + "play`` can be used to start a game if you are not " + "currently in " + "one.\n``" + + prefix + "stop`` can be used to stop your active game at any " + "time.\n``" + prefix + + "info`` provides some useful details about the bot and " + "rules of " + "the game.\n``" + Bot + .getPrefix(guild) + "prefix [character]`` can be used to " + "change the prefix the " + + "bot responds to."), false); + info.addField( + "Add to your server", + "https://top.gg/bot/713635251703906336\nSokobot is currently in " + Bot.getShardManager().getGuilds() + .size() + " servers.", false); /* // Official Support Server info.addField("Support / Feedback", diff --git a/src/main/java/me/polymarsdev/sokobot/commands/PrefixCommand.java b/src/main/java/me/polymarsdev/sokobot/commands/PrefixCommand.java index 7224d40..3ec40f1 100644 --- a/src/main/java/me/polymarsdev/sokobot/commands/PrefixCommand.java +++ b/src/main/java/me/polymarsdev/sokobot/commands/PrefixCommand.java @@ -3,7 +3,6 @@ import me.polymarsdev.sokobot.Bot; import me.polymarsdev.sokobot.entity.Command; import me.polymarsdev.sokobot.event.CommandEvent; -import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; @@ -20,18 +19,28 @@ public void execute(CommandEvent event) { User user = event.getAuthor(); Member member = event.getMember(); String[] args = event.getArgs(); + Guild guild = event.getGuild(); + Bot.debug("Received prefix command: " + event.getMessage().getContentRaw()); if (args.length > 0) { if (!member.hasPermission(Permission.ADMINISTRATOR)) { + Bot.debug("Failed to change prefix of " + guild.getName() + " (" + guild.getId() + + "): Insufficient permissions"); event.reply(user.getAsMention() + ", you do not have permission to use this command."); return; } String newPrefix = args[0].toLowerCase(); if (newPrefix.length() > 1) { + Bot.debug("Failed to change prefix of " + guild.getName() + " (" + guild.getId() + "): length"); event.reply(user.getAsMention() + ", the prefix must be one character long!"); return; } - Bot.setPrefix(event.getGuild(), newPrefix); + Bot.setPrefix(guild, newPrefix); + Bot.debug("Successfully changed server prefix of " + guild.getName() + " (" + guild.getId() + ") to: " + + newPrefix); event.reply("Prefix successfully changed to ``" + newPrefix + "``."); + return; } + event.reply(user.getAsMention() + ", please use `" + Bot.getPrefix(guild) + + "prefix ` to set a server-prefix."); } } diff --git a/src/main/java/me/polymarsdev/sokobot/database/Database.java b/src/main/java/me/polymarsdev/sokobot/database/Database.java index 2ce40aa..bc1f66b 100644 --- a/src/main/java/me/polymarsdev/sokobot/database/Database.java +++ b/src/main/java/me/polymarsdev/sokobot/database/Database.java @@ -10,7 +10,7 @@ public enum DBType {MySQL, SQLite} /** * SQLite Data * Set this data if you use DBType#SQLite - * + *

* field filePath - This can either be a relative or absolute path. * ex: sokobot.db * or: C:/sqlite/db/sokobot.db @@ -82,8 +82,7 @@ public ResultSet query(String sql, Object... preparedParameters) { public ResultSet query(String sql) { try { - ResultSet rs = con.prepareStatement(sql).executeQuery(); - return rs; + return con.prepareStatement(sql).executeQuery(); } catch (SQLException e) { e.printStackTrace(); return null; diff --git a/src/main/java/me/polymarsdev/sokobot/listener/CommandListener.java b/src/main/java/me/polymarsdev/sokobot/listener/CommandListener.java index 5c466c0..e3dc1ea 100644 --- a/src/main/java/me/polymarsdev/sokobot/listener/CommandListener.java +++ b/src/main/java/me/polymarsdev/sokobot/listener/CommandListener.java @@ -1,13 +1,11 @@ package me.polymarsdev.sokobot.listener; import me.polymarsdev.sokobot.Bot; -import me.polymarsdev.sokobot.Game; import me.polymarsdev.sokobot.commands.GameInputCommand; import me.polymarsdev.sokobot.commands.InfoCommand; import me.polymarsdev.sokobot.commands.PrefixCommand; import me.polymarsdev.sokobot.entity.Command; import me.polymarsdev.sokobot.event.CommandEvent; -import me.polymarsdev.sokobot.util.GameUtil; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; @@ -36,35 +34,6 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent event) { Message message = event.getMessage(); TextChannel channel = event.getChannel(); Guild guild = event.getGuild(); - if (user.getId().equals(event.getJDA().getSelfUser().getId())) { - List embeds = message.getEmbeds(); - if (embeds.size() > 0) { - MessageEmbed embed = embeds.get(0); - if (embed.getTitle() != null && embed.getTitle().length() > 0) { - if (embed.getTitle().startsWith("Sokobot | Level ")) { - message.addReaction("U+2B05").queue(); - message.addReaction("U+27A1").queue(); - message.addReaction("U+2B06").queue(); - message.addReaction("U+2B07").queue(); - message.addReaction("U+1F504").queue(); - List fields = embed.getFields(); - for (MessageEmbed.Field field : fields) { - if (field.getName() != null && field.getName().equals("Player")) { - if (field.getValue() != null) { - long playerId = Long - .parseLong(field.getValue().substring(2, field.getValue().length() - 1)); - if (GameUtil.hasGame(playerId)) { - Game game = GameUtil.getGame(playerId); - game.setGameMessage(message); - } - } - } - } - } - } - } - return; - } String msgRaw = message.getContentRaw(); String[] args = msgRaw.split("\\s+"); if (args.length > 0) { @@ -88,13 +57,19 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent event) { } } if (isCommand) { + Bot.debug("Command received: " + arg); if (!hasPermissions(guild, channel)) { + Bot.debug("Not enough permissions to run command: " + arg); sendInvalidPermissionsMessage(user, channel); return; } Command command = commands.get(arg); if (isMention) command = commands.get("info"); - if (command == null) return; + if (command == null) { + Bot.debug("Received command does not exist: " + arg); + return; + } + Bot.debug("Executing command: " + arg); command.execute(new CommandEvent(event, Arrays.copyOfRange(msgRaw.split("\\s+"), 1, args.length))); } } diff --git a/src/main/java/me/polymarsdev/sokobot/listener/GameListener.java b/src/main/java/me/polymarsdev/sokobot/listener/GameListener.java index c98577d..6e79899 100644 --- a/src/main/java/me/polymarsdev/sokobot/listener/GameListener.java +++ b/src/main/java/me/polymarsdev/sokobot/listener/GameListener.java @@ -58,7 +58,10 @@ public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) { reactionCommand = false; break; } - if (reactionCommand) game.run(guild, channel, userInput); + Bot.debug("Executing reaction input: " + userInput); + if (reactionCommand) { + game.run(guild, channel, userInput); + } else Bot.debug("Received invalid reaction command: " + event.getReactionEmote().getName()); if (guild.getSelfMember().hasPermission(channel, Permission.MESSAGE_MANAGE)) reaction.removeReaction(user).queue(); } diff --git a/src/main/java/me/polymarsdev/sokobot/util/GameUtil.java b/src/main/java/me/polymarsdev/sokobot/util/GameUtil.java index 8a3372e..a97930c 100644 --- a/src/main/java/me/polymarsdev/sokobot/util/GameUtil.java +++ b/src/main/java/me/polymarsdev/sokobot/util/GameUtil.java @@ -39,7 +39,15 @@ public static void sendGameEmbed(MessageChannel channel, String level, String ga embed.addField("Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``), ``r`` to reset or ``mr`` to " + "recreate the map", "", false); embed.addField("Player", user.getAsMention(), false); - channel.sendMessage(embed.build()).queue(); + channel.sendMessage(embed.build()).queue(message -> { + message.addReaction("U+2B05").queue(); + message.addReaction("U+27A1").queue(); + message.addReaction("U+2B06").queue(); + message.addReaction("U+2B07").queue(); + message.addReaction("U+1F504").queue(); + Game theGame = GameUtil.getGame(user.getIdLong()); + theGame.setGameMessage(message); + }); } public static void updateGameEmbed(Message message, String level, String game, User user) {