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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
}

dependencies {
implementation 'org.javacord:javacord:3.1.1'
implementation 'org.javacord:javacord:3.8.0'
implementation 'net.aksingh:owm-japis:2.5.3.0'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
implementation 'javax.json:javax.json-api:1.1'
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/jointheleague/discord_bot/DiscordBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.intent.Intent;
import org.jointheleague.features.abstract_classes.Feature;
import org.jointheleague.features.examples.second_features.HighLowGame;
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.second_feature.FeatureTwo;

public class DiscordBot {

Expand All @@ -28,7 +30,7 @@ public DiscordBot(String token, String channelName) {

public void connect(boolean printInvite) {

api = new DiscordApiBuilder().setToken(token).login().join();
api = new DiscordApiBuilder().setToken(token).addIntents(Intent.MESSAGE_CONTENT).login().join();

//Print the URL to invite the bot
if (printInvite) {
Expand All @@ -45,7 +47,7 @@ public void connect(boolean printInvite) {
//add features
addFeature(new RandomNumber(channelName));
addFeature(new CurrentTime(channelName));
addFeature(new HighLowGame(channelName));
addFeature(new FeatureTwo(channelName));
addFeature(new NewsApi(channelName));
addFeature(new CatFactsApi(channelName));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.jointheleague.features.student.second_feature;

import org.javacord.api.event.message.MessageCreateEvent;
import org.jointheleague.features.abstract_classes.Feature;
import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed;
import org.jointheleague.features.templates.FeatureTemplate;

public class FeatureTwo extends FeatureTemplate {
public final String COMMAND = "!popquiz";
int questionNumber = 0;
int points = 0;

public FeatureTwo(String channelName) {
super(channelName);

//Create a help embed to describe feature when !help command is sent
helpEmbed = new HelpEmbed(
COMMAND,
"This command will give you a short quiz about science and science related facts. Remember to answer the question type "+ COMMAND+" and then a space following that and finally your answer from the list."
);
}

@Override
public void handle(MessageCreateEvent event) {
String messageContent = event.getMessageContent();

if (messageContent.equals(COMMAND)) {
//respond to message here
points = 0;
questionNumber = 1;
event.getChannel().sendMessage("First question: What is it called when a liquid's temperature is lowered until it is below its freezing point yet it is still a liquid? \nA: Ice Nucleation \nB: Supercooling \nC: Antifreezing \nD: Impossible"
);
}
else if(messageContent.contains(COMMAND) && questionNumber == 0){
event.getChannel().sendMessage("Please be sure to start the game before adding any answer prompt. Start the game by typing !\u200Epopquiz.");
}
else if (messageContent.contains(COMMAND) && questionNumber == 1){
if(messageContent.equalsIgnoreCase(COMMAND+" Supercooling") || messageContent.equalsIgnoreCase(COMMAND+" B") || messageContent.equalsIgnoreCase(COMMAND + " 2")){
points++;
event.getChannel().sendMessage("Correct! Adding one point to your score. Alright onto the next question!");
questionNumber++;
event.getChannel().sendMessage("Question two: What is the most radioactive substance ever discovered? \n A: Uranium \n B: Plutonium \n C: Radium \n D: Polonium \n E: Nobelium \n F: Lawrencium \n G: Oganesson \n H: Krypton \n I: Promethium \n J: Iridium" );
}
else{
event.getChannel().sendMessage("Ooh, so close. Sadly we cannot give points for incorrect answers. Alright onto the next question!");
questionNumber++;
event.getChannel().sendMessage("Question two: What is the most radioactive substance ever discovered? \n A: Uranium \n B: Plutonium \n C: Radium \n D: Polonium \n E: Nobelium \n F: Lawrencium \n G: Oganesson \n H: Krypton \n I: Promethium \n J: Iridium" );
}
}
else if (messageContent.contains(COMMAND) && questionNumber ==2){
if(messageContent.equalsIgnoreCase(COMMAND + " Oganesson") || messageContent.equalsIgnoreCase(COMMAND + " G") || messageContent.equalsIgnoreCase(COMMAND + " 7")){
points++;
event.getChannel().sendMessage("Correct! Adding one point to your score. Alright onto the next question!");
questionNumber++;
event.getChannel().sendMessage("Question three: Can any element in any state be magnetic? \nA: Yes, all elements can be magnetic and it can occur naturally in each. \nB: Yes, (almost) all elements can be magnetic with electromagnetism. \nC: No, not all elements can be magnetic under any circumstances.");
}
else{
event.getChannel().sendMessage("Ooh, so close. Sadly we cannot give points for incorrect answers. Alright onto the next question!");
questionNumber++;
event.getChannel().sendMessage("Question three: Can any element in any state be magnetic? \nA: Yes, all elements can be magnetic and it can occur naturally in each. \nB: Yes, (almost) all elements can be magnetic with electromagnetism. \nC: No, not all elements can be magnetic under any circumstances.");
}
}
else if (messageContent.contains(COMMAND) && questionNumber ==3){
if(messageContent.equalsIgnoreCase(COMMAND + " Yes, (almost) all elements can be magnetic with electromagnetism.") || messageContent.equalsIgnoreCase(COMMAND + " B") || messageContent.equalsIgnoreCase(COMMAND + " 2")){
points++;
event.getChannel().sendMessage("Correct! Adding one point to your score. Alright onto the next question!");
questionNumber++;
event.getChannel().sendMessage("Question four: What is chalk made out of? \nA: Nothing,chalk is chalk \nB: Limestone, which is made up of Calcite \nC: Calcium Carbonate \nD: Calcium Citrate");
}
else{
event.getChannel().sendMessage("Ooh, so close. Sadly we cannot give points for incorrect answers. Alright onto the next question!");
questionNumber++;
event.getChannel().sendMessage("Question four: What is chalk made out of? \nA: Nothing,chalk is chalk \nB: Limestone, which is made up of Calcite \nC: Calcium Carbonate \nD: Calcium Citrate");
}
}
else if (messageContent.contains(COMMAND) && questionNumber ==4) {
if (messageContent.equalsIgnoreCase(COMMAND + " Limestone, which is made up of Calcite") || messageContent.equalsIgnoreCase(COMMAND + " B") || messageContent.equalsIgnoreCase(COMMAND + " 2")) {
points++;
event.getChannel().sendMessage("Correct! Adding one point to your score. Alright onto the next question!");
questionNumber++;
event.getChannel().sendMessage("What is the only tree to grow in saltwater? \nA: Mangrove \nB: Banyan \nC: Bald cypress \nD: Water tupelo");
}
else {
event.getChannel().sendMessage("Ooh, so close. Sadly we cannot give points for incorrect answers. Alright onto the next question!");
questionNumber++;
event.getChannel().sendMessage("What is the only tree to grow in saltwater? \nA: Mangrove \nB: Banyan \nC: Bald cypress \nD: Water tupelo");
}
}
else if (messageContent.contains(COMMAND) && questionNumber ==5) {
if (messageContent.equalsIgnoreCase(COMMAND + " Mangrove") || messageContent.equalsIgnoreCase(COMMAND + " A") || messageContent.equalsIgnoreCase(COMMAND + " 1")) {
points++;
event.getChannel().sendMessage("Correct! Adding one point to your score. Time to tally your final score!");
event.getChannel().sendMessage("Your final score is " + points+" points!");
questionNumber=0;
}
else{
event.getChannel().sendMessage("Ooh, so close. Sadly we cannot give points for incorrect answers. Time to tally your final score!");
event.getChannel().sendMessage("Your final score is " + points+" points!");
questionNumber=0;
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void itShouldNotHandleMessagesWithoutCommand() {
currentTime.handle(messageCreateEvent);

//Then
verify(textChannel, never()).sendMessage();
verify(textChannel, never()).sendMessage("");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void itShouldNotHandleMessagesWithoutCommand() {
randomNumber.handle(messageCreateEvent);

//Then
verify(textChannel, never()).sendMessage();
verify(textChannel, never()).sendMessage("");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void itShouldNotHandleMessagesWithoutCommand() {
highLowGame.handle(messageCreateEvent);

//Then
verify(textChannel, never()).sendMessage();
verify(textChannel, never()).sendMessage("");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void givenMessageWithoutCommand_whenHandle_thenDoNothing() {
catFactsApi.handle(messageCreateEvent);

//Then
verify(textChannel, never()).sendMessage();
verify(textChannel, never()).sendMessage("");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void givenMessageWithoutCommand_whenHandle_thenDoNothing() {
newsApi.handle(messageCreateEvent);

//Then
verify(textChannel, never()).sendMessage();
verify(textChannel, never()).sendMessage("");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package org.jointheleague.features.student.second_feature;

import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.event.message.MessageCreateEvent;
import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed;
import org.jointheleague.features.templates.FeatureTemplate;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.never;

public class FeatureTwoTest {
private final String testChannelName = "test";
private final FeatureTwo featureTwo = new FeatureTwo(testChannelName);

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;

@Mock
private MessageCreateEvent messageCreateEvent;

@Mock
private TextChannel textChannel;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
System.setOut(new PrintStream(outContent));
}

@AfterEach
public void itShouldNotPrintToSystemOut() {
String expected = "";
String actual = outContent.toString();

assertEquals(expected, actual);
System.setOut(originalOut);
}

@Test
void itShouldHaveACommand() {
//Given

//When
String command = featureTwo.COMMAND;

//Then

if(!(featureTwo instanceof FeatureTemplate)){
assertNotEquals("!command", command);
}

assertNotEquals("", command);
assertNotEquals("!", command);
assertEquals('!', command.charAt(0));
assertNotNull(command);
}

@Test
void itShouldHandleMessagesWithCommand() {
//Given
HelpEmbed helpEmbed = new HelpEmbed(featureTwo.COMMAND, "test");
when(messageCreateEvent.getMessageContent()).thenReturn(featureTwo.COMMAND);
when(messageCreateEvent.getChannel()).thenReturn((textChannel));

//When
featureTwo.handle(messageCreateEvent);

//Then
verify(textChannel, times(1)).sendMessage(anyString());
}

@Test
void itShouldNotHandleMessagesWithoutCommand() {
//Given
String command = "";
when(messageCreateEvent.getMessageContent()).thenReturn(command);

//When
featureTwo.handle(messageCreateEvent);

//Then
verify(textChannel, never()).sendMessage("");
}

@Test
void itShouldHaveAHelpEmbed() {
//Given

//When
HelpEmbed actualHelpEmbed = featureTwo.getHelpEmbed();

//Then
assertNotNull(actualHelpEmbed);
}

@Test
void itShouldHaveTheCommandAsTheTitleOfTheHelpEmbed() {
//Given

//When
String helpEmbedTitle = featureTwo.getHelpEmbed().getTitle();
String command = featureTwo.COMMAND;

//Then
assertEquals(command, helpEmbedTitle);
}
@Test
void itShouldNotAcceptGuessIfGameIsNotStarted(){
// Do the same thing as HighLowGameTest.java
String command = "!popquiz do something";
when(messageCreateEvent.getMessageContent()).thenReturn(command);
when(messageCreateEvent.getChannel()).thenReturn(textChannel);

featureTwo.handle(messageCreateEvent);

verify(textChannel, times(1)).sendMessage("Please be sure to start the game before adding any answer prompt. Start the game by typing !\u200Epopquiz.");
}
@Test
void itShouldTellUserTheirAnswerIsCorrect(){
featureTwo.questionNumber = 5;
featureTwo.points = 4;
String command = "!popquiz A";
when(messageCreateEvent.getMessageContent()).thenReturn(command);
when(messageCreateEvent.getChannel()).thenReturn(textChannel);

featureTwo.handle(messageCreateEvent);

verify(textChannel, times(1)).sendMessage("Correct! Adding one point to your score. Time to tally your final score!");
assertEquals(5, featureTwo.points);
}
@Test
void itShouldTellUserTheirAnswerIsIncorrect(){
//Have it check point addition, message printing, and other things related to the answer being correct/incorrect.
featureTwo.questionNumber = 5;
featureTwo.points = 4;
String command = "!popquiz C";
when(messageCreateEvent.getMessageContent()).thenReturn(command);
when(messageCreateEvent.getChannel()).thenReturn(textChannel);

featureTwo.handle(messageCreateEvent);

verify(textChannel, times(1)).sendMessage("Ooh, so close. Sadly we cannot give points for incorrect answers. Time to tally your final score!");
assertEquals(4, featureTwo.points);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void itShouldNotHandleMessagesWithoutCommand() {
featureTemplate.handle(messageCreateEvent);

//Then
verify(textChannel, never()).sendMessage();
verify(textChannel, never()).sendMessage("");
}

@Test
Expand Down