diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index bf72030..de02137 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -145,6 +145,7 @@ public class HidekoBot messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.LoveCalculatorCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.MagicBallCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.SayCommand()); + messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.TriviaCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.UrbanDictionaryCommand()); Cache.setMessageCommandListener(messageCommandListener); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java new file mode 100644 index 0000000..11b8211 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java @@ -0,0 +1,87 @@ +package wtf.beatrice.hidekobot.commands.message; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.json.JSONObject; +import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.objects.TriviaQuestion; +import wtf.beatrice.hidekobot.objects.commands.CommandCategory; +import wtf.beatrice.hidekobot.objects.commands.MessageCommand; +import wtf.beatrice.hidekobot.util.TriviaUtil; + +import java.nio.channels.ScatteringByteChannel; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class TriviaCommand implements MessageCommand +{ + + @Override + public LinkedList getCommandLabels() { + return new LinkedList<>(Collections.singletonList("trivia")); + } + + @Nullable + @Override + public List getPermissions() { + return null; + } + + @Override + public boolean passRawArgs() { + return false; + } + + @NotNull + @Override + public CommandCategory getCategory() { + return CommandCategory.FUN; + } + + @NotNull + @Override + public String getDescription() { + return "Start a Trivia session and play with others!"; + } + + @Nullable + @Override + public String getUsage() { + return null; + } + + @Override + public void runCommand(MessageReceivedEvent event, String label, String[] args) + { + + if(TriviaUtil.channelsRunningTrivia.contains(event.getChannel().getId())) + { + // todo nicer looking + Message err = event.getMessage().reply("Trivia is already running here!").complete(); + Cache.getTaskScheduler().schedule(() -> err.delete().queue(), 15, TimeUnit.SECONDS); + return; + } + + EmbedBuilder embedBuilder = new EmbedBuilder(); + + embedBuilder.setColor(Cache.getBotColor()); + embedBuilder.setTitle("Trivia"); + + JSONObject triviaJson = TriviaUtil.fetchTrivia(); + List questions = TriviaUtil.getQuestions(triviaJson); //todo null check, rate limiting... + TriviaQuestion first = questions.get(0); + + embedBuilder.addField("Question", first.question(), false); + + event.getChannel().sendMessageEmbeds(embedBuilder.build()).queue(); + + + + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/objects/TriviaQuestion.java b/src/main/java/wtf/beatrice/hidekobot/objects/TriviaQuestion.java new file mode 100644 index 0000000..3fba222 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/objects/TriviaQuestion.java @@ -0,0 +1,8 @@ +package wtf.beatrice.hidekobot.objects; + +import java.util.List; + +public record TriviaQuestion(String question, String correctAnswer, + List wrongAnswers) { + +} diff --git a/src/main/java/wtf/beatrice/hidekobot/util/TriviaUtil.java b/src/main/java/wtf/beatrice/hidekobot/util/TriviaUtil.java new file mode 100644 index 0000000..0297f9c --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/util/TriviaUtil.java @@ -0,0 +1,74 @@ +package wtf.beatrice.hidekobot.util; + +import org.apache.commons.text.StringEscapeUtils; +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONString; +import org.jsoup.Jsoup; +import wtf.beatrice.hidekobot.objects.TriviaQuestion; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; + +public class TriviaUtil +{ + private final static String link = "https://opentdb.com/api.php?amount=10"; + + public static List channelsRunningTrivia = new ArrayList<>(); + + public static JSONObject fetchTrivia() + { + try { + URL url = new URL(link); + URLConnection connection = url.openConnection(); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + + String currentChar; + StringBuilder jsonStrBuilder = new StringBuilder(); + while((currentChar = bufferedReader.readLine()) != null) + { + jsonStrBuilder.append(currentChar); + } + bufferedReader.close(); + return new JSONObject(jsonStrBuilder.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + + public static List getQuestions(JSONObject jsonObject) + { + List questions = new ArrayList<>(); + + JSONArray results = jsonObject.getJSONArray("results"); + + for(Object currentQuestionGeneric : results) + { + JSONObject questionJson = (JSONObject) currentQuestionGeneric; + String question = StringEscapeUtils.unescapeHtml4(questionJson.getString("question")); + String correctAnswer = StringEscapeUtils.unescapeHtml4(questionJson.getString("correct_answer")); + + List incorrectAnswersList = new ArrayList<>(); + + JSONArray incorrectAnswers = questionJson.getJSONArray("incorrect_answers"); + for(Object incorrectAnswerGeneric : incorrectAnswers) + { + String incorrectAnswer = (String) incorrectAnswerGeneric; + incorrectAnswersList.add(StringEscapeUtils.unescapeHtml4(incorrectAnswer)); + } + + TriviaQuestion triviaQuestion = new TriviaQuestion(question, correctAnswer, incorrectAnswersList); + questions.add(triviaQuestion); + } + + return questions; + } + +}