Compare commits
13 Commits
461fbab6f0
...
b7b6c2bda9
Author | SHA1 | Date |
---|---|---|
BodgeMaster | b7b6c2bda9 | |
BodgeMaster | 75b9530029 | |
BodgeMaster | 56eaf9a9b3 | |
BodgeMaster | 06160852fc | |
BodgeMaster | 9738530b4e | |
BodgeMaster | 8f2636a283 | |
BodgeMaster | 5628b3847c | |
BodgeMaster | 02649a0365 | |
BodgeMaster | 76ad2e0adc | |
Shwoomple | 01d023a2bf | |
Shwoomple | bd9eae58cd | |
BodgeMaster | 88014c726d | |
BodgeMaster | 81ffecec6a |
6
pom.xml
6
pom.xml
|
@ -12,6 +12,12 @@
|
||||||
<version>3.4.0</version>
|
<version>3.4.0</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mariadb.jdbc</groupId>
|
||||||
|
<artifactId>mariadb-java-client</artifactId>
|
||||||
|
<version>3.0.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
|
|
@ -8,33 +8,42 @@ import java.util.Date;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Main {
|
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) {
|
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;
|
||||||
|
|
||||||
// Insert your bot's token here
|
// CREATE TABLE questions(id INT(32) AUTO_INCREMENT PRIMARY KEY, question TEXT, max_score INT(8));
|
||||||
String token = args[0];
|
// 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");
|
CheckDeploy check = new CheckDeploy(7500, "/var/www/deployment/Hacky-Quizbot/id.txt");
|
||||||
check.start();
|
check.start();
|
||||||
|
|
||||||
DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();
|
DiscordApi api = new DiscordApiBuilder().setToken(commandLineArguments[0]).login().join();
|
||||||
|
|
||||||
//Trivia management
|
//Trivia management
|
||||||
Member member = new Member(api);
|
Member member = new Member(api);
|
||||||
|
|
||||||
// Add a listener which answers with "Pong!" if someone writes "!ping"
|
// small status command
|
||||||
api.addMessageCreateListener(event -> {
|
api.addMessageCreateListener(event -> {
|
||||||
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
|
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
|
||||||
event.getChannel().sendMessage("Pong!\nHacky the quiz bot has been running since "+startupTime);
|
event.getChannel().sendMessage("Pong!\nHacky the quiz bot has been running since "+startupTime);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Print the invite url of your bot
|
}
|
||||||
System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
|
|
||||||
|
public static String getCommandLineArgument(int index) {
|
||||||
|
return commandLineArguments[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Deploy check thread
|
||||||
public static class CheckDeploy extends Thread {
|
public static class CheckDeploy extends Thread {
|
||||||
private int interval;
|
private int interval;
|
||||||
private String id;
|
private String id;
|
||||||
|
|
|
@ -2,7 +2,9 @@ package linux.general.hackyquizbot;
|
||||||
|
|
||||||
import org.javacord.api.DiscordApi;
|
import org.javacord.api.DiscordApi;
|
||||||
import org.javacord.api.entity.user.User;
|
import org.javacord.api.entity.user.User;
|
||||||
|
import org.javacord.api.entity.message.embed.EmbedBuilder;
|
||||||
|
|
||||||
|
//TODO: find a better name for this class
|
||||||
public class Member {
|
public class Member {
|
||||||
private DiscordApi api;
|
private DiscordApi api;
|
||||||
private User user;
|
private User user;
|
||||||
|
@ -14,10 +16,23 @@ public class Member {
|
||||||
if(event.getMessageContent().equalsIgnoreCase("!trivia")) {
|
if(event.getMessageContent().equalsIgnoreCase("!trivia")) {
|
||||||
if(event.getMessageAuthor().asUser().isPresent()) {
|
if(event.getMessageAuthor().asUser().isPresent()) {
|
||||||
this.user = event.getMessageAuthor().asUser().get();
|
this.user = event.getMessageAuthor().asUser().get();
|
||||||
|
this.user.sendMessage("Hi! I'm Hacky! **Fuck you**! :D");
|
||||||
this.user.sendMessage("Welcome to hell");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendMessage(String question, String[] options) {
|
||||||
|
String desc = new String();
|
||||||
|
|
||||||
|
for(String option: options) {
|
||||||
|
desc += option + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
EmbedBuilder embed = new EmbedBuilder()
|
||||||
|
.setTitle(question)
|
||||||
|
.setDescription(desc);
|
||||||
|
|
||||||
|
this.user.sendMessage(embed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,140 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String[] getCategories() {
|
||||||
|
return categories;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Question getRandomQuestionForCategory(String category) {
|
||||||
|
//TODO: loop
|
||||||
|
//TODO: pick random question
|
||||||
|
//TODO: check if question is in category, return if it is
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Question getNewRandomQuestionForCategory(String discordUserID, String category) {
|
||||||
|
//TODO: get list of previously displayed questions for that user
|
||||||
|
//TODO: retrieve list of answers that user has given to get the question IDs from there
|
||||||
|
//TODO: find a random question that isn’t on said list
|
||||||
|
//TODO: add the question to said list and return it
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getScoreForUser(String discordUserID) {
|
||||||
|
int score = 0;
|
||||||
|
//TODO: iterate over all questions
|
||||||
|
//TODO: get achieved score for the user for the question
|
||||||
|
//TODO: add to score
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Question {
|
||||||
|
|
||||||
|
private String text;
|
||||||
|
private char[] validAnswers;
|
||||||
|
private char[] correctAnswers;
|
||||||
|
private int scoreValue;
|
||||||
|
private int ID;
|
||||||
|
private ArrayList<String> categories = new ArrayList<String>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ID unique ID for the question
|
||||||
|
* @param question The string for the question text
|
||||||
|
* @param answers The multiple choice answers
|
||||||
|
* @param mask A mask to be laid over answers to decide which answers are correct and which aren’t
|
||||||
|
* @param scoreValue See getQuestionScoreValue()
|
||||||
|
*/
|
||||||
|
public Question(int ID, String question, String[] answers, boolean[] mask, int scoreValue) {
|
||||||
|
//TODO: build text, valid answers and correct answers
|
||||||
|
this.ID = ID;
|
||||||
|
this.scoreValue = scoreValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: need a way to get back from the answer letter to the text answer and vice versa
|
||||||
|
|
||||||
|
public boolean isInCategory(String category) {
|
||||||
|
for (int i=0; i<this.categories.size(); i++) {
|
||||||
|
if (this.categories.get(i).equals(category)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addCategory(String category) {
|
||||||
|
this.categories.add(category);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the full question text including multiple choice options with letters that correspond to getValidResponses()
|
||||||
|
*/
|
||||||
|
public String getQuestionTextWithOptions() {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a character array containing all the valid answers
|
||||||
|
*/
|
||||||
|
public char[] getValidResponses() {
|
||||||
|
return this.validAnswers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a character array containing all the correct answers
|
||||||
|
*/
|
||||||
|
public char[] getCorrectResponses() {
|
||||||
|
return this.correctAnswers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is used to send the user’s reply back to the backend. Can be called
|
||||||
|
* multiple times to add answers for questions with multiple possible answers.
|
||||||
|
*
|
||||||
|
* @param discordUserID
|
||||||
|
* @param answer
|
||||||
|
*/
|
||||||
|
public void addAnswer(String discordUserID, char answer) {
|
||||||
|
//TODO: store question ID, user ID, and the chosen answer
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to balance easy and difficult questions. Difficult questions will have a
|
||||||
|
* higher value so if someone gets a bunch of them they will get less questions
|
||||||
|
* overall.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int getQuestionScoreValue() {
|
||||||
|
return this.scoreValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAchievedScore(String discordUserID) {
|
||||||
|
//TODO: retrieve given answers by user ID and question ID
|
||||||
|
//TODO: calculate score
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue