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: 5 additions & 0 deletions src/main/java/org/jointheleague/discord_bot/DiscordBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.jointheleague.features.abstract_classes.Feature;
import org.jointheleague.features.examples.first_features.RandomPhrase;
import org.jointheleague.features.examples.second_features.HighLowGame;
import org.jointheleague.features.examples.third_features.CatFactsApi;
import org.jointheleague.features.examples.third_features.NewsApi;
Expand Down Expand Up @@ -48,6 +49,10 @@ public void connect(boolean printInvite) {
addFeature(new HighLowGame(channelName));
addFeature(new NewsApi(channelName));
addFeature(new CatFactsApi(channelName));
//MINE
addFeature(new RandomPhrase(channelName));
//addFeature(new SentenceMaker(channelName));
//addFeature(new AfricanAnimalFactApi(channelName));
}

private void addFeature(Feature feature){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class RandomNumber extends Feature {

public final String COMMAND = "!random";


public RandomNumber(String channelName) {
super(channelName);
helpEmbed = new HelpEmbed(COMMAND, "Allows you to get a random number between 0 and 1000)");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.jointheleague.features.examples.first_features;

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 java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomPhrase extends Feature {
public final String COMMAND = "!phrase";

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

//Create a help embed to describe feature when !help command is sent
helpEmbed = new HelpEmbed(COMMAND, "this gives you a random phrase");
}

@Override
public void handle(MessageCreateEvent event) {
String messageContent = event.getMessageContent();
if (messageContent.startsWith(COMMAND)) {
//respond to message here
Random r = new Random();
List<String> phrases = new ArrayList<>();
phrases.add("cat got ya tongue?");
phrases.add("im all ears");
phrases.add("you're on thin ice");
phrases.add("it'll cost you an arm and a leg");

int num = r.nextInt(phrases.size()-1);
event.getChannel().sendMessage(phrases.get(num));
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String findStory(String topic){

//Get a story from News API
ApiExampleWrapper apiExampleWrapper = getNewsStoryByTopic(topic);

System.out.println(apiExampleWrapper.toString());
//Get the first article
Article article = apiExampleWrapper.getArticles().get(0);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jointheleague.features.examples.third_features.plain_old_java_objects;

import com.google.gson.annotations.SerializedName;

public class AnimalWrapper {
@SerializedName("animal")
public String animal;
@SerializedName("fact")
public String fact;
@SerializedName("source")
public String source;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package org.jointheleague.features.examples.first_features;

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 RandomPhraseTest {
private final String testChannelName = "test";
private final RandomPhrase randomPhrase = new RandomPhrase(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 = randomPhrase.COMMAND;

//Then


assertNotNull(command);
}

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

//When
randomPhrase.handle(messageCreateEvent);

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

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

//When
randomPhrase.handle(messageCreateEvent);

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

@Test
void itShouldHaveAHelpEmbed() {
//Given

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

//Then
assertNotNull(actualHelpEmbed);
}

@Test
void itShouldHaveTheCommandAsTheTitleOfTheHelpEmbed() {
//Given

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

//Then
assertEquals(command, helpEmbedTitle);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.jointheleague.features.examples.second_features;
import org.javacord.api.entity.channel.TextChannel;
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.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.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.never;

class SentenceMakerTest {

private final String testChannelName = "test";
private final SentenceMaker sentenceMaker = new SentenceMaker(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 = sentenceMaker.COMMAND;

//Then
assertNotEquals("", command);
assertNotEquals("!command", command);
assertNotNull(command);
}
@Test
void itShouldHandleMessagesWithCommand() {
//Given
HelpEmbed helpEmbed = new HelpEmbed(sentenceMaker.COMMAND, "test");
when(messageCreateEvent.getMessageContent()).thenReturn(sentenceMaker.COMMAND);
when(messageCreateEvent.getChannel()).thenReturn((textChannel));

//When
sentenceMaker.handle(messageCreateEvent);

//Then
verify(textChannel, times(1)).sendMessage(anyString());
}
@Test
void itShouldNotHandleMessagesWithoutCommand() {
//Given
String command = "";
when(messageCreateEvent.getMessageContent()).thenReturn(command);

//When
sentenceMaker.handle(messageCreateEvent);

//Then
verify(textChannel, never()).sendMessage();
}
@Test
void itShouldHaveAHelpEmbed() {
//Given

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

//Then
assertNotNull(actualHelpEmbed);
}
@Test
void itShouldHaveTheCommandAsTheTitleOfTheHelpEmbed() {
//Given

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

//Then
assertEquals(command, helpEmbedTitle);
}

//FEATURE SPECIFIC TESTS

@Test
void itShouldSendErrorMessageIfResponseIsNotANumber() {
//Given
String guess = "ten";
String command = "!maker " + guess;
sentenceMaker.check = 3;
when(messageCreateEvent.getMessageContent()).thenReturn(command);
when(messageCreateEvent.getChannel()).thenReturn((textChannel));

//When
sentenceMaker.handle(messageCreateEvent);

//Then
verify(textChannel, times(1)).sendMessage("Please format your response like this: " + sentenceMaker.COMMAND + " 5");
}


}