Skip to content

Commit

Permalink
Merge pull request #37 from AffluentAvo/master
Browse files Browse the repository at this point in the history
added debug mode
  • Loading branch information
AffluentAvo authored Nov 16, 2020
2 parents 5954821 + 0ea7842 commit aeac78c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 50 deletions.
39 changes: 38 additions & 1 deletion src/main/java/me/polymarsdev/sokobot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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")) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/me/polymarsdev/sokobot/commands/InfoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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",
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/me/polymarsdev/sokobot/commands/PrefixCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <new prefix>` to set a server-prefix.");
}
}
5 changes: 2 additions & 3 deletions src/main/java/me/polymarsdev/sokobot/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum DBType {MySQL, SQLite}
/**
* SQLite Data
* Set this data if you use DBType#SQLite
*
* <p>
* field filePath - This can either be a relative or absolute path.
* ex: sokobot.db
* or: C:/sqlite/db/sokobot.db
Expand Down Expand Up @@ -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;
Expand Down
39 changes: 7 additions & 32 deletions src/main/java/me/polymarsdev/sokobot/listener/CommandListener.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<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[] args = msgRaw.split("\\s+");
if (args.length > 0) {
Expand All @@ -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)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/me/polymarsdev/sokobot/util/GameUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit aeac78c

Please sign in to comment.