mirror of
https://github.com/opelly27/Sokobot.git
synced 2026-05-20 01:47:35 +00:00
@@ -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/)!
|
Sokobot is available on top.gg and can be added to your server [in one click](https://top.gg/bot/713635251703906336/)!
|
||||||
### Self-hosting
|
### 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.
|
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.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;
|
||||||
|
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||||
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder;
|
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder;
|
||||||
import net.dv8tion.jda.api.sharding.ShardManager;
|
import net.dv8tion.jda.api.sharding.ShardManager;
|
||||||
|
import net.dv8tion.jda.api.utils.cache.CacheFlag;
|
||||||
|
|
||||||
import javax.security.auth.login.LoginException;
|
import javax.security.auth.login.LoginException;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -16,8 +18,7 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Bot {
|
public class Bot {
|
||||||
static HashMap<Long, String> prefixes = new HashMap<>();
|
static HashMap<Long, String> prefixes = new HashMap<>();
|
||||||
@@ -71,10 +72,15 @@ public class Bot {
|
|||||||
+ "NULL);");
|
+ "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.setStatus(OnlineStatus.ONLINE);
|
||||||
builder.setActivity(Activity.playing("@Sokobot for info!"));
|
builder.setActivity(Activity.playing("@Sokobot for info!"));
|
||||||
builder.addEventListeners(new GameListener(), new CommandListener());
|
builder.addEventListeners(new GameListener(), new CommandListener());
|
||||||
|
builder.disableCache(
|
||||||
|
CacheFlag.CLIENT_STATUS, CacheFlag.ACTIVITY, CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE);
|
||||||
shardManager = builder.build();
|
shardManager = builder.build();
|
||||||
GameUtil.runGameTimer();
|
GameUtil.runGameTimer();
|
||||||
Thread consoleThread = new Thread(() -> {
|
Thread consoleThread = new Thread(() -> {
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ package me.polymarsdev.sokobot;
|
|||||||
import me.polymarsdev.sokobot.objects.Grid;
|
import me.polymarsdev.sokobot.objects.Grid;
|
||||||
import me.polymarsdev.sokobot.util.GameUtil;
|
import me.polymarsdev.sokobot.util.GameUtil;
|
||||||
import net.dv8tion.jda.api.entities.*;
|
import net.dv8tion.jda.api.entities.*;
|
||||||
|
import net.dv8tion.jda.api.requests.RestAction;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
long gameMessageID;
|
long gameMessageID;
|
||||||
@@ -34,9 +36,9 @@ public class Game {
|
|||||||
|
|
||||||
public void newGame(MessageChannel channel) {
|
public void newGame(MessageChannel channel) {
|
||||||
if (!gameActive) {
|
if (!gameActive) {
|
||||||
level = 1;
|
|
||||||
width = 9;
|
width = 9;
|
||||||
height = 6;
|
height = 6;
|
||||||
|
for (int i = 1; i < level; i++) updateWidthHeight();
|
||||||
grid = new Grid(width, height, level, playerEmote);
|
grid = new Grid(width, height, level, playerEmote);
|
||||||
gameActive = true;
|
gameActive = true;
|
||||||
lastAction = System.currentTimeMillis();
|
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() {
|
public void stop() {
|
||||||
gameActive = false;
|
gameActive = false;
|
||||||
TextChannel textChannel = Bot.getShardManager().getTextChannelById(channelID);
|
TextChannel textChannel = Bot.getShardManager().getTextChannelById(channelID);
|
||||||
@@ -53,64 +61,74 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void run(Guild guild, TextChannel channel, String userInput) {
|
public void run(Guild guild, TextChannel channel, String userInput) {
|
||||||
lastAction = System.currentTimeMillis();
|
|
||||||
if (userInput.equals("stop") && gameActive) {
|
if (userInput.equals("stop") && gameActive) {
|
||||||
stop();
|
stop();
|
||||||
channel.sendMessage("Thanks for playing, " + user.getAsMention() + "!")
|
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) {
|
if (userInput.equals("play") && !gameActive) {
|
||||||
newGame(channel);
|
newGame(channel);
|
||||||
} else if (gameActive) {
|
} else if (gameActive) {
|
||||||
if (!grid.hasWon()) {
|
lastAction = System.currentTimeMillis();
|
||||||
String direction = userInput;
|
boolean won = grid.hasWon();
|
||||||
switch (direction) {
|
if (!won) {
|
||||||
|
boolean moved = false;
|
||||||
|
switch (userInput) {
|
||||||
case "up":
|
case "up":
|
||||||
case "w":
|
case "w":
|
||||||
grid.getPlayer().moveUp();
|
moved = grid.getPlayer().moveUp();
|
||||||
break;
|
break;
|
||||||
case "down":
|
case "down":
|
||||||
case "s":
|
case "s":
|
||||||
grid.getPlayer().moveDown();
|
moved = grid.getPlayer().moveDown();
|
||||||
break;
|
break;
|
||||||
case "left":
|
case "left":
|
||||||
case "a":
|
case "a":
|
||||||
grid.getPlayer().moveLeft();
|
moved = grid.getPlayer().moveLeft();
|
||||||
break;
|
break;
|
||||||
case "right":
|
case "right":
|
||||||
case "d":
|
case "d":
|
||||||
grid.getPlayer().moveRight();
|
moved = grid.getPlayer().moveRight();
|
||||||
break;
|
break;
|
||||||
case "mr":
|
case "mr":
|
||||||
grid.resetMap();
|
grid.resetMap();
|
||||||
|
moved = true;
|
||||||
break;
|
break;
|
||||||
case "r":
|
case "r":
|
||||||
grid.reset();
|
grid.reset();
|
||||||
|
moved = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!grid.hasWon()) {
|
grid.updateGrid();
|
||||||
|
won = grid.hasWon();
|
||||||
|
if (!won && moved) {
|
||||||
TextChannel textChannel = Bot.getShardManager().getTextChannelById(channelID);
|
TextChannel textChannel = Bot.getShardManager().getTextChannelById(channelID);
|
||||||
if (textChannel != null) {
|
if (textChannel != null) {
|
||||||
textChannel.retrieveMessageById(gameMessageID).queue(gameMessage -> GameUtil
|
queue(textChannel.retrieveMessageById(gameMessageID), gameMessage -> GameUtil
|
||||||
.updateGameEmbed(gameMessage, String.valueOf(level), grid.toString(), user));
|
.updateGameEmbed(gameMessage, String.valueOf(level), grid.toString(), user));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (grid.hasWon()) {
|
if (won) {
|
||||||
level += 1;
|
level += 1;
|
||||||
if (width < 13) {
|
updateWidthHeight();
|
||||||
width += 2;
|
|
||||||
}
|
|
||||||
if (height < 8) {
|
|
||||||
height += 1;
|
|
||||||
}
|
|
||||||
TextChannel textChannel = Bot.getShardManager().getTextChannelById(channelID);
|
TextChannel textChannel = Bot.getShardManager().getTextChannelById(channelID);
|
||||||
if (textChannel != null) {
|
if (textChannel != null) {
|
||||||
textChannel.retrieveMessageById(gameMessageID)
|
queue(
|
||||||
.queue(gameMessage -> GameUtil.sendWinEmbed(guild, gameMessage, String.valueOf(level)));
|
textChannel.retrieveMessageById(gameMessageID),
|
||||||
|
gameMessage -> GameUtil.sendWinEmbed(guild, gameMessage, String.valueOf(level)));
|
||||||
}
|
}
|
||||||
grid = new Grid(width, height, level, playerEmote);
|
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);
|
Game game = games.get(playerId);
|
||||||
long timeDifference = now - game.lastAction;
|
long timeDifference = now - game.lastAction;
|
||||||
if (timeDifference > 10 * 60 * 1000) {
|
if (timeDifference > 10 * 60 * 1000) {
|
||||||
|
System.out.println("[INFO] Stopped inactive game of " + playerId);
|
||||||
game.stop();
|
game.stop();
|
||||||
GameUtil.removeGame(playerId);
|
GameUtil.removeGame(playerId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user