Commentation for stupid dummiehead <3

This commit is contained in:
unknown 2022-03-23 21:28:51 -07:00
parent bbe4915045
commit b3bbbfb9f8
6 changed files with 68 additions and 4 deletions

View File

@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>moe.kayla</groupId>
<artifactId>Shikinami</artifactId>
<version>DEV-1</version>
<groupId>moe.oko</groupId>
<artifactId>Kiafumi</artifactId>
<version>0.1</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>

View File

@ -22,6 +22,9 @@ import java.io.InputStream;
import java.nio.file.Files;
import java.time.temporal.TemporalAccessor;
/**
* Kiafumi Main Class
*/
public class Kiafumi {
private File CONFIG_FILE = new File("config.yml");
@ -40,6 +43,10 @@ public class Kiafumi {
public KiafumiDB database;
/**
* Main Class function, makes the classes work or some shit.
* @param args - Arguments for program start.
*/
public static void main(String[] args) {
try {
Kiafumi kia = new Kiafumi();
@ -50,14 +57,19 @@ public class Kiafumi {
}
}
/**
* Ran on program start. Anything in here can determine whether the program will start.
*/
public void start() {
instance = this;
logger.info("Starting Kiafumi.");
//Logger check
System.out.println("If no other messages are present, logger failed to instantiate.");
logger.info("Config load start...");
//Ensuring the configuration file is generated and/or exists.
if (!CONFIG_FILE.exists()) {
try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("config.yml")) {
//Save the default cfg
@ -69,6 +81,7 @@ public class Kiafumi {
}
}
//Try to load configuration into our local friendly configuration API.
try {
yamlConfiguration.load("config.yml");
} catch (FileNotFoundException e) {
@ -79,6 +92,7 @@ public class Kiafumi {
e.printStackTrace();
}
//Initializes our configuration helper & ensures it loads properly.
config = new KiafumiConfig(yamlConfiguration);
if(config.load()) {
logger.info("Config loaded, proceeding.");
@ -86,20 +100,31 @@ public class Kiafumi {
logger.error("Failed to load configuration. Stopping process.");
Runtime.getRuntime().exit(0);
}
//Registers the stop() function if the program is stopped.
logger.info("Registering shutdown hook.");
Runtime.getRuntime().addShutdownHook(new Thread(this::stop));
//We have the prefix as a static thing that can be referenced anywhere, this is for simplicity.
PREFIX = config.getPrefix();
//Initializes database and loads credentials.
database = config.createDb();
//Makes our JDA instance.
startDiscord();
}
/**
* Ran on program shutdown.
*/
public void stop() {
}
/**
* Starts a JDA Instance.
*/
public void startDiscord() {
try {
JDA = JDABuilder.create(config.getToken(), GatewayIntent.GUILD_MEMBERS,
@ -134,5 +159,6 @@ public class Kiafumi {
instance.logger.error(str);
}
//Gets the active database.
public KiafumiDB getDatabase() { return database; }
}

View File

@ -7,8 +7,13 @@ import org.simpleyaml.configuration.file.YamlConfiguration;
import static moe.oko.Kiafumi.Kiafumi.error;
import static moe.oko.Kiafumi.Kiafumi.info;
/**
* KiafumiConfig class
* Helps out with loading things from config and fetching them in a easy manner.
*/
public class KiafumiConfig {
//Our nice YamlConfiguration class.
public YamlConfiguration configuration;
/*

View File

@ -6,8 +6,16 @@ import net.dv8tion.jda.api.hooks.ListenerAdapter;
import static moe.oko.Kiafumi.Kiafumi.info;
/**
* Main Listener
* Used for all utility garbage like guild handling and persistence. (Also LOL prevention).
*/
public class MainListener extends ListenerAdapter {
/**
* GuildJoin event listener, that ensures that a discord has a profile created for it.
* @param event - event to be handled...
*/
@Override
public void onGuildJoin(GuildJoinEvent event) {
//Automatically create our default information for the server if we don't have it already.

View File

@ -8,15 +8,28 @@ import java.sql.*;
import static moe.oko.Kiafumi.Kiafumi.error;
import static moe.oko.Kiafumi.Kiafumi.info;
/**
* Kiafumi DB Class
* Basically our helpful MySQL functions that pertain to data persistence.
*/
public class KiafumiDB {
//Our actual MySQL Connection, this is created on class construction.
private Connection connection;
//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 (?,?,?)";
private final String SERVERINFO_EXISTS = "select * from serverInfo where id = ?";
/**
* KiafumiDB Config Constructor
* @param username - username to access database
* @param password - password to access database
* @param host - host for server
* @param port - port for server (3306 is default)
* @param db - database name
*/
public KiafumiDB(String username, String password, String host, int port, String db) {
try {
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db, username, password);
@ -29,6 +42,9 @@ public class KiafumiDB {
initializeTables();
}
/**
* Table initialization function, ran on every startup.
*/
private void initializeTables() {
try {
connection.prepareStatement(CREATE_SERVERINFO_TABLE).execute();
@ -38,6 +54,11 @@ public class KiafumiDB {
}
}
/**
* Creates the default server information when the bot joins a discord.
* @param guild - the guild that the bot joined
* @return whether the function succeeded.
*/
public boolean createServerInformation(Guild guild) {
try {
PreparedStatement prep = connection.prepareStatement(SERVERINFO_EXISTS);

View File

@ -1,5 +1,9 @@
package moe.oko.Kiafumi.model;
/**
* Server Class
* Used for in-memory data storage. Loaded from Database later.
*/
public class Server {
private String id;
private boolean welcomeEnabled;