feat: add QuizQuestion and TriviaManager classes for Java trivia game#40
feat: add QuizQuestion and TriviaManager classes for Java trivia game#40EJ-Edwards wants to merge 7 commits intoTogether-Java:developfrom
Conversation
|
@EJ-Edwards Hello and congratulations on opening your first pull request in this repository. Have you tested that this code works in a development environment? If so, it would be nice and convenient to have a preview of how it works in the form of screenshots or GIFs, embedded in the pull request description. 👍 |
|
also if I am making a mistake or it messes up sorry like I said this is my first time actually contributing. |
|
EJ-Edwards left a comment (Together-Java/TJ-Plays#40)
also if I am making a mistake or it messes up sorry like I said this is my first time actually contributing.
Sure. I have not looked at the code yet.
How can a member of the server play the Java quiz game from the Discord
bot?
Is there a command you have to trigger in order to activate the game?
Some sort of interaction with the bot from the Discord client?
These details need to be specified in the pull request description to make
it easier for the people reviewing your code changes.
~Chris
|
- Added JavaQuizCommand: Discord slash command that fetches a random Java trivia question via ChatGPT and presents it with answer buttons - Added TriviaManager: service that prompts ChatGPT for quiz questions and parses the JSON response - Added QuizQuestion: data model for trivia questions - Added javaquiz.java: standalone entry point for local testing - Added runQuiz Gradle task for convenience testing - Registered /javaquiz command in Bot.java How to play: A server member types /javaquiz in Discord. The bot displays a random Java trivia question with 4 answer buttons. The user clicks their answer and the bot reveals whether it was correct. Everything should work please let me know if I am missing anything Best -EJ
|
Ok Chris thank you and I just want to say thank you for taking your time and basically guiding me through the contribution process. |
|
Every time I select an answer, I get the following stacktrace: Has this feature been tested thoroughly? |
|
Hello Chris and I actually thought this would work thank you for letting me know also is there a bot token where I can test the bot in a discord server or no? |
tj-wazei
left a comment
There was a problem hiding this comment.
Overall a pretty small and simple PR, great work. Just some small comments.
Could you provide some screenshots of how this looks in action? Possible a screen recording if that isn't too much to ask?
Thanks
app/src/main/java/com/togetherjava/tjplays/games/game2048/javaquiz.java
Outdated
Show resolved
Hide resolved
app/src/main/java/com/togetherjava/tjplays/games/game2048/QuizQuestion.java
Outdated
Show resolved
Hide resolved
app/src/main/java/com/togetherjava/tjplays/games/game2048/TriviaManager.java
Outdated
Show resolved
Hide resolved
app/src/main/java/com/togetherjava/tjplays/games/game2048/TriviaManager.java
Outdated
Show resolved
Hide resolved
app/src/main/java/com/togetherjava/tjplays/listeners/commands/JavaQuizCommand.java
Outdated
Show resolved
Hide resolved
You'll need to create your own Discord server and Discord bot token via the website. We unfortunantely, cannot give you ours. https://discord.com/developers/applications |
…Manager for single question retrieval chore: Remove unused javaquiz main class and add gradle.properties for JDK configuration fix: Improve button interaction handling in JavaQuizCommand I addressed all the review feedback and pushed the changes. I wasn't able to provide screenshots because the bot hangs on startup — it appears the shared OpenAI API key from the pinned message may be expired or invalid. The bot connects to Discord fine (I confirmed it joins the server and receives slash commands), but the ChatGPT API call in ChatGptService times out during initialization. Could someone verify the key is still active?
|
Ok so that should be everything please let me know that this works. |
|
|
||
| import java.util.List; | ||
|
|
||
| public record QuizQuestion( |
There was a problem hiding this comment.
Since this is public-facing, it should have a JavaDoc explaining what it is, where it's used in and where it could be used by future contributors.
| public record QuizQuestion( | ||
| @JsonProperty("question") String question, | ||
| @JsonProperty("choices") List<String> choices, | ||
| @JsonProperty("correct") int correctIndex |
There was a problem hiding this comment.
NIT: It took me a little bit to understand what correctIndex represents. Something like correctAnswerIndex would help.
| @@ -0,0 +1,27 @@ | |||
| package com.togetherjava.tjplays.games.game2048; | |||
There was a problem hiding this comment.
Why is this class part of the games.game2048 package?
| @@ -0,0 +1,30 @@ | |||
| package com.togetherjava.tjplays.games.game2048; | |||
There was a problem hiding this comment.
Why is this class part of the games.game2048 package?
| import java.util.Optional; | ||
|
|
||
| public class TriviaManager { | ||
| private final ChatGptService gpt; |
There was a problem hiding this comment.
Please properly name this variable to chatGptService.
|
|
||
| public JavaQuizCommand(String openAiKey) { | ||
| super(Commands.slash(COMMAND_NAME, "Get a random Java trivia question")); | ||
| this.triviaManager = new TriviaManager(openAiKey); |
There was a problem hiding this comment.
Just like in this code review comment, I believe that we should let callers pass down an instance of TriviaManager.
| return; | ||
| } | ||
|
|
||
| QuizQuestion q = question.get(); |
There was a problem hiding this comment.
Let's use proper variable names instead of a single letter for readability. q could be quizQuestion and the current question can be quizQuestionOptional.
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("**Java Quiz**\n\n"); | ||
| sb.append(q.getQuestion()).append("\n\n"); | ||
| for (int i = 0; i < choices.size(); i++) { | ||
| sb.append("`").append(i + 1).append(")` ").append(choices.get(i)).append("\n"); | ||
| } |
There was a problem hiding this comment.
Let's not use sb as a variable name. This whole highlighted code block is also unreadable.
A lot of things could be changed here, and the removal of StringBuilder is a good place to start from.
Firstly, we should use String.format(...) as it's more readable and does not have .append() being repeated a lot.
Secondly, sending a MessageEmbed would allow for the code to be cleaner but also for the UI/UX to be significantly more structured.
app/src/main/java/com/togetherjava/tjplays/listeners/commands/JavaQuizCommand.java
Outdated
Show resolved
Hide resolved
app/src/main/java/com/togetherjava/tjplays/listeners/commands/JavaQuizCommand.java
Outdated
Show resolved
Hide resolved
…nd update JavaQuizCommand accordingly
…onality. Created a new folder for the package let me know if this works.


please review my code and let me know if I am missing any errors. Note be nice since this is my actual first commit I would like guidance on this.