Persistence is now done

This commit is contained in:
unknown 2022-03-28 12:38:25 -07:00
parent 69f6092570
commit 88d5ca2ab3
5 changed files with 85 additions and 15 deletions

View file

@ -143,7 +143,13 @@ public class Kiafumi {
* Ran on program shutdown.
*/
public void stop() {
if(database.saveServerInformation()) {
info("Successfully saved server information. Shutting down peacefully.");
} else {
for(int i = 0; i < 15; i++) {
error("FAILED TO SAVE SERVER INFORMATION.");
}
}
}
/**
@ -199,7 +205,7 @@ public class Kiafumi {
if(ci.hasOptions()) {
CommandCreateAction cca = guild.upsertCommand(ci.getName(), ci.getDescription());
for(String name : ci.getOptions().keySet()) {
//Any intelligent id will rage about the option not being used, it's added to the action then executed later, DO not edit this (please).
//Any intelligent IDE will rage about the option not being used, it's added to the action then executed later, DO not edit this (please).
cca.addOption(ci.getOptions().get(name), name, ci.getOptionDescriptions().get(name), ci.getOptionRequirements().get(name));
}
//Done

View file

@ -8,10 +8,9 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEve
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import java.awt.*;
import java.awt.Color;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class SettingCommand extends CommandClass {
@ -99,7 +98,7 @@ public class SettingCommand extends CommandClass {
@Override
public List<CommandInfo> getSlashCommandInfo() {
List<CommandInfo> si = new ArrayList<>();
CommandInfo ci = new CommandInfo("setting", "displays all available settings for the current guild");
CommandInfo ci = new CommandInfo("setting", "displays a specific setting for the current guild");
si.add(ci);
CommandInfo ci2 = new CommandInfo("setting", "views the current value for the setting");
@ -114,6 +113,9 @@ public class SettingCommand extends CommandClass {
CommandInfo ci4 = new CommandInfo("setting_clear", "reverts a setting back to its default value");
ci4.addOption("setting_name", "Name of the setting to clear", OptionType.STRING, true);
si.add(ci4);
CommandInfo ci5 = new CommandInfo("settings", "displays all settings available for the guild.");
si.add(ci5);
return si;
}
}

View file

@ -1,9 +1,11 @@
package moe.oko.Kiafumi.model;
import moe.oko.Kiafumi.Kiafumi;
import net.dv8tion.jda.api.entities.Guild;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static moe.oko.Kiafumi.Kiafumi.error;
@ -23,6 +25,8 @@ public class KiafumiDB {
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";
//1 = welcomeEnabled, 2 = welcomeChannel, 3 = joinRole, 4 = serverProtected, 5 = Guild_ID (non-changeable)
private final String SERVER_INFO_MODIFY = "UPDATE `serverInfo` SET welcomeEnabled=?, welcomeChannel=?, joinRole=?,serverProtected=? WHERE id = ?";
/**
* KiafumiDB Config Constructor
@ -128,4 +132,48 @@ public class KiafumiDB {
return null;
}
}
public boolean saveServerInformation() {
Collection<Server> servers = Kiafumi.instance.getServerManager().getServers();
info("Starting save on " + servers.size() + " servers.");
try {
//1 = welcomeEnabled, 2 = welcomeChannel, 3 = joinRole, 4 = serverProtected, 5 = Guild_ID (non-changeable)
PreparedStatement ps = connection.prepareStatement(SERVER_INFO_MODIFY);
int i = 0;
for (Server server : servers) {
if(!server.isModified()) { continue; } //Skip, non modified server.
info("Starting save on modified " + server);
if(server.isWelcomeEnabled()) {
ps.setInt(1, 1);
} else {
ps.setInt(1, 0);
}
if(server.getWelcomeChannel() != null) {
ps.setString(2, server.getWelcomeChannel());
} else {
ps.setNull(2, Types.LONGVARCHAR);
}
if(server.getJoinRole() != null) {
ps.setString(3, server.getJoinRole());
} else {
ps.setNull(3, Types.LONGVARCHAR);
}
if(server.isServerProtected()) {
ps.setInt(4, 1);
} else {
ps.setInt(4, 0);
}
ps.setString(5, server.getId());
ps.addBatch();
i++;
}
info("Total Batches: " + i + ". Starting SQL save.");
ps.executeBatch();
return true;
} catch (Exception ex) {
error("Failed to persist server data. Check stack.");
ex.printStackTrace();
return false;
}
}
}

View file

@ -66,6 +66,8 @@ public class Server {
return welcomeChannel;
}
public boolean isServerProtected() { return serverProtected; }
public void setJoinRole(String joinRole) {
this.modified = true;
this.joinRole = joinRole;
@ -139,6 +141,7 @@ public class Server {
}
public String setOptionByString(String name, String value) {
modified = true; //If this is being used just tell it it's been modified.
switch (name.toLowerCase()) {
case "welcomeenabled":
if(value.equalsIgnoreCase("false")) {
@ -151,18 +154,26 @@ public class Server {
return "Bad Value, use either true or false";
}
case "joinrole":
if(Kiafumi.JDA.getRoleById(value) == null) {
return "That role ID is invalid.";
} else {
joinRole = value;
return "Successfully set joinRole ID to " + value;
try {
if (Kiafumi.JDA.getRoleById(value) == null) {
return "That role ID is invalid.";
} else {
joinRole = value;
return "Successfully set joinRole ID to " + value;
}
} catch (Exception ex) {
return "Bad Value";
}
case "welcomechannel":
if(Kiafumi.JDA.getTextChannelById(value) == null) {
return "That channel ID is invalid.";
} else {
welcomeChannel = value;
return "Successfully set welcomeChannel ID to " + value;
try {
if (Kiafumi.JDA.getTextChannelById(value) == null) {
return "That channel ID is invalid.";
} else {
welcomeChannel = value;
return "Successfully set welcomeChannel ID to " + value;
}
} catch (Exception ex) {
return "Bad Value";
}
case "serverprotected":
if(value.equalsIgnoreCase("false")) {

View file

@ -3,6 +3,7 @@ package moe.oko.Kiafumi.model;
import moe.oko.Kiafumi.Kiafumi;
import net.dv8tion.jda.api.entities.Guild;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@ -72,4 +73,6 @@ public class ServerManager {
return false;
}
}
public Collection<Server> getServers() { return servers.values(); }
}