From b7b6c2bda9aa72f15614d575b251b9ef6a40d6cf Mon Sep 17 00:00:00 2001
From: BodgeMaster <>
Date: Mon, 21 Feb 2022 12:35:35 +0100
Subject: [PATCH] started MySQL implementation
---
pom.xml | 6 ++++++
.../java/linux/general/hackyquizbot/Main.java | 17 +++++++++++------
.../linux/general/hackyquizbot/QuizBackend.java | 14 ++++++++++++++
3 files changed, 31 insertions(+), 6 deletions(-)
diff --git a/pom.xml b/pom.xml
index b48fdd3..0377aaa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -12,6 +12,12 @@
3.4.0
pom
+
+
+ org.mariadb.jdbc
+ mariadb-java-client
+ 3.0.3
+
17
diff --git a/src/main/java/linux/general/hackyquizbot/Main.java b/src/main/java/linux/general/hackyquizbot/Main.java
index d2eb2fa..55c03ed 100644
--- a/src/main/java/linux/general/hackyquizbot/Main.java
+++ b/src/main/java/linux/general/hackyquizbot/Main.java
@@ -8,23 +8,24 @@ import java.util.Date;
import java.util.Scanner;
public class Main {
+ public static final String startupTime = String.format("%1$tY-%1$tm-%1$td %1$tI:%1$tM:%1$tS%1$tp UTC%1$tz", new Date());
+ private static String[] commandLineArguments;
+
public static void main(String[] args) {
- String startupTime = String.format("%1$tY-%1$tm-%1$td %1$tI:%1$tM:%1$tS%1$tp UTC%1$tz", new Date());
+ // [0] token, [1] db username, [2] db password
+ commandLineArguments = args;
- String token = args[0];
- String db_username = args[1];
- String db_password = args[2];
- String db_name = "quizbot";
// CREATE TABLE questions(id INT(32) AUTO_INCREMENT PRIMARY KEY, question TEXT, max_score INT(8));
// CREATE TABLE answer_coices(id INT(32), answer VARCHAR(2048), correctness BOOLEAN);
// CREATE TABLE user_answers(id INT(32), given_answer VARCHAR(2048), discord_user VARCHAR(1024));
// CREATE TABLE categories(id INT(32), category VARCHAR(1024));
+ // checks every 7500 ms if a deployment happened, if so shuts down the bot
CheckDeploy check = new CheckDeploy(7500, "/var/www/deployment/Hacky-Quizbot/id.txt");
check.start();
- DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();
+ DiscordApi api = new DiscordApiBuilder().setToken(commandLineArguments[0]).login().join();
//Trivia management
Member member = new Member(api);
@@ -37,6 +38,10 @@ public class Main {
});
}
+
+ public static String getCommandLineArgument(int index) {
+ return commandLineArguments[index];
+ }
//Deploy check thread
public static class CheckDeploy extends Thread {
diff --git a/src/main/java/linux/general/hackyquizbot/QuizBackend.java b/src/main/java/linux/general/hackyquizbot/QuizBackend.java
index aef9e7f..e2ed268 100644
--- a/src/main/java/linux/general/hackyquizbot/QuizBackend.java
+++ b/src/main/java/linux/general/hackyquizbot/QuizBackend.java
@@ -1,13 +1,27 @@
package linux.general.hackyquizbot;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
import java.util.ArrayList;
public class QuizBackend {
+ private static final String dbUsername = Main.getCommandLineArgument(1);
+ private static final String dbPassword = Main.getCommandLineArgument(2);
+ private static final String dbName = "jdbc:mysql://localhost:3306/quizbot"; //we can make this configurable later
+
private static Question[] allQuestions;
private static String[] categories;
static {
+ try (Connection connection = DriverManager.getConnection(dbName, dbUsername, dbPassword)) {
+ System.out.println("Database connected!");
+ }
+ catch (SQLException e) {
+ System.err.println("Could not connect to DB.");
+ e.printStackTrace();
+ }
//TODO: populate allQuestions
//TODO: populate categories
}