17 Commits

Author SHA1 Message Date
PocketMars 72cf1e742c Merge branch 'master' of https://github.com/PolyMarsDev/Sokobot 2020-06-30 13:30:36 -05:00
PocketMars 75ff975e58 Updated version in build.gradle 2020-06-30 13:30:31 -05:00
PolyMars e69a4672f0 More screenshot changes 2020-06-30 11:57:41 -05:00
PolyMars 396016eeba Added more screenshots to readme 2020-06-30 11:55:14 -05:00
PolyMars 7c2a25bbcf Add updated screenshots to readme 2020-06-30 11:49:25 -05:00
PocketMars f57b271bbf Custom player emojis
Allow player to use any emoji in place of the player by using !play [valid emoji] when starting a game
2020-06-30 11:35:03 -05:00
PocketMars ecf173568e Merge branch 'master' of https://github.com/PolyMarsDev/Sokobot 2020-06-30 10:21:35 -05:00
PocketMars d1077ca9ab Update info command
The info command now reflects new features added in v1.1
2020-06-30 10:21:32 -05:00
PolyMars f39d3a7ac8 Update README.md to reflect new update 2020-06-30 10:01:51 -05:00
PocketMars 9de5a2ae36 Merge branch 'master' of https://github.com/PolyMarsDev/Sokobot 2020-06-30 09:40:32 -05:00
PocketMars 79a09651ab Preparations for public host
This commit adds a lot of new features, optimizations and bugfixes to prepare the bot to be hosted publicly. Updates include a prefix changing command, fixes with box generation to prevent softlocks in later levels, and making edits to game messages instead of sending a new message after each update. Smaller optimizations were made as well, such as removing games from the HashMap when a user quits to save memory.
2020-06-30 09:40:28 -05:00
PolyMars cf5e579e9d Update README.md 2020-06-12 20:19:47 -05:00
PolyMars 9347174aeb Update README.md 2020-06-12 20:19:22 -05:00
PolyMars 584cf828d5 Update README.md 2020-06-12 20:16:34 -05:00
PolyMars c9522a4362 Update README.md 2020-06-12 20:15:51 -05:00
PolyMars 3d76bc15d4 Create README.md 2020-06-12 20:15:28 -05:00
PocketMars 89bd9a8f30 Update Commands.java
the source code link now directs you to this repo
2020-06-12 19:44:19 -05:00
7 changed files with 192 additions and 43 deletions
+42
View File
@@ -0,0 +1,42 @@
# Sokobot
Sokobot is a Discord bot written with [JDA](https://github.com/DV8FromTheWorld/JDA) that lets you play [Sokoban](https://en.wikipedia.org/wiki/Sokoban), the classic box-pushing puzzle game.
## Screenshots
![Level 1](https://cdn.discordapp.com/attachments/670425377503707146/727568442034487316/sokobot_v1.1.gif)
![Level 2](https://cdn.discordapp.com/attachments/670425377503707146/727567694597193829/sokobot_v1.1_.gif)
## Features
### Infinite levels
The maps in Sokobot are randomly generated, increasing in difficulty as you progress.
### Varied controls
Sokobot has multiple control options to improve the player's experience, including reactions and wasd commands!
### Simultaneous games
Thanks to the power of Java HashMaps™️, multiple users can use the bot at the same time without interfering with one another.
### Custom prefixes ``New!``
To prevent Sokobot from conflicting with other bots, admins can choose any single-character prefix to preface Sokobot's commands.
## Commands
### User
- ``!play`` can be used to start a game if you are not currently in one.
- ``!stop`` can be used to stop your active game at any time.
- ``!info`` provides some useful details about the bot and rules of the game.
### Admin ``New!``
- ``!prefix [character]`` can be used to change the prefix the bot responds to in the current server.
## Usage
### Public host ``New!``
Will be available very soon on [top.gg](https://top.gg)!
### 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.
## Compiling
Install [Java 8 JDK](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) and [Gradle](https://gradle.org/).
In the root folder of the project, execute ``gradlew shadowJar``.
The compiled .jar file will be located in ``build/libs``.
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Feel free to create a fork and use the code for any noncommercial purposes.
+2 -1
View File
@@ -5,7 +5,7 @@ plugins {
}
group 'com.polymars'
version '1.0'
version '1.1'
mainClassName = "Bot"
sourceCompatibility = 1.8;
@@ -17,4 +17,5 @@ repositories {
dependencies {
compile 'net.dv8tion:JDA:4.1.1_162'
compile 'com.vdurmont:emoji-java:5.1.1'
}
+18 -2
View File
@@ -2,22 +2,38 @@ import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import javax.security.auth.login.LoginException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
public class Bot {
static String prefix = "!";
static HashMap<Guild, String> prefixes = new HashMap<Guild, String>();
public static void main(String[] args) throws LoginException, IOException {
JDABuilder builder = new JDABuilder(AccountType.BOT);
String token = new String(Files.readAllBytes(Paths.get("token.txt")));
builder.setToken(token);
builder.setStatus(OnlineStatus.ONLINE);
builder.setActivity(Activity.playing("!play to play Sokoban!"));
builder.setActivity(Activity.playing("@Sokobot for info!"));
builder.addEventListeners(new Commands());
builder.build();
}
static void setPrefix(Guild guild, String prefix)
{
prefixes.put(guild, prefix);
}
static String getPrefix(Guild guild)
{
if (!prefixes.containsKey(guild))
{
return "!";
}
return prefixes.get(guild);
}
}
+85 -19
View File
@@ -1,17 +1,32 @@
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.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import com.vdurmont.emoji.EmojiManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class Commands extends ListenerAdapter {
HashMap<User, Game> games = new HashMap<User, Game>();
ArrayList<String> commands = new ArrayList<String>(Arrays.asList("w", "a", "s", "d", "up", "left", "down", "right", "r", Bot.prefix + "play", Bot.prefix + "continue", Bot.prefix + "stop"));
ArrayList<String> commandsPrefix = new ArrayList<String>(Arrays.asList("play", "continue", "stop"));
ArrayList<String> commandsNoPrefix = new ArrayList<String>(Arrays.asList("w", "a", "s", "d", "up", "left", "down", "right", "r"));
public void onGuildLeave(GuildLeaveEvent event) //removes bot's stored prefix for a server if removed from that server
{
if (Bot.prefixes.containsKey(event.getGuild()))
{
Bot.prefixes.remove(event.getGuild());
}
}
public void onGuildMessageReceived(GuildMessageReceivedEvent event)
{
if (event.getAuthor().isBot() && event.getMessage().getEmbeds().get(0).getTitle().charAt(0) == 'L')
@@ -21,26 +36,60 @@ public class Commands extends ListenerAdapter {
event.getMessage().addReaction("U+2B06").queue();
event.getMessage().addReaction("U+2B07").queue();
event.getMessage().addReaction("U+1F504").queue();
if (games.containsKey(event.getJDA().getUserById(event.getMessage().getEmbeds().get(0).getFields().get(0).getValue().substring(10, event.getMessage().getEmbeds().get(0).getFields().get(0).getValue().length() - 1))))
{
games.get(event.getJDA().getUserById(event.getMessage().getEmbeds().get(0).getFields().get(0).getValue().substring(10, event.getMessage().getEmbeds().get(0).getFields().get(0).getValue().length() - 1))).setGameMessage(event.getMessage());
}
return;
}
String[] args = event.getMessage().getContentRaw().split("\\s+");
if (args[0].toLowerCase().equals(Bot.prefix + "info"))
String[] args = event.getMessage().getContentRaw().split("\\s+");
if (args[0].toLowerCase().equals(Bot.getPrefix(event.getGuild()) + "prefix"))
{
event.getChannel().sendMessage(info().build()).queue();
if (event.getMember().hasPermission(Permission.ADMINISTRATOR)) {
if (args.length == 2 && args[1].length() == 1) {
Bot.setPrefix(event.getGuild(), args[1].toLowerCase());
event.getChannel().sendMessage("Prefix successfully changed to ``" + Bot.getPrefix(event.getGuild()) + "``.").queue();
} else {
event.getChannel().sendMessage("``" + args[1] + "`` is not a valid prefix!").queue();
}
else if (commands.contains(args[0].toLowerCase()))
event.getMessage().delete().queue();
}
else
{
event.getChannel().sendMessage(event.getAuthor().getAsMention() + ", you do not have permission to use this command.").queue();
}
}
else if ((commandsNoPrefix.contains(args[0].toLowerCase())) || (Character.toString(args[0].toLowerCase().charAt(0)).equals(Bot.getPrefix(event.getGuild())) && commandsPrefix.contains(args[0].toLowerCase().substring(1))))
{
if (!games.containsKey(event.getAuthor()))
{
games.put(event.getAuthor(), new Game());
games.put(event.getAuthor(), new Game(event.getAuthor()));
}
String userInput = args[0].toLowerCase();
if (Character.toString(userInput.charAt(0)).equals(Bot.prefix))
if (Character.toString(userInput.charAt(0)).equals(Bot.getPrefix(event.getGuild())))
{
userInput = userInput.substring(1, userInput.length());
}
games.get(event.getAuthor()).run(event.getChannel(), userInput);
if (!games.get(event.getAuthor()).gameActive && userInput.equals("play") && args.length == 2 && EmojiManager.isEmoji(args[1]))
{
System.out.println(args[1]);
games.get(event.getAuthor()).setPlayerEmote(args[1]);
}
games.get(event.getAuthor()).run(event.getGuild(), event.getChannel(), userInput);
if (userInput.equals("stop")) //remove game from hashmap when player quits
{
if (games.containsKey(event.getAuthor()))
{
games.remove(event.getAuthor());
}
}
event.getMessage().delete().queue();
}
else if (args[0].toLowerCase().equals(Bot.getPrefix(event.getGuild()) + "info") || event.getMessage().getMentionedUsers().get(0).equals(event.getJDA().getSelfUser()))
{
event.getChannel().sendMessage(info(event.getGuild()).build()).queue();
event.getMessage().delete().queue();
}
}
public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
@@ -50,8 +99,9 @@ public class Commands extends ListenerAdapter {
}
if (!games.containsKey(event.getMember().getUser()))
{
games.put(event.getMember().getUser(), new Game());
games.put(event.getMember().getUser(), new Game(event.getMember().getUser()));
}
boolean reactionCommand = true;
String userInput = "";
switch (event.getReactionEmote().toString())
{
@@ -70,10 +120,16 @@ public class Commands extends ListenerAdapter {
case "RE:U+1f504":
userInput = "r";
break;
default:
reactionCommand = false;
}
games.get(event.getMember().getUser()).run(event.getChannel(), userInput);
if (reactionCommand)
{
games.get(event.getMember().getUser()).run(event.getGuild(), event.getChannel(), userInput);
}
EmbedBuilder info()
event.getReaction().removeReaction(event.getUser()).queue();
}
EmbedBuilder info(Guild guild)
{
EmbedBuilder info = new EmbedBuilder();
info.setTitle("Sokobot");
@@ -81,25 +137,35 @@ public class Commands extends ListenerAdapter {
info.setDescription("Sokobot is a bot that lets you play Sokoban, the classic box-pushing puzzle game.");
info.setColor(0xdd2e53);
info.addField("How to Play", "You are a **Sokoban** :flushed:.\nYour job is to push **boxes** :brown_square: on top of their **destinations** :negative_squared_cross_mark:.", false);
info.addField("Features", ":white_small_square:**Infinite levels**\nThe fun never ends thanks to endless, randomly generated maps of increasing difficulty!\n:white_small_square:**Varied controls**\nSokobot has multiple control options to improve the player's experience, including reactions and wasd commands!\n:white_small_square:**Simultaneous games**\nThanks to the power of Java HashMaps:tm:, multiple users can use the bot at the same time without interfering with eachothers' games.", false);
info.addField("Commands", ("``" + Bot.prefix + "play`` can be used to start a game if you are not currently in one.\n``" + Bot.prefix + "stop`` can be used to stop your active game at any time.\n``" + Bot.prefix + "info`` provides some useful details about the bot."), false);
info.addField("Source code", "https://github.com/polymarsdev", false);
info.addField("Features", ":white_small_square:**Infinite levels**\nThe maps in Sokobot are randomly generated, increasing in difficulty as you progress.\n:white_small_square:**Varied controls**\nSokobot has multiple control options to improve the player's experience, including reactions and wasd commands!\n:white_small_square:**Simultaneous games**\nThanks to the power of 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", ("``" + Bot.getPrefix(guild) + "play`` can be used to start a game if you are not currently in one.\n``" + Bot.getPrefix(guild) + "stop`` can be used to stop your active game at any time.\n``" + Bot.getPrefix(guild) + "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/to-be-added", false);
info.addField("Source code", "https://github.com/PolyMarsDev/Sokobot", false);
info.setFooter("created by PolyMars", "https://avatars0.githubusercontent.com/u/51007356?s=460&u=4eb8fd498421a2eee9781edfbadf654386cf06c7&v=4");
return info;
}
public static void sendGameEmbed(MessageChannel channel, String level, String game)
public static void sendGameEmbed(MessageChannel channel, String level, String game, User user)
{
EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("Level " + level);
embed.setDescription(game);
embed.setFooter("Enter direction (up, down, left, right, or wasd) or r to reset");
embed.addField("Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``) or ``r`` to reset", "Player: " + user.getAsMention(), false);
channel.sendMessage(embed.build()).queue();
}
public static void sendWinEmbed(MessageChannel channel, String level)
public static void updateGameEmbed(Message message, String level, String game, User user)
{
EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("Level " + level);
embed.setDescription(game);
embed.addField("Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``) or ``r`` to reset", "Player: " + user.getAsMention(), false);
message.editMessage(embed.build()).queue();
}
public static void sendWinEmbed(Guild guild, Message message, String level)
{
EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("You win!");
embed.setDescription("Type ``" + Bot.prefix + "continue`` to continue to Level " + level + " or ``" + Bot.prefix + "stop`` to quit ");
channel.sendMessage(embed.build()).queue();
embed.setDescription("Type ``" + Bot.getPrefix(guild) + "continue`` to continue to Level " + level + " or ``" + Bot.getPrefix(guild) + "stop`` to quit ");
embed.setFooter("You can also press any reaction to continue.");
message.editMessage(embed.build()).queue();
}
}
+28 -9
View File
@@ -1,11 +1,29 @@
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.User;
public class Game {
boolean gameActive = false;
Message gameMessage;
User user;
String playerEmote = ":flushed:";
public boolean gameActive = false;
public int level = 1;
int width = 9;
int height = 6;
Grid grid = new Grid(width, height, level);
Grid grid = new Grid(width, height, level, playerEmote);
public Game(User user)
{
this.user = user;
}
public void setPlayerEmote(String emote)
{
playerEmote = emote;
}
public void setGameMessage(Message message)
{
gameMessage = message;
}
public void newGame(MessageChannel channel)
{
if (!gameActive)
@@ -13,17 +31,18 @@ public class Game {
level = 1;
width = 9;
height = 6;
grid = new Grid(width, height, level);
grid = new Grid(width, height, level, playerEmote);
gameActive = true;
Commands.sendGameEmbed(channel, String.valueOf(level), grid.toString());
Commands.sendGameEmbed(channel, String.valueOf(level), grid.toString(), user);
}
}
public void run(MessageChannel channel, String userInput)
public void run(Guild guild, MessageChannel channel, String userInput)
{
if (userInput.equals("stop") && gameActive)
{
channel.sendMessage("Thanks for playing!").queue();
channel.sendMessage("Thanks for playing, " + user.getAsMention() + "!").queue();
gameActive = false;
}
@@ -53,7 +72,7 @@ public class Game {
grid.reset();
}
if (!grid.hasWon()) { //need to check again
Commands.sendGameEmbed(channel, String.valueOf(level), grid.toString());
Commands.updateGameEmbed(gameMessage, String.valueOf(level), grid.toString(), user);
}
}
if (grid.hasWon()) {
@@ -65,8 +84,8 @@ public class Game {
{
height += 1;
}
Commands.sendWinEmbed(channel, String.valueOf(level));
grid = new Grid(width, height, level);
Commands.sendWinEmbed(guild, gameMessage, String.valueOf(level));
grid = new Grid(width, height, level, playerEmote);
}
}
}
+10 -8
View File
@@ -14,9 +14,11 @@ public class Grid
int width = 0;
int color = 0;
Player player;
String playerEmote;
Randomizer rand = new Randomizer();
public Grid(int width, int height, int boxCount) //create a random grid with specific width, height, and number of boxes
public Grid(int width, int height, int boxCount, String playerEmote) //create a random grid with specific width, height, and number of boxes
{
this.playerEmote = playerEmote;
player = new Player(2, 2, this);
if (boxCount > MAX_BOXES)
{
@@ -97,7 +99,7 @@ public class Grid
int y = rand.nextInt(height - 4) + 2;
for (int j = 0; j < i; j++)
{
while ((x == boxes[j].getX() && y == boxes[j].getY()) || (x == 2 && y == 2))
while ((x == boxes[j].getX() && y == boxes[j].getY()) || (x == 2 && y == 2) || (x - 1 == boxes[j].getX() && y == boxes[j].getY()) || (x + 1 == boxes[j].getX() && y == boxes[j].getY()))
{
x = rand.nextInt(width - 4) + 2;
y = rand.nextInt(height - 4) + 2;
@@ -129,21 +131,21 @@ public class Grid
{
for (int j = 0; j < width; j++)
{
grid[j][i] = new Tile(GROUND);
grid[j][i] = new Tile(GROUND, playerEmote);
if (j == 0 || j == width - 1 || i == 0 || i == height - 1)
{
grid[j][i] = new Tile(WALL, color);
grid[j][i] = new Tile(WALL, color, playerEmote);
}
for (int k = 0; k < boxCount; k++)
{
if (destinations[k].getX() == j && destinations[k].getY() == i)
{
grid[j][i] = new Tile(DESTINATION);
grid[j][i] = new Tile(DESTINATION, playerEmote);
}
}
if (player.getX() == j && player.getY() == i)
{
grid[j][i] = new Tile(PLAYER);
grid[j][i] = new Tile(PLAYER, playerEmote);
}
for (int k = 0; k < boxCount; k++)
{
@@ -151,9 +153,9 @@ public class Grid
{
if (boxes[k].onDestination())
{
grid[j][i] = new Tile(WALL, color);
grid[j][i] = new Tile(WALL, color, playerEmote);
} else {
grid[j][i] = new Tile(BOX);
grid[j][i] = new Tile(BOX, playerEmote);
}
}
+6 -3
View File
@@ -7,14 +7,17 @@ public class Tile
final int PLAYER = 4;
int color = 0;
int status = 0;
public Tile(int status)
String playerEmote;
public Tile(int status, String playerEmote)
{
this.status = status;
this.playerEmote = playerEmote;
}
public Tile(int status, int color)
public Tile(int status, int color, String playerEmote)
{
this.status = status;
this.color = color;
this.playerEmote = playerEmote;
}
public void setStatus(int status)
{
@@ -60,6 +63,6 @@ public class Tile
{
return ":negative_squared_cross_mark:";
}
return ":flushed:";
return playerEmote;
}
}