From 268a46f3f70307dcc47ab56d08659685114ec69b Mon Sep 17 00:00:00 2001 From: AffluentAvo Date: Thu, 6 Aug 2020 22:46:27 +0200 Subject: [PATCH] added database made prefixes database-stored --- src/main/java/Bot.java | 49 ++++++++++++++++- src/main/java/Commands.java | 2 +- src/main/java/Database.java | 107 ++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 src/main/java/Database.java diff --git a/src/main/java/Bot.java b/src/main/java/Bot.java index 574df11..b5f2480 100644 --- a/src/main/java/Bot.java +++ b/src/main/java/Bot.java @@ -8,6 +8,8 @@ import javax.security.auth.login.LoginException; import java.io.File; 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; @@ -15,6 +17,7 @@ public class Bot { static HashMap prefixes = new HashMap<>(); private static ShardManager shardManager; + private static Database database = null; public static void main(String[] args) throws LoginException { String token = null; @@ -22,7 +25,8 @@ public class Bot { File tokenFile = Paths.get("token.txt").toFile(); if (!tokenFile.exists()) { System.out.println("[ERROR] Could not find token.txt file"); - System.out.println("[ERROR] Please create a file called \"token.txt\" in the same folder as the jar " + "file and paste in your bot token."); + System.out.println("[ERROR] Please create a file called \"token.txt\" in the same folder as the jar " + + "file and paste in your bot token."); return; } token = new String(Files.readAllBytes(tokenFile.toPath())); @@ -30,6 +34,17 @@ public class Bot { ex.printStackTrace(); } if (token == null) return; + // database = new Database(Database.DBType.SQLite); + if (database != null) { + if (!database.isConnected()) { + database = null; + System.out.println("[ERROR] Database connection failed. Continuing without database."); + } else { + database.update( + "CREATE TABLE IF NOT EXISTS guildprefix (guildId VARCHAR(18) NOT NULL, prefix VARCHAR(8) NOT " + + "NULL);"); + } + } DefaultShardManagerBuilder builder = new DefaultShardManagerBuilder(token); builder.setStatus(OnlineStatus.ONLINE); builder.setActivity(Activity.playing("@Sokobot for info!")); @@ -54,6 +69,10 @@ public class Bot { if (cmd.equalsIgnoreCase("stop")) { System.out.println("Shutting down..."); shardManager.shutdown(); + if (database != null) { + System.out.println("Disconnecting database..."); + database.disconnect(); + } System.out.println("Bye!"); System.exit(0); return; @@ -65,11 +84,37 @@ public class Bot { return shardManager; } + static void removePrefix(long guildId) { + prefixes.remove(guildId); + if (database != null) { + database.update("DELETE FROM guildprefix WHERE guildId=?;", String.valueOf(guildId)); + } + } + static void setPrefix(Guild guild, String prefix) { prefixes.put(guild.getIdLong(), prefix); + if (database != null) { + database.update("DELETE FROM guildprefix WHERE guildId=?;", guild.getId()); + database.update("INSERT INTO guildprefix VALUES (?, ?);", guild.getId(), prefix); + } } static String getPrefix(Guild guild) { - return prefixes.getOrDefault(guild.getIdLong(), "!"); + if (prefixes.containsKey(guild.getIdLong())) return prefixes.get(guild.getIdLong()); + if (database != null) { + try (ResultSet rs = database.query("SELECT prefix FROM guildprefix WHERE guildId=?;", guild.getId())) { + if (rs.next()) { + String prefix = rs.getString("prefix"); + prefixes.put(guild.getIdLong(), prefix); + return prefix; + } + prefixes.put(guild.getIdLong(), "!"); + return "!"; + } catch (SQLException ex) { + System.out.println("[ERROR] Error at retrieving guild prefix of guild id " + guild.getId() + ": " + ex + .getMessage()); + } + } + return "!"; } } \ No newline at end of file diff --git a/src/main/java/Commands.java b/src/main/java/Commands.java index 69cc57c..ac28e11 100644 --- a/src/main/java/Commands.java +++ b/src/main/java/Commands.java @@ -18,7 +18,7 @@ public class Commands extends ListenerAdapter { @Override public void onGuildLeave(GuildLeaveEvent event) { Guild guild = event.getGuild(); - Bot.prefixes.remove(guild.getIdLong()); + Bot.removePrefix(guild.getIdLong()); } @Override diff --git a/src/main/java/Database.java b/src/main/java/Database.java new file mode 100644 index 0000000..7a43a8d --- /dev/null +++ b/src/main/java/Database.java @@ -0,0 +1,107 @@ +import java.sql.*; + +public class Database { + + enum DBType {MySQL, SQLite} + + /** + * SQLite Data + * Set this data if you use DBType#SQLite + * + * @param filePath This can either be a relative or absolute path. + * ex: sokobot.db + * or: C:/sqlite/db/sokobot.db + */ + private final String filePath = "sokobot.db"; + + /** + * MySQL Data + * Set this data if you use DBType#MySQL + */ + private final String mysql_hostname = "127.0.0.1"; + private final int mysql_port = 3306; + private final String mysql_database = "sokobot"; + private final String mysql_username = "sokobot"; + private final String mysql_password = "$€cUR€_P4sSw0R!)"; + + private Connection con = null; + + public Database(DBType dbType) { + try { + if (dbType == DBType.MySQL) { + con = DriverManager.getConnection( + "jdbc:mysql://" + mysql_hostname + ":" + mysql_port + "/" + mysql_database + + "?autoReconnect=true", mysql_username, mysql_password); + } else if(dbType == DBType.SQLite){ + con = DriverManager.getConnection("jdbc:sqlite:" + filePath); + } + } catch (Exception ex) { + System.out.println("[ERROR] Error at creating database connection: " + ex.getMessage()); + } + } + + public void disconnect() { + try { + con.clearWarnings(); + con.close(); + con = null; + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + + public Connection getCon() { + return con; + } + + public ResultSet query(String sql, Object... preparedParameters) { + try { + PreparedStatement ps = con.prepareStatement(sql); + int id = 1; + for (Object preparedParameter : preparedParameters) { + ps.setObject(id, preparedParameter); + id++; + } + return ps.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + + public ResultSet query(String sql) { + try { + ResultSet rs = con.prepareStatement(sql).executeQuery(); + return rs; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + + + public void update(String sql, Object... preparedParameters) { + try (PreparedStatement ps = con.prepareStatement(sql)) { + int id = 1; + for (Object preparedParameter : preparedParameters) { + ps.setObject(id, preparedParameter); + id++; + } + ps.executeUpdate(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + + public void update(String sql) { + try (PreparedStatement ps = con.prepareStatement(sql)) { + ps.executeUpdate(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + + public boolean isConnected() { + return con != null; + } +} \ No newline at end of file