More SQL Fixes and Func Prep

This commit is contained in:
unknown 2022-03-25 22:04:24 -07:00
parent b3bbbfb9f8
commit 38a3b5e57c
6 changed files with 127 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package moe.oko.Kiafumi;
import moe.oko.Kiafumi.listener.MainListener;
import moe.oko.Kiafumi.model.KiafumiDB;
import moe.oko.Kiafumi.model.ServerManager;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
@ -43,6 +44,8 @@ public class Kiafumi {
public KiafumiDB database;
public ServerManager serverManager;
/**
* Main Class function, makes the classes work or some shit.
* @param args - Arguments for program start.
@ -111,6 +114,8 @@ public class Kiafumi {
//Initializes database and loads credentials.
database = config.createDb();
serverManager = new ServerManager();
//Makes our JDA instance.
startDiscord();
}
@ -161,4 +166,7 @@ public class Kiafumi {
//Gets the active database.
public KiafumiDB getDatabase() { return database; }
//Gets active ServerManager
public ServerManager getServerManager() { return serverManager; }
}

View File

@ -20,7 +20,7 @@ public class MainListener extends ListenerAdapter {
public void onGuildJoin(GuildJoinEvent event) {
//Automatically create our default information for the server if we don't have it already.
info("Joined a new guild, NAME: " + event.getGuild().getName() + " ID: " + event.getGuild().getId());
Kiafumi.instance.getDatabase().createServerInformation(event.getGuild());
Kiafumi.instance.getServerManager().createNewDefaultServer(event.getGuild());
}
}

View File

@ -1,9 +1,10 @@
package moe.oko.Kiafumi.model;
import com.mysql.cj.xdevapi.Type;
import net.dv8tion.jda.api.entities.Guild;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import static moe.oko.Kiafumi.Kiafumi.error;
import static moe.oko.Kiafumi.Kiafumi.info;
@ -18,9 +19,10 @@ public class KiafumiDB {
//The prepared statement strings
private final String CREATE_SERVERINFO_TABLE = "CREATE TABLE IF NOT EXISTS `serverInfo`" +
"(`id` LONGTEXT NOT NULL,`welcomeEnabled` TINYINT NOT NULL,`welcomeChannel` LONGTEXT NULL);";
private final String INSERT_SERVERINFO_DEFAULT = "INSERT INTO serverInfo(id,welcomeEnabled,welcomeChannel) values (?,?,?)";
"(`id` LONGTEXT NOT NULL,`welcomeEnabled` TINYINT NOT NULL,`welcomeChannel` LONGTEXT NULL, joinRole LONGTEXT NULL, `serverProtected` TINYINT NOT NULL);";
private final String INSERT_SERVERINFO_DEFAULT = "INSERT INTO serverInfo(id,welcomeEnabled,welcomeChannel, joinRole, serverProtected) values (?,?,?,?,?)";
private final String SERVERINFO_EXISTS = "select * from serverInfo where id = ?";
private final String SERVER_INFO_LOAD = "select * from serverInfo";
/**
* KiafumiDB Config Constructor
@ -76,6 +78,8 @@ public class KiafumiDB {
ps.setString(1, guild.getId());
ps.setInt(2, 0); //default is false.
ps.setNull(3, Types.LONGVARCHAR);
ps.setNull(4, Types.LONGVARCHAR);
ps.setInt(5, 0); //Falseeeee
ps.execute();
return true;
} catch (Exception ex) {
@ -84,4 +88,40 @@ public class KiafumiDB {
return false;
}
}
public List<Server> loadServerInformation() {
List<Server> servers = new ArrayList<>();
try {
PreparedStatement ps = connection.prepareStatement(SERVER_INFO_LOAD);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
info("Starting new load for server: " + rs.getString(1));
String id = rs.getString(1);
boolean welcomeEnabled = false;
switch(rs.getInt(2)) {
case 0:
welcomeEnabled = false;
case 1:
welcomeEnabled = true;
}
String welcomeChannel = rs.getString(3);
String joinRole = rs.getString(4);
boolean protectionEnabled = false;
switch(rs.getInt(5)) {
case 0:
protectionEnabled = false;
case 1:
protectionEnabled = true;
}
Server server = new Server(id, welcomeEnabled, welcomeChannel, joinRole, protectionEnabled);
info("Loaded: " + server);
servers.add(server);
}
return null;
} catch (Exception ex) {
ex.printStackTrace();
error("Failed to load server information, check stack.");
return null;
}
}
}

View File

@ -1,5 +1,7 @@
package moe.oko.Kiafumi.model;
import javax.annotation.Nullable;
/**
* Server Class
* Used for in-memory data storage. Loaded from Database later.
@ -8,4 +10,35 @@ public class Server {
private String id;
private boolean welcomeEnabled;
private String welcomeChannel;
private String joinRole;
private boolean serverProtected;
public Server(String id) {
this.id = id;
this.welcomeEnabled = false;
this.welcomeChannel = null;
this.joinRole = null;
this.serverProtected = false;
}
/**
* Database Constructor
* @param id - id of the server
* @param welcomeEnabled - is welcome enabled for the server
* @param welcomeChannel - channel for welcome messages, if enabled
* @param serverProtected - is the server under protection rules. (/archive, /stop, etc.)
*/
public Server(String id, boolean welcomeEnabled, @Nullable String welcomeChannel, @Nullable String joinRole, boolean serverProtected) {
this.id = id;
this.welcomeEnabled = welcomeEnabled;
this.welcomeChannel = welcomeChannel;
this.joinRole = joinRole;
this.serverProtected = serverProtected;
}
@Override
public String toString() {
return "Server{id=" + this.id + ",welcomeEnabled=" + welcomeEnabled + ",welcomeChannel="+welcomeChannel+
",joinrole=" + joinRole +",serverProtected="+serverProtected+"}";
}
}

View File

@ -0,0 +1,40 @@
package moe.oko.Kiafumi.model;
import moe.oko.Kiafumi.Kiafumi;
import net.dv8tion.jda.api.entities.Guild;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static moe.oko.Kiafumi.Kiafumi.error;
import static moe.oko.Kiafumi.Kiafumi.info;
public class ServerManager {
private HashMap<String, Server> servers = new HashMap<>();
public ServerManager() {
List<Server> loadedServers = Kiafumi.instance.database.loadServerInformation();
if(loadedServers == null) {
error("Failed to load servers properly. Null val on database.");
return;
}
servers = loadedServers;
}
public Server getOrCreateServer(String id) {
}
public boolean createNewDefaultServer(Guild guild) {
info("Started default server creation for " + guild.getId());
Server server = new Server(guild.getId());
if(Kiafumi.instance.getDatabase().createServerInformation(guild)) {
info("New defaults persistent for " + server);
return true;
} else {
error("Failed to create new defaults for " + guild.getId());
return false;
}
}
}

View File

@ -28,8 +28,8 @@ discord:
main:
#Currently, unimplemented, maybe in the future if performance becomes and issue.
sharded: false
#Activity Type (STREAMING|PLAYING|WATCHING|COMPETING|DEFAULT|CUSTOM_STATUS)
activityType: "CUSTOM_STATUS"
#Activity Type (STREAMING|PLAYING|WATCHING|COMPETING|DEFAULT)
activityType: "PLAYING"
#Activity Message
activityMsg: "+help - gaaaaay"
#Status type (ONLINE|IDLE|DO_NOT_DISTURB|INVISIBLE)