From 577478223849abbeed36fd870f5118640934ed13 Mon Sep 17 00:00:00 2001 From: AffluentAvo Date: Sat, 7 Nov 2020 22:22:02 +0100 Subject: [PATCH] fixed startup issues copy pasted some code from public bot code added difference information to README.md --- README.md | 1 + src/main/java/me/polymarsdev/sokobot/Bot.java | 12 +++- .../java/me/polymarsdev/sokobot/Game.java | 60 ++++++++++++------- .../me/polymarsdev/sokobot/util/GameUtil.java | 3 +- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 7e7c82a..d5bb121 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ To prevent Sokobot from conflicting with other bots, admins can choose any singl Sokobot is available on top.gg and can be added to your server [in one click](https://top.gg/bot/713635251703906336/)! You can alternatively add it using the direct link [here](https://discord.com/api/oauth2/authorize?client_id=713635251703906336&permissions=8192&scope=bot). ### Self-hosting Grab the [latest .jar](https://github.com/PolyMarsDev/Sokobot/releases) or [build it yourself](#compiling). Then, create a Discord Bot Application [here](https://discord.com/developers/applications/) and paste the bot token into ``token.txt``. Then, ensure the two files are in the same directory and run the .jar file. +Please note, this bot differs a bit from the public bot. For example, there is no voting rewards (custom emotes are always unlocked), no leaderboard, no progress saving, etc. diff --git a/src/main/java/me/polymarsdev/sokobot/Bot.java b/src/main/java/me/polymarsdev/sokobot/Bot.java index e36f658..d95914a 100644 --- a/src/main/java/me/polymarsdev/sokobot/Bot.java +++ b/src/main/java/me/polymarsdev/sokobot/Bot.java @@ -7,8 +7,10 @@ import me.polymarsdev.sokobot.util.GameUtil; import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder; import net.dv8tion.jda.api.sharding.ShardManager; +import net.dv8tion.jda.api.utils.cache.CacheFlag; import javax.security.auth.login.LoginException; import java.io.File; @@ -16,8 +18,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.HashMap; -import java.util.Scanner; +import java.util.*; public class Bot { static HashMap prefixes = new HashMap<>(); @@ -71,10 +72,15 @@ public class Bot { + "NULL);"); } } - DefaultShardManagerBuilder builder = new DefaultShardManagerBuilder(token); + List intents = new ArrayList<>( + Arrays.asList(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_EMOJIS, + GatewayIntent.GUILD_MESSAGE_REACTIONS)); + DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.create(token, intents); builder.setStatus(OnlineStatus.ONLINE); builder.setActivity(Activity.playing("@Sokobot for info!")); builder.addEventListeners(new GameListener(), new CommandListener()); + builder.disableCache( + CacheFlag.CLIENT_STATUS, CacheFlag.ACTIVITY, CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE); shardManager = builder.build(); GameUtil.runGameTimer(); Thread consoleThread = new Thread(() -> { diff --git a/src/main/java/me/polymarsdev/sokobot/Game.java b/src/main/java/me/polymarsdev/sokobot/Game.java index e1c4a23..70326e3 100644 --- a/src/main/java/me/polymarsdev/sokobot/Game.java +++ b/src/main/java/me/polymarsdev/sokobot/Game.java @@ -3,8 +3,10 @@ package me.polymarsdev.sokobot; import me.polymarsdev.sokobot.objects.Grid; import me.polymarsdev.sokobot.util.GameUtil; import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.requests.RestAction; import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; public class Game { long gameMessageID; @@ -34,9 +36,9 @@ public class Game { public void newGame(MessageChannel channel) { if (!gameActive) { - level = 1; width = 9; height = 6; + for (int i = 1; i < level; i++) updateWidthHeight(); grid = new Grid(width, height, level, playerEmote); gameActive = true; lastAction = System.currentTimeMillis(); @@ -44,6 +46,12 @@ public class Game { } } + // This method used to something earlier. (I actually just forgot what I used it for) + // It did not work like it was supposed to, so it was changed to this basic line. + private void queue(RestAction restAction, Consumer success) { + restAction.queue(success); + } + public void stop() { gameActive = false; TextChannel textChannel = Bot.getShardManager().getTextChannelById(channelID); @@ -53,64 +61,74 @@ public class Game { } public void run(Guild guild, TextChannel channel, String userInput) { - lastAction = System.currentTimeMillis(); if (userInput.equals("stop") && gameActive) { stop(); channel.sendMessage("Thanks for playing, " + user.getAsMention() + "!") - .queue(msg -> msg.delete().queueAfter(10, TimeUnit.SECONDS)); + .queue(msg -> msg.delete().queueAfter(10, TimeUnit.SECONDS)); } if (userInput.equals("play") && !gameActive) { newGame(channel); } else if (gameActive) { - if (!grid.hasWon()) { - String direction = userInput; - switch (direction) { + lastAction = System.currentTimeMillis(); + boolean won = grid.hasWon(); + if (!won) { + boolean moved = false; + switch (userInput) { case "up": case "w": - grid.getPlayer().moveUp(); + moved = grid.getPlayer().moveUp(); break; case "down": case "s": - grid.getPlayer().moveDown(); + moved = grid.getPlayer().moveDown(); break; case "left": case "a": - grid.getPlayer().moveLeft(); + moved = grid.getPlayer().moveLeft(); break; case "right": case "d": - grid.getPlayer().moveRight(); + moved = grid.getPlayer().moveRight(); break; case "mr": grid.resetMap(); + moved = true; break; case "r": grid.reset(); + moved = true; break; } - if (!grid.hasWon()) { + grid.updateGrid(); + won = grid.hasWon(); + if (!won && moved) { TextChannel textChannel = Bot.getShardManager().getTextChannelById(channelID); if (textChannel != null) { - textChannel.retrieveMessageById(gameMessageID).queue(gameMessage -> GameUtil + queue(textChannel.retrieveMessageById(gameMessageID), gameMessage -> GameUtil .updateGameEmbed(gameMessage, String.valueOf(level), grid.toString(), user)); } } } - if (grid.hasWon()) { + if (won) { level += 1; - if (width < 13) { - width += 2; - } - if (height < 8) { - height += 1; - } + updateWidthHeight(); TextChannel textChannel = Bot.getShardManager().getTextChannelById(channelID); if (textChannel != null) { - textChannel.retrieveMessageById(gameMessageID) - .queue(gameMessage -> GameUtil.sendWinEmbed(guild, gameMessage, String.valueOf(level))); + queue( + textChannel.retrieveMessageById(gameMessageID), + gameMessage -> GameUtil.sendWinEmbed(guild, gameMessage, String.valueOf(level))); } grid = new Grid(width, height, level, playerEmote); } } } + + private void updateWidthHeight() { + if (width < 13) { + width += 2; + } + if (height < 8) { + height += 1; + } + } } diff --git a/src/main/java/me/polymarsdev/sokobot/util/GameUtil.java b/src/main/java/me/polymarsdev/sokobot/util/GameUtil.java index 5e8988e..8a3372e 100644 --- a/src/main/java/me/polymarsdev/sokobot/util/GameUtil.java +++ b/src/main/java/me/polymarsdev/sokobot/util/GameUtil.java @@ -71,6 +71,7 @@ public class GameUtil { Game game = games.get(playerId); long timeDifference = now - game.lastAction; if (timeDifference > 10 * 60 * 1000) { + System.out.println("[INFO] Stopped inactive game of " + playerId); game.stop(); GameUtil.removeGame(playerId); } @@ -78,4 +79,4 @@ public class GameUtil { } }, 10 * 60 * 1000, 60 * 1000); } -} +} \ No newline at end of file