mirror of
https://github.com/opelly27/Sokobot.git
synced 2026-05-20 01:47:35 +00:00
added database
made prefixes database-stored
This commit is contained in:
+47
-2
@@ -8,6 +8,8 @@ import javax.security.auth.login.LoginException;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
@@ -15,6 +17,7 @@ public class Bot {
|
|||||||
static HashMap<Long, String> prefixes = new HashMap<>();
|
static HashMap<Long, String> prefixes = new HashMap<>();
|
||||||
|
|
||||||
private static ShardManager shardManager;
|
private static ShardManager shardManager;
|
||||||
|
private static Database database = null;
|
||||||
|
|
||||||
public static void main(String[] args) throws LoginException {
|
public static void main(String[] args) throws LoginException {
|
||||||
String token = null;
|
String token = null;
|
||||||
@@ -22,7 +25,8 @@ public class Bot {
|
|||||||
File tokenFile = Paths.get("token.txt").toFile();
|
File tokenFile = Paths.get("token.txt").toFile();
|
||||||
if (!tokenFile.exists()) {
|
if (!tokenFile.exists()) {
|
||||||
System.out.println("[ERROR] Could not find token.txt file");
|
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;
|
return;
|
||||||
}
|
}
|
||||||
token = new String(Files.readAllBytes(tokenFile.toPath()));
|
token = new String(Files.readAllBytes(tokenFile.toPath()));
|
||||||
@@ -30,6 +34,17 @@ public class Bot {
|
|||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
if (token == null) return;
|
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);
|
DefaultShardManagerBuilder builder = new DefaultShardManagerBuilder(token);
|
||||||
builder.setStatus(OnlineStatus.ONLINE);
|
builder.setStatus(OnlineStatus.ONLINE);
|
||||||
builder.setActivity(Activity.playing("@Sokobot for info!"));
|
builder.setActivity(Activity.playing("@Sokobot for info!"));
|
||||||
@@ -54,6 +69,10 @@ public class Bot {
|
|||||||
if (cmd.equalsIgnoreCase("stop")) {
|
if (cmd.equalsIgnoreCase("stop")) {
|
||||||
System.out.println("Shutting down...");
|
System.out.println("Shutting down...");
|
||||||
shardManager.shutdown();
|
shardManager.shutdown();
|
||||||
|
if (database != null) {
|
||||||
|
System.out.println("Disconnecting database...");
|
||||||
|
database.disconnect();
|
||||||
|
}
|
||||||
System.out.println("Bye!");
|
System.out.println("Bye!");
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
return;
|
return;
|
||||||
@@ -65,11 +84,37 @@ public class Bot {
|
|||||||
return shardManager;
|
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) {
|
static void setPrefix(Guild guild, String prefix) {
|
||||||
prefixes.put(guild.getIdLong(), 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) {
|
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 "!";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,7 @@ public class Commands extends ListenerAdapter {
|
|||||||
@Override
|
@Override
|
||||||
public void onGuildLeave(GuildLeaveEvent event) {
|
public void onGuildLeave(GuildLeaveEvent event) {
|
||||||
Guild guild = event.getGuild();
|
Guild guild = event.getGuild();
|
||||||
Bot.prefixes.remove(guild.getIdLong());
|
Bot.removePrefix(guild.getIdLong());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user