diff --git a/KilluaBot.iml b/KilluaBot.iml index 3c97b68..cb88d6a 100644 --- a/KilluaBot.iml +++ b/KilluaBot.iml @@ -24,5 +24,6 @@ + \ No newline at end of file diff --git a/pom.xml b/pom.xml index d44fc0c..ced75c0 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,11 @@ JDA 4.2.0_168 + + com.google.code.gson + gson + 2.8.6 + diff --git a/src/main/java/com/github/nilstrieb/cofig/Secrets.java b/src/main/java/com/github/nilstrieb/cofig/Secrets.java index ba5fc92..e35a683 100644 --- a/src/main/java/com/github/nilstrieb/cofig/Secrets.java +++ b/src/main/java/com/github/nilstrieb/cofig/Secrets.java @@ -1,5 +1,5 @@ package com.github.nilstrieb.cofig; public class Secrets { - public static final String TOKEN = "invite xp bot here: https://discord.com/api/oauth2/authorize?client_id=706935674800177193&permissions=8&scope=bot"; + public static final String TOKEN = "ODAxMDE1MjU0MDIzNzk4ODI1.YAahlg.l8wbPw461HNWNr9D3ndoiIQeRlY"; } diff --git a/src/main/java/com/github/nilstrieb/commands/fun/TriviaCommand.java b/src/main/java/com/github/nilstrieb/commands/fun/TriviaCommand.java index 0c13e1f..d6ed698 100644 --- a/src/main/java/com/github/nilstrieb/commands/fun/TriviaCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/fun/TriviaCommand.java @@ -4,7 +4,7 @@ import com.github.nilstrieb.cofig.Config; import com.github.nilstrieb.commands.handler.Command; import com.github.nilstrieb.sections.ChannelMessageEventManager; import com.github.nilstrieb.sections.Section; -import com.github.nilstrieb.util.trivia.Arc; +import com.github.nilstrieb.util.ConsoleColors; import com.github.nilstrieb.util.trivia.TriviaQuestion; import com.github.nilstrieb.util.trivia.TriviaQuestionData; import net.dv8tion.jda.api.EmbedBuilder; @@ -29,29 +29,41 @@ public class TriviaCommand extends Command { @Override public void called(MessageReceivedEvent event, String args) { - int arc = 0; - try { - arc = Integer.parseInt(args); - } catch (NumberFormatException ignored) { - } - TriviaQuestion question = TriviaQuestionData.getQuestion(arc); - StringBuilder answers = new StringBuilder(); - for (int i = 0; i < question.getAnswers().length; i++) { - answers.append(i).append(". ").append(question.getAnswers()[i]).append("\n"); - } - EmbedBuilder builder = Config.getDefaultEmbed(event) - .addField(question.getQuestion(), answers.toString(), false); + if (args.equals("dump") && event.getAuthor().getIdLong() == Config.NILS_ID) { + TriviaQuestionData.dump(); + reply(event, "dumped"); + } else if (args.startsWith("add")) { + reply(event, "Enter the Question"); + new AddSection(event.getTextChannel().getIdLong(), event.getAuthor().getIdLong()); + } else { + int arc = 0; + try { + arc = Integer.parseInt(args); + } catch (NumberFormatException ignored) { + } - reply(event, builder.build()); - new TriviaSection(event.getTextChannel().getIdLong(), event.getAuthor().getIdLong(), question); + TriviaQuestion question = TriviaQuestionData.getQuestion(arc); + StringBuilder answers = new StringBuilder(); + for (int i = 0; i < question.getAnswers().length; i++) { + answers.append(i).append(". ").append(question.getAnswers()[i]).append("\n"); + } + EmbedBuilder builder = Config.getDefaultEmbed(event) + .addField(question.getQuestion(), answers.toString(), false); + + reply(event, builder.build()); + new TriviaSection(event.getTextChannel().getIdLong(), event.getAuthor().getIdLong(), question); + } } +/// +/// Trivia Section class +/// private static class TriviaSection extends Section { private final TriviaQuestion question; - public TriviaSection(long textChannelID, long userID, TriviaQuestion question) { + private TriviaSection(long textChannelID, long userID, TriviaQuestion question) { super(textChannelID, userID); this.question = question; } @@ -80,11 +92,47 @@ public class TriviaCommand extends Command { .setTitle(answer) .setThumbnail(null) .addField("Correct answer", correctAnswer, false); - if (question.getArc() == Arc.EXAM) { + if (question.getArc() == TriviaQuestion.EXAM) { builder.setFooter("Tip: Use " + Config.PREFIX + "help trivia for more questions."); } reply(event, builder.build()); - ChannelMessageEventManager.removeListener(this); + dispose(); + } + } + + +/// +/// Add question section class +/// + + private static class AddSection extends Section { + + private int status = 0; + private static final String[] messages = {"Enter all answers seperated by a ;", "Enter the correct answer index (starting at 0)", + "Enter the arc this question belongs to as a number (see " + Config.PREFIX + "help trivia for more info)"}; + private String[] answers = new String[4]; + + private AddSection(long textChannelID, long userID) { + super(textChannelID, userID); + } + + @Override + public void messageReceived(MessageReceivedEvent event) { + System.out.println(ConsoleColors.BLUE_BOLD + "[TriviaCommand.AddSection 121] Received Next Message: " + + event.getMessage().getContentRaw() + " status: " + status + ConsoleColors.RESET); + answers[status] = event.getMessage().getContentRaw(); + if (status >= 3) { + try { + TriviaQuestionData.addNew(new TriviaQuestion(answers)); + reply(event, "Question successfully added for approval"); + } catch (NumberFormatException e) { + reply(event, "Error: " + e.getMessage()); + } + dispose(); + } else { + reply(event, messages[status]); + } + status++; } } } diff --git a/src/main/java/com/github/nilstrieb/commands/handler/Command.java b/src/main/java/com/github/nilstrieb/commands/handler/Command.java index 8ec240d..188cea6 100644 --- a/src/main/java/com/github/nilstrieb/commands/handler/Command.java +++ b/src/main/java/com/github/nilstrieb/commands/handler/Command.java @@ -1,13 +1,8 @@ package com.github.nilstrieb.commands.handler; import com.github.nilstrieb.cofig.Config; -import com.github.nilstrieb.util.MultiPageEmbed; -import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; -import java.util.Timer; -import java.util.TimerTask; - public abstract class Command extends MessageSender{ private final String name; private final String description; diff --git a/src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java b/src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java index 235b859..1327447 100644 --- a/src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java +++ b/src/main/java/com/github/nilstrieb/sections/ChannelMessageEventManager.java @@ -7,9 +7,9 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import java.util.*; public class ChannelMessageEventManager { - private static HashMap> listeners = new HashMap<>(); - private static List removeBuffer = new ArrayList<>(); - private static Set removedChannels = new HashSet<>(); + private static final HashMap> listeners = new HashMap<>(); + private static final List removeBuffer = new ArrayList<>(); + private static final Set removedChannels = new HashSet<>(); public static void addListener(ChannelListener listener, long channel) { if (!listeners.containsKey(channel)) { diff --git a/src/main/java/com/github/nilstrieb/sections/Section.java b/src/main/java/com/github/nilstrieb/sections/Section.java index 425c558..bda7a2d 100644 --- a/src/main/java/com/github/nilstrieb/sections/Section.java +++ b/src/main/java/com/github/nilstrieb/sections/Section.java @@ -18,6 +18,10 @@ public abstract class Section extends MessageSender implements ChannelListener{ this.userID = 0; } + protected void dispose(){ + ChannelMessageEventManager.removeListener(this); + } + @Override public long getUserID() { return userID; diff --git a/src/main/java/com/github/nilstrieb/util/trivia/Arc.java b/src/main/java/com/github/nilstrieb/util/trivia/Arc.java deleted file mode 100644 index a89ff8d..0000000 --- a/src/main/java/com/github/nilstrieb/util/trivia/Arc.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.github.nilstrieb.util.trivia; - -public enum Arc { - EXAM(0), ZOLDYCK_FAMILY(1), HEAVENS_ARENA(2), YORKNEW_CITY(3), - GREED_ISLAND(4), CHIMERA_ANT(5), ELECTION(6); - - private final int number; - Arc(int number) { - this.number = number; - } - - public int getNumber() { - return number; - } -} diff --git a/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestion.java b/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestion.java index 7687f79..9f46f7f 100644 --- a/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestion.java +++ b/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestion.java @@ -1,18 +1,36 @@ package com.github.nilstrieb.util.trivia; -public class TriviaQuestion { - private String question; - private String[] answers; - private int correctAnswer; - private Arc arc; +import java.util.Arrays; - public TriviaQuestion(String question, int correctAnswer, Arc arc, String ... answers) { +public class TriviaQuestion { + + public static final int EXAM = 0; + public static final int ZOLDYCK_FAMILY = 1; + public static final int HEAVENS_ARENA = 2; + public static final int YORKNEW_CITY = 3; + public static final int GREED_ISLAND = 4; + public static final int CHIMERA_ANT = 5; + public static final int ELECTION = 6; + + private final String question; + private final String[] answers; + private final int correctAnswer; + private final int arc; + + public TriviaQuestion(String question, int correctAnswer, int arc, String ... answers) { this.question = question; this.answers = answers; this.arc = arc; this.correctAnswer = correctAnswer; } + public TriviaQuestion(String[] answers) { + this.question = answers[0]; + this.answers = answers[1].split(" *; *"); + this.correctAnswer = Integer.parseInt(answers[2]); + this.arc = Integer.parseInt(answers[3]); + } + public String getQuestion() { return question; } @@ -25,7 +43,17 @@ public class TriviaQuestion { return correctAnswer; } - public Arc getArc() { + public int getArc() { return arc; } + + @Override + public String toString() { + return "TriviaQuestion{" + + "question='" + question + '\'' + + ", answers=" + Arrays.toString(answers) + + ", correctAnswer=" + correctAnswer + + ", arc=" + arc + + '}'; + } } diff --git a/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestionData.java b/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestionData.java index 62e51fa..4c4e980 100644 --- a/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestionData.java +++ b/src/main/java/com/github/nilstrieb/util/trivia/TriviaQuestionData.java @@ -1,21 +1,16 @@ package com.github.nilstrieb.util.trivia; +import com.github.nilstrieb.util.ConsoleColors; +import com.google.gson.Gson; + +import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Random; -/* ------------------------------------------------------------------------------- -| | -| HXH SPOILER WARING | -| | -| CONTINUE READING AT YOUR ON RISK | -| | ------------------------------------------------------------------------------- -*/ - public class TriviaQuestionData { static List> questions = new ArrayList<>(); + private static final List newQuestions = new ArrayList<>(); private static final Random random = new Random(); static { @@ -26,38 +21,72 @@ public class TriviaQuestionData { questions.add(new ArrayList<>()); questions.add(new ArrayList<>()); questions.add(new ArrayList<>()); - - - add(new TriviaQuestion("What's the name of Gons aunt?", 1, Arc.EXAM, - "Kite", "Mito", "Ging", "Kurapika")); - add(new TriviaQuestion("What is Bisky's true form?", 1, Arc.GREED_ISLAND, - "Boy", "Big Woman", "Little Girl")); - add(new TriviaQuestion("Why does Leorio want money?", 0, Arc.EXAM, - "To become a doctor", "To send it to e-girls", "So that others don't get it", "To buy large quantities of drugs")); - add(new TriviaQuestion("Where did Meruem die?", 2, Arc.CHIMERA_ANT, - "Volcano", "In the desert with Netero", "In the basement of the palace", "In the womb of the queen")); - add(new TriviaQuestion("Who is Alluka?", 3, Arc.ELECTION, - "Gon's sister", "A Zodiac", "The 'dark' side of Nanika", "Killua's sister")); - add(new TriviaQuestion("How did Gon die on Greed Island?", 0, Arc.GREED_ISLAND, - "He didn't die on Greed Island", "Hisoka killed him with a card to the neck", "He lost too muich blood when Genthru blew his arm of", "He killed himself after Killuas death")); - add(new TriviaQuestion("How many doors are there at the Testing Gate of the Zoldyck Family?", 0, Arc.ZOLDYCK_FAMILY, - "7", "5", "12", "8")); - add(new TriviaQuestion("What was Gon's aim after Killua left him alone playing with the Chairman Netero?", 1, Arc.EXAM, - "Get the ball", "Make Netero use his right hand", "Make Netero use his left leg", "Let Netero drop the ball")); - add(new TriviaQuestion("What did Tonpa mix into the drink?", 2, Arc.EXAM, - "Sleeping pills", "Headache tabletsschla", "Laxative", "Rat poison")); - + loadJSON(); } - private static void add(TriviaQuestion triviaQuestion) { - questions.get(triviaQuestion.getArc().getNumber()).add(triviaQuestion); + private static void loadJSON() { + StringBuilder json = new StringBuilder(); + try (BufferedReader bufferedReader = new BufferedReader( + new FileReader("trivia_questions.json"))) { + String line = bufferedReader.readLine(); + while (line != null) { + json.append(line); + line = bufferedReader.readLine(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + Gson gson = new Gson(); + TriviaQuestion[] array = gson.fromJson(json.toString(), TriviaQuestion[].class); + + for (TriviaQuestion triviaQuestion : array) { + questions.get(triviaQuestion.getArc()).add(triviaQuestion); + } + } + + private static void saveJSON(String path, TriviaQuestion[] array) { + Gson gson = new Gson(); + + try (BufferedWriter bufferedWriter = new BufferedWriter( + new FileWriter(path))) { + + String json = gson.toJson(array); + bufferedWriter.write(json); + bufferedWriter.newLine(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void saveJSONFromAll(String path) { + TriviaQuestion[] questionsArray = new TriviaQuestion[getTotalQuestions()]; + + int i = 0; + for (List questionList : questions) { + for (TriviaQuestion triviaQuestion : questionList) { + questionsArray[i] = triviaQuestion; + i++; + } + } + + saveJSON(path, questionsArray); + } + + public static void add(TriviaQuestion triviaQuestion) { + questions.get(triviaQuestion.getArc()).add(triviaQuestion); + saveJSONFromAll("trivia_questions.json"); + } + + public static void addNew(TriviaQuestion triviaQuestion) { + newQuestions.add(triviaQuestion); + TriviaQuestion[] array = new TriviaQuestion[newQuestions.size()]; + saveJSON("new_trivia_questions.json", newQuestions.toArray(array)); } public static TriviaQuestion getQuestion(int toArc) { - int totalQuestions = 0; - for (int i = 0; i <= toArc; i++) { - totalQuestions += questions.get(i).size(); - } + int totalQuestions = getTotalQuestions(toArc); int randomQuestion = random.nextInt(totalQuestions); @@ -71,4 +100,28 @@ public class TriviaQuestionData { throw new IndexOutOfBoundsException("No Question available for arc " + toArc); } + + private static int getTotalQuestions(int toArc) { + int totalQuestions = 0; + for (int i = 0; i <= toArc; i++) { + totalQuestions += questions.get(i).size(); + } + return totalQuestions; + } + + private static int getTotalQuestions() { + return getTotalQuestions(questions.size() - 1); + } + + public static void dump() { + int i = 0; + for (List question : questions) { + System.out.println(ConsoleColors.BLUE_BACKGROUND + ConsoleColors.YELLOW + + "QUESTIONS ARC: " + i + ConsoleColors.RESET); + for (TriviaQuestion triviaQuestion : question) { + System.out.println(ConsoleColors.PURPLE_BOLD_BRIGHT + triviaQuestion + ConsoleColors.RESET); + } + i++; + } + } } diff --git a/trivia_questions.json b/trivia_questions.json new file mode 100644 index 0000000..d8d09ce --- /dev/null +++ b/trivia_questions.json @@ -0,0 +1,110 @@ +[ + { + "question": "What\u0027s the name of Gons aunt?", + "answers": [ + "Kite", + "Mito", + "Ging", + "Kurapika" + ], + "correctAnswer": 1, + "arc": 0 + }, + { + "question": "Why does Leorio want money?", + "answers": [ + "To become a doctor", + "To send it to e-girls", + "So that others don\u0027t get it", + "To buy large quantities of drugs" + ], + "correctAnswer": 0, + "arc": 0 + }, + { + "question": "What was Gon\u0027s aim after Killua left him alone playing with the Chairman Netero?", + "answers": [ + "Get the ball", + "Make Netero use his right hand", + "Make Netero use his left leg", + "Let Netero drop the ball" + ], + "correctAnswer": 1, + "arc": 0 + }, + { + "question": "What did Tonpa mix into the drink?", + "answers": [ + "Sleeping pills", + "Headache tabletsschla", + "Laxative", + "Rat poison" + ], + "correctAnswer": 2, + "arc": 0 + }, + { + "question": "How many doors are there at the Testing Gate of the Zoldyck Family?", + "answers": [ + "7", + "5", + "12", + "8" + ], + "correctAnswer": 0, + "arc": 1 + }, + { + "question": "What is Bisky\u0027s true form?", + "answers": [ + "Boy", + "Big Woman", + "Little Girl" + ], + "correctAnswer": 1, + "arc": 4 + }, + { + "question": "How did Gon die on Greed Island?", + "answers": [ + "He didn\u0027t die on Greed Island", + "Hisoka killed him with a card to the neck", + "He lost too muich blood when Genthru blew his arm of", + "He killed himself after Killuas death" + ], + "correctAnswer": 0, + "arc": 4 + }, + { + "question": "Where did Meruem die?", + "answers": [ + "Volcano", + "In the desert with Netero", + "In the basement of the palace", + "In the womb of the queen" + ], + "correctAnswer": 2, + "arc": 5 + }, + { + "question": "Who is Alluka?", + "answers": [ + "Gon\u0027s sister", + "A Zodiac", + "The \u0027dark\u0027 side of Nanika", + "Killua\u0027s sister" + ], + "correctAnswer": 3, + "arc": 6 + }, + { + "question": "What is Genthrus ability called?", + "answers": [ + "Nuke", + "Massive Explosion", + "Little Flower" + ], + "correctAnswer": 2, + "arc": 4 + } +]