added debug mode

added debug messages
moved first reaction add code
This commit is contained in:
AffluentAvo
2020-11-16 21:04:34 +01:00
parent 5774782238
commit 0ea7842d56
8 changed files with 84 additions and 50 deletions
+38 -1
View File
@@ -4,6 +4,7 @@ import me.polymarsdev.sokobot.database.Database;
import me.polymarsdev.sokobot.listener.CommandListener; import me.polymarsdev.sokobot.listener.CommandListener;
import me.polymarsdev.sokobot.listener.GameListener; import me.polymarsdev.sokobot.listener.GameListener;
import me.polymarsdev.sokobot.util.GameUtil; import me.polymarsdev.sokobot.util.GameUtil;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
@@ -32,6 +33,8 @@ public class Bot {
private static final boolean enableDatabase = false; private static final boolean enableDatabase = false;
private static final Database.DBType dbType = Database.DBType.SQLite; private static final Database.DBType dbType = Database.DBType.SQLite;
public static boolean debug = false;
private static ShardManager shardManager; private static ShardManager shardManager;
private static Database database = null; private static Database database = null;
@@ -96,7 +99,14 @@ public class Bot {
private static void processCommand(String cmd) { private static void processCommand(String cmd) {
if (cmd.equalsIgnoreCase("help")) { 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; return;
} }
if (cmd.equalsIgnoreCase("stop")) { if (cmd.equalsIgnoreCase("stop")) {
@@ -113,6 +123,33 @@ public class Bot {
System.out.println("Unknown command. Please use \"help\" for a list of commands."); 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() { public static ShardManager getShardManager() {
return shardManager; return shardManager;
} }
@@ -29,6 +29,7 @@ public class GameInputCommand extends Command {
} else game = GameUtil.getGame(user.getIdLong()); } else game = GameUtil.getGame(user.getIdLong());
// //
String userInput = this.getName().toLowerCase(); String userInput = this.getName().toLowerCase();
Bot.debug("Processing game input: " + userInput);
if (userInput.equals("play")) { if (userInput.equals("play")) {
if (!game.gameActive) { if (!game.gameActive) {
if (args.length > 0 && EmojiManager.isEmoji(args[0])) game.setPlayerEmote(args[0]); if (args.length > 0 && EmojiManager.isEmoji(args[0])) game.setPlayerEmote(args[0]);
@@ -14,6 +14,7 @@ public class InfoCommand extends Command {
@Override @Override
public void execute(CommandEvent event) { public void execute(CommandEvent event) {
Bot.debug("Received info command (or bot mention)");
Guild guild = event.getGuild(); Guild guild = event.getGuild();
EmbedBuilder info = new EmbedBuilder(); EmbedBuilder info = new EmbedBuilder();
final String prefix = Bot.getPrefix(guild); final String prefix = Bot.getPrefix(guild);
@@ -30,16 +31,17 @@ public class InfoCommand extends Command {
+ "Java HashMaps:tm:, multiple users can use the bot at the same time without interfering with one " + "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 " + "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); + "bots, admins can choose any single-character prefix to preface Sokobot's commands.", false);
info.addField("Commands", info.addField(
("``" + prefix + "play`` can be used to start a game if you are not " + "currently in " "Commands",
+ "one.\n``" + prefix + "stop`` can be used to stop your active game at any " ("``" + prefix + "play`` can be used to start a game if you are not " + "currently in " + "one.\n``"
+ "time.\n``" + prefix + "info`` provides some useful details about the bot and " + prefix + "stop`` can be used to stop your active game at any " + "time.\n``" + prefix
+ "rules of " + "the game.\n``" + Bot.getPrefix(guild) + "info`` provides some useful details about the bot and " + "rules of " + "the game.\n``" + Bot
+ "prefix [character]`` can be used to " + "change the prefix the " + "bot responds to."), .getPrefix(guild) + "prefix [character]`` can be used to " + "change the prefix the "
false); + "bot responds to."), false);
info.addField("Add to your server", info.addField(
"https://top.gg/bot/713635251703906336\nSokobot is currently in " + Bot.getShardManager() "Add to your server",
.getGuilds().size() + " servers.", false); "https://top.gg/bot/713635251703906336\nSokobot is currently in " + Bot.getShardManager().getGuilds()
.size() + " servers.", false);
/* /*
// Official Support Server // Official Support Server
info.addField("Support / Feedback", info.addField("Support / Feedback",
@@ -3,7 +3,6 @@ package me.polymarsdev.sokobot.commands;
import me.polymarsdev.sokobot.Bot; import me.polymarsdev.sokobot.Bot;
import me.polymarsdev.sokobot.entity.Command; import me.polymarsdev.sokobot.entity.Command;
import me.polymarsdev.sokobot.event.CommandEvent; import me.polymarsdev.sokobot.event.CommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
@@ -20,18 +19,28 @@ public class PrefixCommand extends Command {
User user = event.getAuthor(); User user = event.getAuthor();
Member member = event.getMember(); Member member = event.getMember();
String[] args = event.getArgs(); String[] args = event.getArgs();
Guild guild = event.getGuild();
Bot.debug("Received prefix command: " + event.getMessage().getContentRaw());
if (args.length > 0) { if (args.length > 0) {
if (!member.hasPermission(Permission.ADMINISTRATOR)) { 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."); event.reply(user.getAsMention() + ", you do not have permission to use this command.");
return; return;
} }
String newPrefix = args[0].toLowerCase(); String newPrefix = args[0].toLowerCase();
if (newPrefix.length() > 1) { 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!"); event.reply(user.getAsMention() + ", the prefix must be one character long!");
return; 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 + "``."); event.reply("Prefix successfully changed to ``" + newPrefix + "``.");
} return;
}
event.reply(user.getAsMention() + ", please use `" + Bot.getPrefix(guild)
+ "prefix <new prefix>` to set a server-prefix.");
} }
} }
@@ -10,7 +10,7 @@ public class Database {
/** /**
* SQLite Data * SQLite Data
* Set this data if you use DBType#SQLite * Set this data if you use DBType#SQLite
* * <p>
* field filePath - This can either be a relative or absolute path. * field filePath - This can either be a relative or absolute path.
* ex: sokobot.db * ex: sokobot.db
* or: C:/sqlite/db/sokobot.db * or: C:/sqlite/db/sokobot.db
@@ -82,8 +82,7 @@ public class Database {
public ResultSet query(String sql) { public ResultSet query(String sql) {
try { try {
ResultSet rs = con.prepareStatement(sql).executeQuery(); return con.prepareStatement(sql).executeQuery();
return rs;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
@@ -1,13 +1,11 @@
package me.polymarsdev.sokobot.listener; package me.polymarsdev.sokobot.listener;
import me.polymarsdev.sokobot.Bot; import me.polymarsdev.sokobot.Bot;
import me.polymarsdev.sokobot.Game;
import me.polymarsdev.sokobot.commands.GameInputCommand; import me.polymarsdev.sokobot.commands.GameInputCommand;
import me.polymarsdev.sokobot.commands.InfoCommand; import me.polymarsdev.sokobot.commands.InfoCommand;
import me.polymarsdev.sokobot.commands.PrefixCommand; import me.polymarsdev.sokobot.commands.PrefixCommand;
import me.polymarsdev.sokobot.entity.Command; import me.polymarsdev.sokobot.entity.Command;
import me.polymarsdev.sokobot.event.CommandEvent; import me.polymarsdev.sokobot.event.CommandEvent;
import me.polymarsdev.sokobot.util.GameUtil;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
@@ -36,35 +34,6 @@ public class CommandListener extends ListenerAdapter {
Message message = event.getMessage(); Message message = event.getMessage();
TextChannel channel = event.getChannel(); TextChannel channel = event.getChannel();
Guild guild = event.getGuild(); Guild guild = event.getGuild();
if (user.getId().equals(event.getJDA().getSelfUser().getId())) {
List<MessageEmbed> 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<MessageEmbed.Field> 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 msgRaw = message.getContentRaw();
String[] args = msgRaw.split("\\s+"); String[] args = msgRaw.split("\\s+");
if (args.length > 0) { if (args.length > 0) {
@@ -88,13 +57,19 @@ public class CommandListener extends ListenerAdapter {
} }
} }
if (isCommand) { if (isCommand) {
Bot.debug("Command received: " + arg);
if (!hasPermissions(guild, channel)) { if (!hasPermissions(guild, channel)) {
Bot.debug("Not enough permissions to run command: " + arg);
sendInvalidPermissionsMessage(user, channel); sendInvalidPermissionsMessage(user, channel);
return; return;
} }
Command command = commands.get(arg); Command command = commands.get(arg);
if (isMention) command = commands.get("info"); 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))); command.execute(new CommandEvent(event, Arrays.copyOfRange(msgRaw.split("\\s+"), 1, args.length)));
} }
} }
@@ -58,7 +58,10 @@ public class GameListener extends ListenerAdapter {
reactionCommand = false; reactionCommand = false;
break; 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)) if (guild.getSelfMember().hasPermission(channel, Permission.MESSAGE_MANAGE))
reaction.removeReaction(user).queue(); reaction.removeReaction(user).queue();
} }
@@ -39,7 +39,15 @@ public class GameUtil {
embed.addField("Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``), ``r`` to reset or ``mr`` to " embed.addField("Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``), ``r`` to reset or ``mr`` to "
+ "recreate the map", "", false); + "recreate the map", "", false);
embed.addField("Player", user.getAsMention(), 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) { public static void updateGameEmbed(Message message, String level, String game, User user) {