Skip to content

Commit aeac78c

Browse files
authored
Merge pull request #37 from AffluentAvo/master
added debug mode
2 parents 5954821 + 0ea7842 commit aeac78c

File tree

8 files changed

+84
-50
lines changed

8 files changed

+84
-50
lines changed

src/main/java/me/polymarsdev/sokobot/Bot.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import me.polymarsdev.sokobot.listener.CommandListener;
55
import me.polymarsdev.sokobot.listener.GameListener;
66
import me.polymarsdev.sokobot.util.GameUtil;
7+
import net.dv8tion.jda.api.JDA;
78
import net.dv8tion.jda.api.OnlineStatus;
89
import net.dv8tion.jda.api.entities.Activity;
910
import net.dv8tion.jda.api.entities.Guild;
@@ -32,6 +33,8 @@ public class Bot {
3233
private static final boolean enableDatabase = false;
3334
private static final Database.DBType dbType = Database.DBType.SQLite;
3435

36+
public static boolean debug = false;
37+
3538
private static ShardManager shardManager;
3639
private static Database database = null;
3740

@@ -96,7 +99,14 @@ public static void main(String[] args) throws LoginException {
9699

97100
private static void processCommand(String cmd) {
98101
if (cmd.equalsIgnoreCase("help")) {
99-
System.out.println("Commands:\nstop - Shuts down the bot and exits the program");
102+
System.out.println("Commands:\nstop - Shuts down the bot and exits the program\ndebug - Toggle debug mode");
103+
return;
104+
}
105+
if (cmd.equalsIgnoreCase("debug")) {
106+
debug = !debug;
107+
String response = debug ? "on" : "off";
108+
System.out.println("[INFO] Turned " + response + " debug mode");
109+
Bot.debug("Make sure to turn off debug mode after necessary information has been collected.");
100110
return;
101111
}
102112
if (cmd.equalsIgnoreCase("stop")) {
@@ -113,6 +123,33 @@ private static void processCommand(String cmd) {
113123
System.out.println("Unknown command. Please use \"help\" for a list of commands.");
114124
}
115125

126+
/*
127+
Debug Info for Developer information
128+
> Limit update to 10 seconds minimum because of JDA shard checks
129+
*/
130+
private static long lastDebugInfoUpdate = -1L;
131+
private static String debugInfo = "";
132+
133+
private static void updateDebugInfo() {
134+
long now = System.currentTimeMillis();
135+
if (now - lastDebugInfoUpdate < 10000) return;
136+
lastDebugInfoUpdate = now;
137+
int a = enableDatabase ? 1 : 0;
138+
int b = enableDatabase ? database.isConnected() ? 1 : 0 : 0;
139+
int c = 0;
140+
int d = shardManager.getShardsTotal();
141+
for (JDA shard : shardManager.getShards()) if (shard.getStatus() == JDA.Status.CONNECTED) c++;
142+
debugInfo = a + b + c + d + "";
143+
}
144+
145+
// Print a message when debug is on
146+
public static void debug(String log) {
147+
if (debug) {
148+
updateDebugInfo();
149+
System.out.println("[DEBUG " + debugInfo + "] " + log);
150+
}
151+
}
152+
116153
public static ShardManager getShardManager() {
117154
return shardManager;
118155
}

src/main/java/me/polymarsdev/sokobot/commands/GameInputCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void execute(CommandEvent event) {
2929
} else game = GameUtil.getGame(user.getIdLong());
3030
//
3131
String userInput = this.getName().toLowerCase();
32+
Bot.debug("Processing game input: " + userInput);
3233
if (userInput.equals("play")) {
3334
if (!game.gameActive) {
3435
if (args.length > 0 && EmojiManager.isEmoji(args[0])) game.setPlayerEmote(args[0]);

src/main/java/me/polymarsdev/sokobot/commands/InfoCommand.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public InfoCommand() {
1414

1515
@Override
1616
public void execute(CommandEvent event) {
17+
Bot.debug("Received info command (or bot mention)");
1718
Guild guild = event.getGuild();
1819
EmbedBuilder info = new EmbedBuilder();
1920
final String prefix = Bot.getPrefix(guild);
@@ -30,16 +31,17 @@ public void execute(CommandEvent event) {
3031
+ "Java HashMaps:tm:, multiple users can use the bot at the same time without interfering with one "
3132
+ "another.\n:white_small_square:**Custom prefixes**\nTo prevent Sokobot from conflicting with other "
3233
+ "bots, admins can choose any single-character prefix to preface Sokobot's commands.", false);
33-
info.addField("Commands",
34-
("``" + prefix + "play`` can be used to start a game if you are not " + "currently in "
35-
+ "one.\n``" + prefix + "stop`` can be used to stop your active game at any "
36-
+ "time.\n``" + prefix + "info`` provides some useful details about the bot and "
37-
+ "rules of " + "the game.\n``" + Bot.getPrefix(guild)
38-
+ "prefix [character]`` can be used to " + "change the prefix the " + "bot responds to."),
39-
false);
40-
info.addField("Add to your server",
41-
"https://top.gg/bot/713635251703906336\nSokobot is currently in " + Bot.getShardManager()
42-
.getGuilds().size() + " servers.", false);
34+
info.addField(
35+
"Commands",
36+
("``" + prefix + "play`` can be used to start a game if you are not " + "currently in " + "one.\n``"
37+
+ prefix + "stop`` can be used to stop your active game at any " + "time.\n``" + prefix
38+
+ "info`` provides some useful details about the bot and " + "rules of " + "the game.\n``" + Bot
39+
.getPrefix(guild) + "prefix [character]`` can be used to " + "change the prefix the "
40+
+ "bot responds to."), false);
41+
info.addField(
42+
"Add to your server",
43+
"https://top.gg/bot/713635251703906336\nSokobot is currently in " + Bot.getShardManager().getGuilds()
44+
.size() + " servers.", false);
4345
/*
4446
// Official Support Server
4547
info.addField("Support / Feedback",

src/main/java/me/polymarsdev/sokobot/commands/PrefixCommand.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import me.polymarsdev.sokobot.Bot;
44
import me.polymarsdev.sokobot.entity.Command;
55
import me.polymarsdev.sokobot.event.CommandEvent;
6-
import net.dv8tion.jda.api.EmbedBuilder;
76
import net.dv8tion.jda.api.Permission;
87
import net.dv8tion.jda.api.entities.Guild;
98
import net.dv8tion.jda.api.entities.Member;
@@ -20,18 +19,28 @@ public void execute(CommandEvent event) {
2019
User user = event.getAuthor();
2120
Member member = event.getMember();
2221
String[] args = event.getArgs();
22+
Guild guild = event.getGuild();
23+
Bot.debug("Received prefix command: " + event.getMessage().getContentRaw());
2324
if (args.length > 0) {
2425
if (!member.hasPermission(Permission.ADMINISTRATOR)) {
26+
Bot.debug("Failed to change prefix of " + guild.getName() + " (" + guild.getId()
27+
+ "): Insufficient permissions");
2528
event.reply(user.getAsMention() + ", you do not have permission to use this command.");
2629
return;
2730
}
2831
String newPrefix = args[0].toLowerCase();
2932
if (newPrefix.length() > 1) {
33+
Bot.debug("Failed to change prefix of " + guild.getName() + " (" + guild.getId() + "): length");
3034
event.reply(user.getAsMention() + ", the prefix must be one character long!");
3135
return;
3236
}
33-
Bot.setPrefix(event.getGuild(), newPrefix);
37+
Bot.setPrefix(guild, newPrefix);
38+
Bot.debug("Successfully changed server prefix of " + guild.getName() + " (" + guild.getId() + ") to: "
39+
+ newPrefix);
3440
event.reply("Prefix successfully changed to ``" + newPrefix + "``.");
41+
return;
3542
}
43+
event.reply(user.getAsMention() + ", please use `" + Bot.getPrefix(guild)
44+
+ "prefix <new prefix>` to set a server-prefix.");
3645
}
3746
}

src/main/java/me/polymarsdev/sokobot/database/Database.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public enum DBType {MySQL, SQLite}
1010
/**
1111
* SQLite Data
1212
* Set this data if you use DBType#SQLite
13-
*
13+
* <p>
1414
* field filePath - This can either be a relative or absolute path.
1515
* ex: sokobot.db
1616
* or: C:/sqlite/db/sokobot.db
@@ -82,8 +82,7 @@ public ResultSet query(String sql, Object... preparedParameters) {
8282

8383
public ResultSet query(String sql) {
8484
try {
85-
ResultSet rs = con.prepareStatement(sql).executeQuery();
86-
return rs;
85+
return con.prepareStatement(sql).executeQuery();
8786
} catch (SQLException e) {
8887
e.printStackTrace();
8988
return null;

src/main/java/me/polymarsdev/sokobot/listener/CommandListener.java

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package me.polymarsdev.sokobot.listener;
22

33
import me.polymarsdev.sokobot.Bot;
4-
import me.polymarsdev.sokobot.Game;
54
import me.polymarsdev.sokobot.commands.GameInputCommand;
65
import me.polymarsdev.sokobot.commands.InfoCommand;
76
import me.polymarsdev.sokobot.commands.PrefixCommand;
87
import me.polymarsdev.sokobot.entity.Command;
98
import me.polymarsdev.sokobot.event.CommandEvent;
10-
import me.polymarsdev.sokobot.util.GameUtil;
119
import net.dv8tion.jda.api.Permission;
1210
import net.dv8tion.jda.api.entities.*;
1311
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
@@ -36,35 +34,6 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
3634
Message message = event.getMessage();
3735
TextChannel channel = event.getChannel();
3836
Guild guild = event.getGuild();
39-
if (user.getId().equals(event.getJDA().getSelfUser().getId())) {
40-
List<MessageEmbed> embeds = message.getEmbeds();
41-
if (embeds.size() > 0) {
42-
MessageEmbed embed = embeds.get(0);
43-
if (embed.getTitle() != null && embed.getTitle().length() > 0) {
44-
if (embed.getTitle().startsWith("Sokobot | Level ")) {
45-
message.addReaction("U+2B05").queue();
46-
message.addReaction("U+27A1").queue();
47-
message.addReaction("U+2B06").queue();
48-
message.addReaction("U+2B07").queue();
49-
message.addReaction("U+1F504").queue();
50-
List<MessageEmbed.Field> fields = embed.getFields();
51-
for (MessageEmbed.Field field : fields) {
52-
if (field.getName() != null && field.getName().equals("Player")) {
53-
if (field.getValue() != null) {
54-
long playerId = Long
55-
.parseLong(field.getValue().substring(2, field.getValue().length() - 1));
56-
if (GameUtil.hasGame(playerId)) {
57-
Game game = GameUtil.getGame(playerId);
58-
game.setGameMessage(message);
59-
}
60-
}
61-
}
62-
}
63-
}
64-
}
65-
}
66-
return;
67-
}
6837
String msgRaw = message.getContentRaw();
6938
String[] args = msgRaw.split("\\s+");
7039
if (args.length > 0) {
@@ -88,13 +57,19 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
8857
}
8958
}
9059
if (isCommand) {
60+
Bot.debug("Command received: " + arg);
9161
if (!hasPermissions(guild, channel)) {
62+
Bot.debug("Not enough permissions to run command: " + arg);
9263
sendInvalidPermissionsMessage(user, channel);
9364
return;
9465
}
9566
Command command = commands.get(arg);
9667
if (isMention) command = commands.get("info");
97-
if (command == null) return;
68+
if (command == null) {
69+
Bot.debug("Received command does not exist: " + arg);
70+
return;
71+
}
72+
Bot.debug("Executing command: " + arg);
9873
command.execute(new CommandEvent(event, Arrays.copyOfRange(msgRaw.split("\\s+"), 1, args.length)));
9974
}
10075
}

src/main/java/me/polymarsdev/sokobot/listener/GameListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
5858
reactionCommand = false;
5959
break;
6060
}
61-
if (reactionCommand) game.run(guild, channel, userInput);
61+
Bot.debug("Executing reaction input: " + userInput);
62+
if (reactionCommand) {
63+
game.run(guild, channel, userInput);
64+
} else Bot.debug("Received invalid reaction command: " + event.getReactionEmote().getName());
6265
if (guild.getSelfMember().hasPermission(channel, Permission.MESSAGE_MANAGE))
6366
reaction.removeReaction(user).queue();
6467
}

src/main/java/me/polymarsdev/sokobot/util/GameUtil.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ public static void sendGameEmbed(MessageChannel channel, String level, String ga
3939
embed.addField("Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``), ``r`` to reset or ``mr`` to "
4040
+ "recreate the map", "", false);
4141
embed.addField("Player", user.getAsMention(), false);
42-
channel.sendMessage(embed.build()).queue();
42+
channel.sendMessage(embed.build()).queue(message -> {
43+
message.addReaction("U+2B05").queue();
44+
message.addReaction("U+27A1").queue();
45+
message.addReaction("U+2B06").queue();
46+
message.addReaction("U+2B07").queue();
47+
message.addReaction("U+1F504").queue();
48+
Game theGame = GameUtil.getGame(user.getIdLong());
49+
theGame.setGameMessage(message);
50+
});
4351
}
4452

4553
public static void updateGameEmbed(Message message, String level, String game, User user) {

0 commit comments

Comments
 (0)