Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/org/jointheleague/discord_bot/DiscordBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
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";

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();
// 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\"\nDEBUG: " + getQuestion());
boolean questionActive = true;
System.out.println("2");
}
}
}

public String getQuestion () {

//Make the request, accepting the response as a plain old java object you created
Mono<TriviaWrapper> triviaWrapperMono = webClient.get()
.retrieve()
.bodyToMono(TriviaWrapper.class);

//collect the response into a plain old java object
TriviaWrapper triviaWrapper = triviaWrapperMono.block();

//get the question from the response
//assert triviaWrapper != null;

//send the message
return triviaWrapper.getData().get(0);
}

public void setWebClient(WebClient webClient) {
this.webClient = webClient;
}
}

// 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 = "";
}


}
}
}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jointheleague.features.student.first_feature;

import java.util.List;

public class TriviaWrapper {
private List<String> data = null;

public List<String> getData() {
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down