Merge pull request #33 from AffluentAvo/master

fixed startup issues
This commit is contained in:
PolyMars
2020-11-10 08:42:48 -06:00
committed by GitHub
4 changed files with 51 additions and 25 deletions
+1
View File
@@ -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/)!
### 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.
@@ -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<Long, String> prefixes = new HashMap<>();
@@ -71,10 +72,15 @@ public class Bot {
+ "NULL);");
}
}
DefaultShardManagerBuilder builder = new DefaultShardManagerBuilder(token);
List<GatewayIntent> 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(() -> {
+39 -21
View File
@@ -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<Message> restAction, Consumer<? super Message> 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;
}
}
}
@@ -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);
}