From f44cf0ec49bec126b9fa6986f4c7d27705577f99 Mon Sep 17 00:00:00 2001 From: YeetMaster467 Date: Wed, 16 Apr 2025 19:55:41 -0700 Subject: [PATCH 1/3] Started on trivia game, looked at APIs and learned about them to try and set this up soon. --- .../jointheleague/discord_bot/DiscordBot.java | 5 +- .../student/first_feature/FeatureOne.java | 48 -------- .../student/first_feature/TriviaGame.java | 114 ++++++++++++++++++ .../student/first_feature/FeatureOneTest.java | 2 +- 4 files changed, 117 insertions(+), 52 deletions(-) delete mode 100644 src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java create mode 100644 src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java diff --git a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java index cefb5f79..058e4886 100644 --- a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java +++ b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java @@ -11,9 +11,8 @@ import org.jointheleague.features.examples.third_features.CatFactsApi; import org.jointheleague.features.examples.third_features.NewsApi; import org.jointheleague.features.examples.first_features.CurrentTime; -import org.jointheleague.features.examples.first_features.RandomNumber; import org.jointheleague.features.help_embed.HelpListener; -import org.jointheleague.features.student.first_feature.FeatureOne; +import org.jointheleague.features.student.first_feature.TriviaGame; public class DiscordBot { @@ -56,7 +55,7 @@ public void connect(boolean printInvite) throws InterruptedException { api.addEventListener(helpListener); //add features - addFeature(new FeatureOne(channelName)); + addFeature(new TriviaGame(channelName)); addFeature(new CurrentTime(channelName)); addFeature(new HighLowGame(channelName)); addFeature(new NewsApi(channelName)); diff --git a/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java b/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java deleted file mode 100644 index 8ab6ffc7..00000000 --- a/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.jointheleague.features.student.first_feature; - -import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; -import org.jointheleague.api_wrapper.ReceivedMessage; -import org.jointheleague.features.abstract_classes.Feature; -import org.jointheleague.features.templates.FeatureTemplate; - -import java.util.Random; - -public class FeatureOne extends FeatureTemplate { - public final String COMMAND = "!joke"; - - public FeatureOne(String channelName) { - super(channelName); - - //Create a help embed to describe feature when !help command is sent - helpEmbed = new HelpEmbed( - COMMAND, - "This command prints cheesy jokes." - ); - } -//IGNORE - @Override - public void handle(ReceivedMessage event) { - String messageContent = event.getMessageContent(); - if (messageContent.startsWith(COMMAND)) { - //respond to message here - Random ran = new Random(); - int ranInt = ran.nextInt(5); - - switch(ranInt){ - case 0:event.sendResponse("What’s the best thing about Switzerland? \n \n I don’t know, but the flag is a big plus."); - break; - case 1:event.sendResponse("I invented a new word! \n \n Plagiarism!"); - break; - case 2:event.sendResponse("Hear about the new restaurant called Karma? \n \n There’s no menu: You get what you deserve."); - break; - case 3:event.sendResponse("Did you hear about the claustrophobic astronaut? \n \n He just needed a little space."); - break; - case 4:event.sendResponse("Why don’t scientists trust atoms? \n \n Because they make up everything."); - break; - default:event.sendResponse("What’s the different between a cat and a comma? \n \n A cat has claws at the end of paws; A comma is a pause at the end of a clause. "); - break; - } - - } - } -} diff --git a/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java b/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java new file mode 100644 index 00000000..aa18f4af --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java @@ -0,0 +1,114 @@ +package org.jointheleague.features.student.first_feature; + +import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; +import org.jointheleague.api_wrapper.ReceivedMessage; +import org.jointheleague.features.templates.FeatureTemplate; +import org.springframework.web.reactive.function.client.WebClient; + +import java.util.Random; + +public class TriviaGame extends FeatureTemplate { + public final String COMMAND = "!trivia"; + + private WebClient webClient; + public static final String baseUrl = "https://opentdb.com/api.php?amount=10"; + + public TriviaGame(String channelName) { + super(channelName); + + //Create a help embed to describe feature when !help command is sent + helpEmbed = new HelpEmbed( + COMMAND, + "Play a trivia game, it'll be pretty chill." + ); + + this.webClient = WebClient + .builder() + .baseUrl(baseUrl) + .build(); + } +//IGNORE + @Override + public void handle(ReceivedMessage event) { + String messageContent = event.getMessageContent(); + if (messageContent.startsWith(COMMAND)) { + if (messageContent.equals(COMMAND)){ + event.sendResponse("I will tell you a trivia question, and you will attempt to answer it using the command. Ex: \"!trivia George Washington\""); + } + } + } +} + +// Chill Game here to use bits and pieces for logic. +/* +String messageContent = event.getMessageContent(); + if (messageContent.startsWith(COMMAND)) { + //respond to message here + if (messageContent.equals(COMMAND)){ + if (wordToGuess.isEmpty()){ + wordToGuess = Utilities.readRandomLineFromFile("src/main/java/org/jointheleague/features/student/first_feature/dictionary.txt"); + for (int i = 0; i < wordToGuess.length(); i++){ + currentDisplay += "-"; + } + event.sendResponse("There is a word you need to guess, guess a letter by doing the command, then a letter. Ex: \"!chillGame e\""); + System.out.println(wordToGuess); + } else { + event.sendResponse("You still haven't guessed the current word!"); + } + } else { + //check if the game has been started + if(wordToGuess.isEmpty()){ + //tell them to start the game first + event.sendResponse("Please start the game first using just the command"); + return; + } + + //parse the guess from the message + String guessMessage = messageContent.replaceAll(" ", "").replace(COMMAND, ""); + + //change the guess to a char + if (!guessMessage.equalsIgnoreCase(wordToGuess)){ + char guess = 0; + try{ + guess = guessMessage.charAt(0); + } + catch(IndexOutOfBoundsException e){ + //tell them to format their guess properly + event.sendResponse("Please format your guess like this: " + COMMAND + " e"); + return; + } + + if (wordToGuess.contains(guess + "") && !currentDisplay.contains(guess + "")){ + + char[] chars = currentDisplay.toCharArray(); + for (int i = 0; i < wordToGuess.length(); i++) { + if (wordToGuess.charAt(i) == guess){ + chars[i] = guess; + } + } + currentDisplay = new String(chars); + if (currentDisplay.contains("-")){ + event.sendResponse("Correct Guess!!\n" + currentDisplay); + } else { + event.sendResponse("Correct! The word I picked was " + wordToGuess); + wordToGuess = ""; + currentDisplay = ""; + } + + } else { + event.sendResponse("Incorrect Guess!!\n" + currentDisplay); + } + + } else { + //they got it correct + event.sendResponse("Correct! The word I picked was " + wordToGuess); + //set wordToGuess back to empty string + wordToGuess = ""; + currentDisplay = ""; + } + + + } + } + } + */ diff --git a/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java b/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java index 8c95c657..248894a1 100644 --- a/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java +++ b/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java @@ -21,7 +21,7 @@ public class FeatureOneTest { private final String testChannelName = "test"; - private final FeatureOne featureOne = new FeatureOne(testChannelName); + private final TriviaGame featureOne = new TriviaGame(testChannelName); private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; From 446119b7ff04a5d8ca82b5b2d59fa6a05729b475 Mon Sep 17 00:00:00 2001 From: YeetMaster467 Date: Wed, 30 Apr 2025 19:56:22 -0700 Subject: [PATCH 2/3] Tried to implement the Wrapper and API. The API website was being glitchy and I had some trouble, will reattempt. --- .../student/first_feature/TriviaGame.java | 25 +++++++++++++++++++ .../student/first_feature/TriviaWrapper.java | 4 +++ 2 files changed, 29 insertions(+) create mode 100644 src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java diff --git a/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java b/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java index aa18f4af..8bb98ddb 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java @@ -1,14 +1,17 @@ package org.jointheleague.features.student.first_feature; +import org.jointheleague.features.examples.third_features.plain_old_java_objects.cat_facts_api.CatWrapper; import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; import org.jointheleague.api_wrapper.ReceivedMessage; import org.jointheleague.features.templates.FeatureTemplate; import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; import java.util.Random; public class TriviaGame extends FeatureTemplate { public final String COMMAND = "!trivia"; + boolean questionActive = false; private WebClient webClient; public static final String baseUrl = "https://opentdb.com/api.php?amount=10"; @@ -34,9 +37,31 @@ public void handle(ReceivedMessage event) { if (messageContent.startsWith(COMMAND)) { if (messageContent.equals(COMMAND)){ event.sendResponse("I will tell you a trivia question, and you will attempt to answer it using the command. Ex: \"!trivia George Washington\""); + boolean questionActive = true; } } } + + public String getQuestion () { + + //Make the request, accepting the response as a plain old java object you created + Mono triviaWrapperMono = webClient.get() + .retrieve() + .bodyToMono(TriviaWrapper.class); + + //collect the response into a plain old java object + TriviaWrapper triviaWrapper = triviaWrapperMono.block(); + + //get the cat fact from the response + //String message = triviaWrapper.getData().get(0); + + //send the message + return null; + } + + public void setWebClient(WebClient webClient) { + this.webClient = webClient; + } } // Chill Game here to use bits and pieces for logic. diff --git a/src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java b/src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java new file mode 100644 index 00000000..a10388dd --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java @@ -0,0 +1,4 @@ +package org.jointheleague.features.student.first_feature; + +public class TriviaWrapper { +} From 0d6bc5548be3bd55d674dab40bf31e7b78f27dea Mon Sep 17 00:00:00 2001 From: YeetMaster467 Date: Wed, 7 May 2025 19:57:43 -0700 Subject: [PATCH 3/3] Debugged discord bot not working --- .../features/student/first_feature/TriviaGame.java | 12 ++++++++---- .../student/first_feature/TriviaWrapper.java | 7 +++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java b/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java index 8bb98ddb..96fcaecb 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/TriviaGame.java @@ -34,10 +34,14 @@ public TriviaGame(String channelName) { @Override public void handle(ReceivedMessage event) { String messageContent = event.getMessageContent(); + // newline character might interfere, printed 1 but not 2. + messageContent = messageContent.trim(); if (messageContent.startsWith(COMMAND)) { + System.out.println("1"); if (messageContent.equals(COMMAND)){ - event.sendResponse("I will tell you a trivia question, and you will attempt to answer it using the command. Ex: \"!trivia George Washington\""); + event.sendResponse("I will tell you a trivia question, and you will attempt to answer it using the command. Ex: \"!trivia George Washington\"\nDEBUG: " + getQuestion()); boolean questionActive = true; + System.out.println("2"); } } } @@ -52,11 +56,11 @@ public String getQuestion () { //collect the response into a plain old java object TriviaWrapper triviaWrapper = triviaWrapperMono.block(); - //get the cat fact from the response - //String message = triviaWrapper.getData().get(0); + //get the question from the response + //assert triviaWrapper != null; //send the message - return null; + return triviaWrapper.getData().get(0); } public void setWebClient(WebClient webClient) { diff --git a/src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java b/src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java index a10388dd..e8b7fdb5 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/TriviaWrapper.java @@ -1,4 +1,11 @@ package org.jointheleague.features.student.first_feature; +import java.util.List; + public class TriviaWrapper { + private List data = null; + + public List getData() { + return data; + } }