diff --git a/.gitignore b/.gitignore index 63db41c1..5c5a517a 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ src/main/java/org.jointheleague.Launcher.java run.sh appspec.yml .gradle/ +/bin/ +/build/ diff --git a/src/main/java/org/jointheleague/Launcher.java b/src/main/java/org/jointheleague/Launcher.java index 17b44585..f4d6b1d4 100644 --- a/src/main/java/org/jointheleague/Launcher.java +++ b/src/main/java/org/jointheleague/Launcher.java @@ -6,8 +6,8 @@ public class Launcher { public static void main(String[] args) throws InterruptedException { //Initialize variables - String channelName = System.getenv("CHANNEL_NAME"); - String discordToken = System.getenv("DISCORD_TOKEN"); + String channelName = "bob_bot"; + String discordToken = "include token here"; boolean printDiscordInvite = true; //Instantiate DiscordBot and connect diff --git a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java index cefb5f79..46850e3c 100644 --- a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java +++ b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java @@ -1,19 +1,22 @@ package org.jointheleague.discord_bot; -import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.JDABuilder; -import net.dv8tion.jda.api.requests.GatewayIntent; -import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; -import net.dv8tion.jda.api.utils.messages.MessageCreateData; +import java.util.Random; import org.jointheleague.features.abstract_classes.Feature; +import org.jointheleague.features.examples.first_features.CurrentTime; 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.first_feature.BananaSoftware; import org.jointheleague.features.student.first_feature.FeatureOne; +import org.jointheleague.features.student.first_feature.WeatherAPI; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.requests.GatewayIntent; +import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; +import net.dv8tion.jda.api.utils.messages.MessageCreateData; public class DiscordBot { @@ -46,7 +49,7 @@ public void connect(boolean printInvite) throws InterruptedException { //Send bot connected message in channel MessageCreateData botConnected = new MessageCreateBuilder() - .addContent(api.getSelfUser().getName() + " has connected") + .addContent(""+new Random().nextInt()) .build(); api.getTextChannelsByName(channelName, true).forEach(e -> { e.sendMessage(botConnected).submit().join(); @@ -56,7 +59,9 @@ public void connect(boolean printInvite) throws InterruptedException { api.addEventListener(helpListener); //add features - addFeature(new FeatureOne(channelName)); + addFeature(new WeatherAPI(channelName)); + addFeature(new FeatureOne(channelName, false)); + addFeature(new BananaSoftware(channelName)); addFeature(new CurrentTime(channelName)); addFeature(new HighLowGame(channelName)); addFeature(new NewsApi(channelName)); diff --git a/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java b/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java index 4e4d7b47..015650ce 100644 --- a/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java +++ b/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java @@ -52,6 +52,10 @@ else if (messageContent.contains(COMMAND) return; } + if(guess > 100 || guess < 1) { + event.sendResponse("dang that guess was useless"); + } + if (guess < numberToGuess) { //tell them it's too low event.sendResponse(guess + " is too low. Guess again!"); diff --git a/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java b/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java new file mode 100644 index 00000000..c151dc0e --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java @@ -0,0 +1,192 @@ +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; +import java.util.Scanner; + +public class BananaSoftware extends FeatureTemplate { + + public BananaSoftware(String channelName) { + super(channelName); + } + + public final String COMMAND = "banana"; + public final String password = "ON"; + + public boolean BananaMode = false; + + static public Random r = new Random(); + + @Override + public void handle(ReceivedMessage event) { + String messageContent = event.getMessageContent(); + + + if(messageContent.toLowerCase().startsWith(COMMAND) && messageContent.toUpperCase().contains(password)) { + if(BananaMode) { + event.sendResponse("Message received! BananaMode is already on."); + } + else { + event.sendResponse("Message received! BananaMode is now on."); + BananaMode = true; + startupSequence(event); + } + } + + else if (messageContent.toLowerCase().startsWith(COMMAND) && !BananaMode) { + event.sendResponse("Message received! BananaMode remains off."); + } + + else if(messageContent.toLowerCase().startsWith(COMMAND) && BananaMode) { + + String t = messageContent.substring(7); // good + String[] things = t.trim().split(" "); + + if(things.length <3 && !things[0].toUpperCase().equals("OFF")) event.sendResponse("So youre meant to *include* __both__ numbers in your argument"); + + switch(things[0].toLowerCase()) { + + case "add": + String a; + int b=0; + String c; + int d=0; + try { + a = things[1]; + b = Integer.parseInt(a); + c = things[2]; + d = Integer.parseInt(c); + event.sendResponse(b+d+""); + } + catch(Exception e){ + event.sendResponse("hey i dont think thats an integer"); + } + + event.sendResponse("\n \nWhich one would you like to execute? [Add/Subt/Mult/Divi/OFF] Always start with the Banana keyword"); + event.sendResponse("Form: [Add/Subt/Mult/Divi/OFF] [Number 1] [Number 2]"); + + break; + + case "subt": + String aa; + int bb=0; + String cc; + int dd=0; + try { + aa = things[1]; + bb = Integer.parseInt(aa); + cc = things[2]; + dd = Integer.parseInt(cc); + event.sendResponse(bb-dd+""); + } + catch(Exception e){ + event.sendResponse("hey i dont think thats an integer"); + } + + event.sendResponse("\n \nWhich one would you like to execute? [Add/Subt/Mult/Divi/OFF] Always start with the Banana keyword"); + event.sendResponse("Form: [Add/Subt/Mult/Divi/OFF] [Number 1] [Number 2]"); + + break; + + case "mult": + String aaa; + int bbb=0; + String ccc; + int ddd=0; + try { + aaa = things[1]; + bbb = Integer.parseInt(aaa); + ccc = things[2]; + ddd = Integer.parseInt(ccc); + event.sendResponse(bbb*ddd+""); + } + catch(Exception e){ + event.sendResponse("hey i dont think thats an integer"); + } + + event.sendResponse("\n \nWhich one would you like to execute? [Add/Subt/Mult/Divi/OFF] Always start with the Banana keyword"); + event.sendResponse("Form: [Add/Subt/Mult/Divi/OFF] [Number 1] [Number 2]"); + + break; + + case "divi": + + String aaaa; + int bbbb=0; + String cccc; + int dddd=0; + try { + aaaa = things[1]; + bbbb = Integer.parseInt(aaaa); + cccc = things[2]; + dddd = Integer.parseInt(cccc); + event.sendResponse(bbbb/dddd+""); + } + catch(ArithmeticException ae) { + event.sendResponse("bruh"); + } + catch(Exception e){ + event.sendResponse("hey i dont think thats an integer"); + } + + event.sendResponse("\n \nWhich one would you like to execute? [Add/Subt/Mult/Divi/OFF] Always start with the Banana keyword"); + event.sendResponse("Form: [Add/Subt/Mult/Divi/OFF] [Number 1] [Number 2]"); + + break; + case "off": + BananaMode = false; + event.sendResponse("Got it. BananaMode is now off."); + + break; + } + + } + } + + public void startupSequence(ReceivedMessage event) { + + event.sendResponse("Loading JavaBanana Architecture... "); + + try { + Thread.sleep(r.nextInt(3500)+500); + + event.sendResponse("Loading... [--- ] 20%"); + + Thread.sleep(r.nextInt(1500)+500); + + event.sendResponse("Loading... [------ ] 40%"); + + Thread.sleep(r.nextInt(300)+100); + + event.sendResponse("Loading... [-------- ] 60%"); + + Thread.sleep(300+r.nextInt(800)); + + event.sendResponse("Loading... [----------- ] 80%"); + + Thread.sleep(500+r.nextInt(3000)); + + event.sendResponse("Loading... [--------------] 100%"); + + Thread.sleep(500+r.nextInt(2000)); + + event.sendResponse("-------- \n\n"); + + Thread.sleep(200); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + + event.sendResponse("\n" + "JAVA BANANA SOFTWARE v1.0.0"); + + event.sendResponse("\nThe JavaBanana Calculator has many operations to help you with math homework. "); + event.sendResponse("\n \nWhich one would you like to execute? [Add/Subt/Mult/Divi/OFF] Always start with the Banana keyword"); + event.sendResponse("Form: [Add/Subt/Mult/Divi/OFF] [Number 1] [Number 2]"); + } + +} diff --git a/src/main/java/org/jointheleague/features/student/first_feature/Condition.java b/src/main/java/org/jointheleague/features/student/first_feature/Condition.java new file mode 100644 index 00000000..ffdacf85 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/Condition.java @@ -0,0 +1,100 @@ + +package org.jointheleague.features.student.first_feature; + +import java.util.LinkedHashMap; +import java.util.Map; +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "text", + "icon", + "code" +}) +@Generated("jsonschema2pojo") +public class Condition { + + @JsonProperty("text") + private String text; + @JsonProperty("icon") + private String icon; + @JsonProperty("code") + private Integer code; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); + + @JsonProperty("text") + public String getText() { + return text; + } + + @JsonProperty("text") + public void setText(String text) { + this.text = text; + } + + @JsonProperty("icon") + public String getIcon() { + return icon; + } + + @JsonProperty("icon") + public void setIcon(String icon) { + this.icon = icon; + } + + @JsonProperty("code") + public Integer getCode() { + return code; + } + + @JsonProperty("code") + public void setCode(Integer code) { + this.code = code; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Condition.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("text"); + sb.append('='); + sb.append(((this.text == null)?"":this.text)); + sb.append(','); + sb.append("icon"); + sb.append('='); + sb.append(((this.icon == null)?"":this.icon)); + sb.append(','); + sb.append("code"); + sb.append('='); + sb.append(((this.code == null)?"":this.code)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + +} diff --git a/src/main/java/org/jointheleague/features/student/first_feature/Current.java b/src/main/java/org/jointheleague/features/student/first_feature/Current.java new file mode 100644 index 00000000..c42f7c67 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/Current.java @@ -0,0 +1,542 @@ + +package org.jointheleague.features.student.first_feature; + +import java.util.LinkedHashMap; +import java.util.Map; +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "last_updated_epoch", + "last_updated", + "temp_c", + "temp_f", + "is_day", + "condition", + "wind_mph", + "wind_kph", + "wind_degree", + "wind_dir", + "pressure_mb", + "pressure_in", + "precip_mm", + "precip_in", + "humidity", + "cloud", + "feelslike_c", + "feelslike_f", + "windchill_c", + "windchill_f", + "heatindex_c", + "heatindex_f", + "dewpoint_c", + "dewpoint_f", + "vis_km", + "vis_miles", + "uv", + "gust_mph", + "gust_kph" +}) +@Generated("jsonschema2pojo") +public class Current { + + @JsonProperty("last_updated_epoch") + private Integer lastUpdatedEpoch; + @JsonProperty("last_updated") + private String lastUpdated; + @JsonProperty("temp_c") + private Double tempC; + @JsonProperty("temp_f") + private Double tempF; + @JsonProperty("is_day") + private Integer isDay; + @JsonProperty("condition") + private Condition condition; + @JsonProperty("wind_mph") + private Double windMph; + @JsonProperty("wind_kph") + private Double windKph; + @JsonProperty("wind_degree") + private Integer windDegree; + @JsonProperty("wind_dir") + private String windDir; + @JsonProperty("pressure_mb") + private Double pressureMb; + @JsonProperty("pressure_in") + private Double pressureIn; + @JsonProperty("precip_mm") + private Double precipMm; + @JsonProperty("precip_in") + private Double precipIn; + @JsonProperty("humidity") + private Integer humidity; + @JsonProperty("cloud") + private Integer cloud; + @JsonProperty("feelslike_c") + private Double feelslikeC; + @JsonProperty("feelslike_f") + private Double feelslikeF; + @JsonProperty("windchill_c") + private Double windchillC; + @JsonProperty("windchill_f") + private Double windchillF; + @JsonProperty("heatindex_c") + private Double heatindexC; + @JsonProperty("heatindex_f") + private Double heatindexF; + @JsonProperty("dewpoint_c") + private Double dewpointC; + @JsonProperty("dewpoint_f") + private Double dewpointF; + @JsonProperty("vis_km") + private Double visKm; + @JsonProperty("vis_miles") + private Double visMiles; + @JsonProperty("uv") + private Double uv; + @JsonProperty("gust_mph") + private Double gustMph; + @JsonProperty("gust_kph") + private Double gustKph; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); + + @JsonProperty("last_updated_epoch") + public Integer getLastUpdatedEpoch() { + return lastUpdatedEpoch; + } + + @JsonProperty("last_updated_epoch") + public void setLastUpdatedEpoch(Integer lastUpdatedEpoch) { + this.lastUpdatedEpoch = lastUpdatedEpoch; + } + + @JsonProperty("last_updated") + public String getLastUpdated() { + return lastUpdated; + } + + @JsonProperty("last_updated") + public void setLastUpdated(String lastUpdated) { + this.lastUpdated = lastUpdated; + } + + @JsonProperty("temp_c") + public Double getTempC() { + return tempC; + } + + @JsonProperty("temp_c") + public void setTempC(Double tempC) { + this.tempC = tempC; + } + + @JsonProperty("temp_f") + public Double getTempF() { + return tempF; + } + + @JsonProperty("temp_f") + public void setTempF(Double tempF) { + this.tempF = tempF; + } + + @JsonProperty("is_day") + public Integer getIsDay() { + return isDay; + } + + @JsonProperty("is_day") + public void setIsDay(Integer isDay) { + this.isDay = isDay; + } + + @JsonProperty("condition") + public Condition getCondition() { + return condition; + } + + @JsonProperty("condition") + public void setCondition(Condition condition) { + this.condition = condition; + } + + @JsonProperty("wind_mph") + public Double getWindMph() { + return windMph; + } + + @JsonProperty("wind_mph") + public void setWindMph(Double windMph) { + this.windMph = windMph; + } + + @JsonProperty("wind_kph") + public Double getWindKph() { + return windKph; + } + + @JsonProperty("wind_kph") + public void setWindKph(Double windKph) { + this.windKph = windKph; + } + + @JsonProperty("wind_degree") + public Integer getWindDegree() { + return windDegree; + } + + @JsonProperty("wind_degree") + public void setWindDegree(Integer windDegree) { + this.windDegree = windDegree; + } + + @JsonProperty("wind_dir") + public String getWindDir() { + return windDir; + } + + @JsonProperty("wind_dir") + public void setWindDir(String windDir) { + this.windDir = windDir; + } + + @JsonProperty("pressure_mb") + public Double getPressureMb() { + return pressureMb; + } + + @JsonProperty("pressure_mb") + public void setPressureMb(Double pressureMb) { + this.pressureMb = pressureMb; + } + + @JsonProperty("pressure_in") + public Double getPressureIn() { + return pressureIn; + } + + @JsonProperty("pressure_in") + public void setPressureIn(Double pressureIn) { + this.pressureIn = pressureIn; + } + + @JsonProperty("precip_mm") + public Double getPrecipMm() { + return precipMm; + } + + @JsonProperty("precip_mm") + public void setPrecipMm(Double precipMm) { + this.precipMm = precipMm; + } + + @JsonProperty("precip_in") + public Double getPrecipIn() { + return precipIn; + } + + @JsonProperty("precip_in") + public void setPrecipIn(Double precipIn) { + this.precipIn = precipIn; + } + + @JsonProperty("humidity") + public Integer getHumidity() { + return humidity; + } + + @JsonProperty("humidity") + public void setHumidity(Integer humidity) { + this.humidity = humidity; + } + + @JsonProperty("cloud") + public Integer getCloud() { + return cloud; + } + + @JsonProperty("cloud") + public void setCloud(Integer cloud) { + this.cloud = cloud; + } + + @JsonProperty("feelslike_c") + public Double getFeelslikeC() { + return feelslikeC; + } + + @JsonProperty("feelslike_c") + public void setFeelslikeC(Double feelslikeC) { + this.feelslikeC = feelslikeC; + } + + @JsonProperty("feelslike_f") + public Double getFeelslikeF() { + return feelslikeF; + } + + @JsonProperty("feelslike_f") + public void setFeelslikeF(Double feelslikeF) { + this.feelslikeF = feelslikeF; + } + + @JsonProperty("windchill_c") + public Double getWindchillC() { + return windchillC; + } + + @JsonProperty("windchill_c") + public void setWindchillC(Double windchillC) { + this.windchillC = windchillC; + } + + @JsonProperty("windchill_f") + public Double getWindchillF() { + return windchillF; + } + + @JsonProperty("windchill_f") + public void setWindchillF(Double windchillF) { + this.windchillF = windchillF; + } + + @JsonProperty("heatindex_c") + public Double getHeatindexC() { + return heatindexC; + } + + @JsonProperty("heatindex_c") + public void setHeatindexC(Double heatindexC) { + this.heatindexC = heatindexC; + } + + @JsonProperty("heatindex_f") + public Double getHeatindexF() { + return heatindexF; + } + + @JsonProperty("heatindex_f") + public void setHeatindexF(Double heatindexF) { + this.heatindexF = heatindexF; + } + + @JsonProperty("dewpoint_c") + public Double getDewpointC() { + return dewpointC; + } + + @JsonProperty("dewpoint_c") + public void setDewpointC(Double dewpointC) { + this.dewpointC = dewpointC; + } + + @JsonProperty("dewpoint_f") + public Double getDewpointF() { + return dewpointF; + } + + @JsonProperty("dewpoint_f") + public void setDewpointF(Double dewpointF) { + this.dewpointF = dewpointF; + } + + @JsonProperty("vis_km") + public Double getVisKm() { + return visKm; + } + + @JsonProperty("vis_km") + public void setVisKm(Double visKm) { + this.visKm = visKm; + } + + @JsonProperty("vis_miles") + public Double getVisMiles() { + return visMiles; + } + + @JsonProperty("vis_miles") + public void setVisMiles(Double visMiles) { + this.visMiles = visMiles; + } + + @JsonProperty("uv") + public Double getUv() { + return uv; + } + + @JsonProperty("uv") + public void setUv(Double uv) { + this.uv = uv; + } + + @JsonProperty("gust_mph") + public Double getGustMph() { + return gustMph; + } + + @JsonProperty("gust_mph") + public void setGustMph(Double gustMph) { + this.gustMph = gustMph; + } + + @JsonProperty("gust_kph") + public Double getGustKph() { + return gustKph; + } + + @JsonProperty("gust_kph") + public void setGustKph(Double gustKph) { + this.gustKph = gustKph; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Current.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("lastUpdatedEpoch"); + sb.append('='); + sb.append(((this.lastUpdatedEpoch == null)?"":this.lastUpdatedEpoch)); + sb.append(','); + sb.append("lastUpdated"); + sb.append('='); + sb.append(((this.lastUpdated == null)?"":this.lastUpdated)); + sb.append(','); + sb.append("tempC"); + sb.append('='); + sb.append(((this.tempC == null)?"":this.tempC)); + sb.append(','); + sb.append("tempF"); + sb.append('='); + sb.append(((this.tempF == null)?"":this.tempF)); + sb.append(','); + sb.append("isDay"); + sb.append('='); + sb.append(((this.isDay == null)?"":this.isDay)); + sb.append(','); + sb.append("condition"); + sb.append('='); + sb.append(((this.condition == null)?"":this.condition)); + sb.append(','); + sb.append("windMph"); + sb.append('='); + sb.append(((this.windMph == null)?"":this.windMph)); + sb.append(','); + sb.append("windKph"); + sb.append('='); + sb.append(((this.windKph == null)?"":this.windKph)); + sb.append(','); + sb.append("windDegree"); + sb.append('='); + sb.append(((this.windDegree == null)?"":this.windDegree)); + sb.append(','); + sb.append("windDir"); + sb.append('='); + sb.append(((this.windDir == null)?"":this.windDir)); + sb.append(','); + sb.append("pressureMb"); + sb.append('='); + sb.append(((this.pressureMb == null)?"":this.pressureMb)); + sb.append(','); + sb.append("pressureIn"); + sb.append('='); + sb.append(((this.pressureIn == null)?"":this.pressureIn)); + sb.append(','); + sb.append("precipMm"); + sb.append('='); + sb.append(((this.precipMm == null)?"":this.precipMm)); + sb.append(','); + sb.append("precipIn"); + sb.append('='); + sb.append(((this.precipIn == null)?"":this.precipIn)); + sb.append(','); + sb.append("humidity"); + sb.append('='); + sb.append(((this.humidity == null)?"":this.humidity)); + sb.append(','); + sb.append("cloud"); + sb.append('='); + sb.append(((this.cloud == null)?"":this.cloud)); + sb.append(','); + sb.append("feelslikeC"); + sb.append('='); + sb.append(((this.feelslikeC == null)?"":this.feelslikeC)); + sb.append(','); + sb.append("feelslikeF"); + sb.append('='); + sb.append(((this.feelslikeF == null)?"":this.feelslikeF)); + sb.append(','); + sb.append("windchillC"); + sb.append('='); + sb.append(((this.windchillC == null)?"":this.windchillC)); + sb.append(','); + sb.append("windchillF"); + sb.append('='); + sb.append(((this.windchillF == null)?"":this.windchillF)); + sb.append(','); + sb.append("heatindexC"); + sb.append('='); + sb.append(((this.heatindexC == null)?"":this.heatindexC)); + sb.append(','); + sb.append("heatindexF"); + sb.append('='); + sb.append(((this.heatindexF == null)?"":this.heatindexF)); + sb.append(','); + sb.append("dewpointC"); + sb.append('='); + sb.append(((this.dewpointC == null)?"":this.dewpointC)); + sb.append(','); + sb.append("dewpointF"); + sb.append('='); + sb.append(((this.dewpointF == null)?"":this.dewpointF)); + sb.append(','); + sb.append("visKm"); + sb.append('='); + sb.append(((this.visKm == null)?"":this.visKm)); + sb.append(','); + sb.append("visMiles"); + sb.append('='); + sb.append(((this.visMiles == null)?"":this.visMiles)); + sb.append(','); + sb.append("uv"); + sb.append('='); + sb.append(((this.uv == null)?"":this.uv)); + sb.append(','); + sb.append("gustMph"); + sb.append('='); + sb.append(((this.gustMph == null)?"":this.gustMph)); + sb.append(','); + sb.append("gustKph"); + sb.append('='); + sb.append(((this.gustKph == null)?"":this.gustKph)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + +} diff --git a/src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java b/src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java new file mode 100644 index 00000000..b06e27ca --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java @@ -0,0 +1,83 @@ + +package org.jointheleague.features.student.first_feature; + +import java.util.LinkedHashMap; +import java.util.Map; +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "location", + "current" +}) +@Generated("jsonschema2pojo") +public class CurrentWeather { + + @JsonProperty("location") + private Location location; + @JsonProperty("current") + private Current current; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); + + @JsonProperty("location") + public Location getLocation() { + return location; + } + + @JsonProperty("location") + public void setLocation(Location location) { + this.location = location; + } + + @JsonProperty("current") + public Current getCurrent() { + return current; + } + + @JsonProperty("current") + public void setCurrent(Current current) { + this.current = current; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(CurrentWeather.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("location"); + sb.append('='); + sb.append(((this.location == null)?"":this.location)); + sb.append(','); + sb.append("current"); + sb.append('='); + sb.append(((this.current == null)?"":this.current)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + +} 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 index 8ab6ffc7..29f6cd1c 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java @@ -8,41 +8,36 @@ import java.util.Random; public class FeatureOne extends FeatureTemplate { - public final String COMMAND = "!joke"; - - public FeatureOne(String channelName) { + public final String COMMAND = "fob??"; + public final String password = "7219"; + + private boolean testMode; + + public FeatureOne(String channelName, boolean testMode) { super(channelName); + this.testMode = testMode; //Create a help embed to describe feature when !help command is sent helpEmbed = new HelpEmbed( COMMAND, - "This command prints cheesy jokes." + "Fob Fobbin" ); } //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; - } - + if (messageContent.startsWith(COMMAND) && messageContent.contains(password)) { + event.sendResponse("how did you guess the password?!"); + } + else if(messageContent.startsWith(COMMAND)) { + event.sendResponse("How can I help? Do not expect responses in a timely manner."); + } + else if(messageContent.trim().startsWith("DIE!")) { + event.sendResponse("You killed Fob! What's wrong with you??"); + if(!testMode) {System.exit(0);}} + else if(messageContent.trim().toLowerCase().startsWith("please help me with this math problem")) { + event.sendResponse("It's simple the answer is " + new Random().nextInt(100)); } } } diff --git a/src/main/java/org/jointheleague/features/student/first_feature/Location.java b/src/main/java/org/jointheleague/features/student/first_feature/Location.java new file mode 100644 index 00000000..6cef34e1 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/Location.java @@ -0,0 +1,185 @@ + +package org.jointheleague.features.student.first_feature; + +import java.util.LinkedHashMap; +import java.util.Map; +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "name", + "region", + "country", + "lat", + "lon", + "tz_id", + "localtime_epoch", + "localtime" +}) +@Generated("jsonschema2pojo") +public class Location { + + @JsonProperty("name") + private String name; + @JsonProperty("region") + private String region; + @JsonProperty("country") + private String country; + @JsonProperty("lat") + private Double lat; + @JsonProperty("lon") + private Double lon; + @JsonProperty("tz_id") + private String tzId; + @JsonProperty("localtime_epoch") + private Integer localtimeEpoch; + @JsonProperty("localtime") + private String localtime; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); + + @JsonProperty("name") + public String getName() { + return name; + } + + @JsonProperty("name") + public void setName(String name) { + this.name = name; + } + + @JsonProperty("region") + public String getRegion() { + return region; + } + + @JsonProperty("region") + public void setRegion(String region) { + this.region = region; + } + + @JsonProperty("country") + public String getCountry() { + return country; + } + + @JsonProperty("country") + public void setCountry(String country) { + this.country = country; + } + + @JsonProperty("lat") + public Double getLat() { + return lat; + } + + @JsonProperty("lat") + public void setLat(Double lat) { + this.lat = lat; + } + + @JsonProperty("lon") + public Double getLon() { + return lon; + } + + @JsonProperty("lon") + public void setLon(Double lon) { + this.lon = lon; + } + + @JsonProperty("tz_id") + public String getTzId() { + return tzId; + } + + @JsonProperty("tz_id") + public void setTzId(String tzId) { + this.tzId = tzId; + } + + @JsonProperty("localtime_epoch") + public Integer getLocaltimeEpoch() { + return localtimeEpoch; + } + + @JsonProperty("localtime_epoch") + public void setLocaltimeEpoch(Integer localtimeEpoch) { + this.localtimeEpoch = localtimeEpoch; + } + + @JsonProperty("localtime") + public String getLocaltime() { + return localtime; + } + + @JsonProperty("localtime") + public void setLocaltime(String localtime) { + this.localtime = localtime; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Location.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("name"); + sb.append('='); + sb.append(((this.name == null)?"":this.name)); + sb.append(','); + sb.append("region"); + sb.append('='); + sb.append(((this.region == null)?"":this.region)); + sb.append(','); + sb.append("country"); + sb.append('='); + sb.append(((this.country == null)?"":this.country)); + sb.append(','); + sb.append("lat"); + sb.append('='); + sb.append(((this.lat == null)?"":this.lat)); + sb.append(','); + sb.append("lon"); + sb.append('='); + sb.append(((this.lon == null)?"":this.lon)); + sb.append(','); + sb.append("tzId"); + sb.append('='); + sb.append(((this.tzId == null)?"":this.tzId)); + sb.append(','); + sb.append("localtimeEpoch"); + sb.append('='); + sb.append(((this.localtimeEpoch == null)?"":this.localtimeEpoch)); + sb.append(','); + sb.append("localtime"); + sb.append('='); + sb.append(((this.localtime == null)?"":this.localtime)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + +} diff --git a/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java b/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java new file mode 100644 index 00000000..b16e4a71 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java @@ -0,0 +1,147 @@ +package org.jointheleague.features.student.first_feature; + +import java.util.Random; + +import org.jointheleague.api_wrapper.ReceivedMessage; +import org.jointheleague.features.examples.third_features.plain_old_java_objects.news_api.ApiExampleWrapper; +import org.jointheleague.features.templates.FeatureTemplate; +import org.springframework.web.reactive.function.client.WebClient; + +import reactor.core.publisher.Mono; + +public class WeatherAPI extends FeatureTemplate{ + +public final String COMMAND = "weather"; +//public final String COMMAND_ = "raw_weather"; + + private static final String key = "7a59fc69444d4617a5a225318250309"; + private static final String URL = "http://api.weatherapi.com/v1/current.json"; + + private WebClient webClient; + + public WeatherAPI(String channelName) { + super(channelName); + // TODO Auto-generated constructor stub + + this.webClient = WebClient + .builder() + .baseUrl(URL) + .build(); + } + + public WeatherAPI(String channelName, WebClient wc) { + super(channelName); + // TODO Auto-generated constructor stub + + this.webClient = wc; + } + + + /*@Deprecated + public String getCurrentWeather(String city) { + + Mono apiExampleWrapperMono = webClient.get() + .uri(uriBuilder -> uriBuilder + .queryParam("q", city) + .queryParam("key", key) + .build()) + .retrieve() + .bodyToMono(String.class); + + return apiExampleWrapperMono.block(); + + }*/ + + public CurrentWeather getCurrentWeather2(String city) { + + Mono apiExampleWrapperMono = webClient.get() + .uri(uriBuilder -> uriBuilder + .queryParam("q", city) + .queryParam("key", key) + .build()) + .retrieve() + .bodyToMono(CurrentWeather.class); + + return apiExampleWrapperMono.block(); + + } + + /*public void printCurrentWeather(String city) { + + Mono apiExampleWrapperMono = webClient.get() + .uri(uriBuilder -> uriBuilder + .queryParam("q", city) + .queryParam("key", key) + .build()) + .retrieve() + .bodyToMono(String.class); + + System.out.println( apiExampleWrapperMono.block()); + + }*/ + + public void handle(ReceivedMessage event) { + String mc = event.getMessageContent(); + if(mc.trim().toLowerCase().equals(COMMAND)/*||mc.trim().toLowerCase().equals(COMMAND_)*/) { + event.sendResponse("Please add a city."); + } + + else if(mc.trim().toLowerCase().startsWith(COMMAND)) { + + String city = mc.trim().substring(COMMAND.length()+1); + + //String json = getCurrentWeather(city); + + CurrentWeather actualObject = getCurrentWeather2(city); + + //event.sendResponse(json); + + /*String tempKey = "\"temp_f\":"; + int tempStart = json.indexOf(tempKey) + tempKey.length(); + int tempEnd = json.indexOf(",", tempStart); + String temp = json.substring(tempStart, tempEnd); + + // Extract humidity + String humidityKey = "\"humidity\":"; + int humidityStart = json.indexOf(humidityKey) + humidityKey.length(); + int humidityEnd = json.indexOf(",", humidityStart); + String humidity = json.substring(humidityStart, humidityEnd); + + String iconKey = "\"icon\":\""; + int iconStart = json.indexOf(iconKey) + iconKey.length(); + int iconEnd = json.indexOf("\"", iconStart); + String icon = json.substring(iconStart, iconEnd); + + String regionKey = "\"region\":\""; + int regionStart = json.indexOf(regionKey) + regionKey.length(); + int regionEnd = json.indexOf("\"", regionStart); + String region = json.substring(regionStart, regionEnd); + + String countryKey = "\"country\":\""; + int countryStart = json.indexOf(countryKey) + countryKey.length(); + int countryEnd = json.indexOf("\"", countryStart); + String country = json.substring(countryStart, countryEnd);*/ + + event.sendResponse("__Conditions for " + city + ", "+actualObject.getLocation().getCountry()+".__" ); + event.sendResponse("Temperature (F): "+actualObject.getCurrent().getTempF()); + event.sendResponse("Humidity (%): "+actualObject.getCurrent().getHumidity()); + event.sendResponse("https:"+actualObject.getCurrent().getCondition().getIcon()); + + + // TODO: Make a thread run this code so that I can have an external Timer + // and after some time has elapsed interrupt and say "city not found" + + } + /*if(mc.trim().toLowerCase().startsWith(COMMAND_)) { + String city = mc.trim().substring(COMMAND.length()+1); + + String json = getCurrentWeather(city); + event.sendResponse(json); + }*/ + } + + + + + +} diff --git a/src/test/java/org/jointheleague/features/student/first_feature/BananaTest.java b/src/test/java/org/jointheleague/features/student/first_feature/BananaTest.java new file mode 100644 index 00000000..50b4ddd8 --- /dev/null +++ b/src/test/java/org/jointheleague/features/student/first_feature/BananaTest.java @@ -0,0 +1,237 @@ +package org.jointheleague.features.student.first_feature; + +import org.jointheleague.api_wrapper.ReceivedMessage; +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.*; + +public class BananaTest { + + private final String testChannelName = "test"; + private final BananaSoftware banana = new BananaSoftware(testChannelName); + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + public final String COMMAND = "banana"; + public final String password = "ON"; + + @Mock + private ReceivedMessage receivedMessage; + + @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 itShouldConfirmBananasExist() { + + //Given + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana"); + banana.BananaMode=false; + + //When + banana.handle(receivedMessage); + + //Then + verify(receivedMessage, times(1)).sendResponse("Message received! BananaMode remains off."); + + } + + @Test + void itShouldTurnOnBananaMode() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana on"); + banana.BananaMode=false; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("Message received! BananaMode is now on."); + assertEquals(banana.BananaMode,true); + + } + + @Test + void alreadyOn() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana on"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("Message received! BananaMode is already on."); + assertEquals(banana.BananaMode,true); + } + + @Test void off(){ + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana OFF"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("Got it. BananaMode is now off."); + assertEquals(banana.BananaMode,false); + } + + @Test void notEnoughNumbers(){ + + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana add 5"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("So youre meant to *include* __both__ numbers in your argument"); + assertEquals(banana.BananaMode,true); + } + + @Test + void adding() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana add 5 6"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse(5+6+""); + assertEquals(banana.BananaMode,true); + } + + @Test void addingButDecimals() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana add 5.2 6"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("hey i dont think thats an integer"); + assertEquals(banana.BananaMode,true); + } + + @Test void subtracting() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana subt 5 6"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse(5-6+""); + assertEquals(banana.BananaMode,true); + } + + @Test void subtractingButDecimals() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana subt 5.2 6"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("hey i dont think thats an integer"); + assertEquals(banana.BananaMode,true); + } + @Test void multiplying() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana mult 5 5"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse(5*5+""); + assertEquals(banana.BananaMode,true); + } + + @Test void multiplyingButDecimals() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana mult 5.2 6.5"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("hey i dont think thats an integer"); + assertEquals(banana.BananaMode,true); + } + + @Test + void noBanana(){ + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("bonoonoo add 5"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, never()).sendResponse(anyString());; + assertEquals(banana.BananaMode,true); + } + + @Test + void noBanana2(){ + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana a"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + assertEquals(banana.BananaMode,true); + } + + @Test void dividing() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana divi 4 2"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse(4/2+""); + assertEquals(banana.BananaMode,true); + } + + @Test void dividingButDecimals() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana divi 5.2 6"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("hey i dont think thats an integer"); + assertEquals(banana.BananaMode,true); + } + @Test void dividingButZero() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana divi 5 0"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("bruh"); + assertEquals(banana.BananaMode,true); + } + +} 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..b22f27a6 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 @@ -10,7 +10,7 @@ import org.mockito.MockitoAnnotations; import java.io.ByteArrayOutputStream; -import java.io.PrintStream; +import java.io.PrintStream; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -21,13 +21,14 @@ public class FeatureOneTest { private final String testChannelName = "test"; - private final FeatureOne featureOne = new FeatureOne(testChannelName); + private final FeatureOne featureOne = new FeatureOne(testChannelName, true); private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; @Mock private ReceivedMessage receivedMessage; + @BeforeEach void setUp() { @@ -60,7 +61,7 @@ void itShouldHaveACommand() { assertNotEquals("", command); assertNotEquals("!", command); assertNotEquals("!command", command); - assertEquals('!', command.charAt(0)); + //assertEquals('!', command.charAt(0)); assertNotNull(command); } @@ -76,6 +77,44 @@ void itShouldHandleMessagesWithCommand() { //Then verify(receivedMessage, times(1)).sendResponse(anyString()); } + @Test + void die() { + //Given + HelpEmbed helpEmbed = new HelpEmbed(featureOne.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("DIE!!"); + + //When + featureOne.handle(receivedMessage); + + //Then + verify(receivedMessage, times(1)).sendResponse("You killed Fob! What's wrong with you??"); + + } + @Test + void math() { + //Given + HelpEmbed helpEmbed = new HelpEmbed(featureOne.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("please help me with this math problem"); + + //When + featureOne.handle(receivedMessage); + + //Then + verify(receivedMessage, times(1)).sendResponse(contains("It's simple the answer is ")); + } + @Test + void password() { + //Given + HelpEmbed helpEmbed = new HelpEmbed(featureOne.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("fob?? I think the password is 7219..."); + //when(receivedMessage.getMessageContent()).thenReturn(contains("7219")); + + //When + featureOne.handle(receivedMessage); + + //Then + verify(receivedMessage, times(1)).sendResponse("how did you guess the password?!"); + } @Test void itShouldNotHandleMessagesWithoutCommand() { diff --git a/src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java b/src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java new file mode 100644 index 00000000..1508926e --- /dev/null +++ b/src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java @@ -0,0 +1,162 @@ +package org.jointheleague.features.student.first_feature; + +import org.jointheleague.api_wrapper.ReceivedMessage; +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 org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.UriBuilder; + + +import reactor.core.publisher.Mono; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.function.Function; + +import java.net.URI; + +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.*; + +public class WeatherTest { + + private final String testChannelName = "test"; + WeatherAPI test; + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + public final String COMMAND = "weather"; + public final String COMMAND_ = "raw_weather"; + + @Mock + public WebClient wc; + + @Mock + WebClient.RequestHeadersUriSpec requestHeadersUriSpecMock; + + @Mock + WebClient.RequestHeadersSpec requestHeadersSpecMock; + + @Mock + WebClient.ResponseSpec responseSpecMock; + + @Mock + Mono m_cw; + + @Mock + ReceivedMessage rm; + + @Mock + UriBuilder urib; + + //@Mock + //URI uri; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + System.setOut(new PrintStream(outContent)); + test = new WeatherAPI(testChannelName, wc); + } + + @Test + void confirmProperResponse() { + + //Given + String city = "Bob"; + + CurrentWeather cww = new CurrentWeather(); + Current c = new Current(); c.setTempF(37.5); c.setHumidity(30); Condition cond = new Condition(); cond.setIcon("//cdn.weatherapi.com/weather/64x64/day/122.png");c.setCondition(cond); cww.setCurrent(c); + Location l = new Location(); l.setCountry("Boblandia"); cww.setLocation(l); + + //When + when(wc.get()).thenReturn(requestHeadersUriSpecMock); + when(requestHeadersUriSpecMock.uri((Function) any())).thenReturn(requestHeadersSpecMock); + when(requestHeadersSpecMock.retrieve()).thenReturn(responseSpecMock); + when(responseSpecMock.bodyToMono(CurrentWeather.class)).thenReturn(m_cw); + + when(urib.queryParam("q", city)).thenReturn(urib); + when(urib.queryParam("key","7a59fc69444d4617a5a225318250309")).thenReturn(urib); + when(urib.build()).thenReturn(null); + + when(m_cw.block()).thenReturn(cww); + + when(rm.getMessageContent()).thenReturn("weather Bob"); // change this + + test.handle(rm); + + //Then + + verify(rm,times(1)).sendResponse("__Conditions for Bob, Boblandia.__"); + verify(rm,times(1)).sendResponse("Temperature (F): 37.5"); + verify(rm,times(1)).sendResponse("Humidity (%): 30"); + verify(rm,times(1)).sendResponse("https://cdn.weatherapi.com/weather/64x64/day/122.png"); + + + } + + @Test + void equals() { + //Given + String city = "Bob"; + + CurrentWeather cww = new CurrentWeather(); + Current c = new Current(); c.setTempF(37.5); c.setHumidity(30); Condition cond = new Condition(); cond.setIcon("//cdn.weatherapi.com/weather/64x64/day/122.png");c.setCondition(cond); cww.setCurrent(c); + Location l = new Location(); l.setCountry("Boblandia"); cww.setLocation(l); + + //When + when(wc.get()).thenReturn(requestHeadersUriSpecMock); + when(requestHeadersUriSpecMock.uri((Function) any())).thenReturn(requestHeadersSpecMock); + when(requestHeadersSpecMock.retrieve()).thenReturn(responseSpecMock); + when(responseSpecMock.bodyToMono(CurrentWeather.class)).thenReturn(m_cw); + + when(m_cw.block()).thenReturn(cww); + + when(urib.queryParam("q", city)).thenReturn(urib); + when(urib.queryParam("key","7a59fc69444d4617a5a225318250309")).thenReturn(urib); + when(urib.build()).thenReturn(null); + + when(rm.getMessageContent()).thenReturn("weather"); // change this + + test.handle(rm); + + //Then + + verify(rm,times(1)).sendResponse("Please add a city."); + } + + @Test + + void nothing() { + String city = "Bob"; + + CurrentWeather cww = new CurrentWeather(); + Current c = new Current(); c.setTempF(37.5); c.setHumidity(30); Condition cond = new Condition(); cond.setIcon("//cdn.weatherapi.com/weather/64x64/day/122.png");c.setCondition(cond); cww.setCurrent(c); + Location l = new Location(); l.setCountry("Boblandia"); cww.setLocation(l); + + //When + when(wc.get()).thenReturn(requestHeadersUriSpecMock); + when(requestHeadersUriSpecMock.uri((Function) any())).thenReturn(requestHeadersSpecMock); + when(requestHeadersSpecMock.retrieve()).thenReturn(responseSpecMock); + when(responseSpecMock.bodyToMono(CurrentWeather.class)).thenReturn(m_cw); + + when(m_cw.block()).thenReturn(cww); + + when(rm.getMessageContent()).thenReturn("banana"); // change this + + test.handle(rm); + + //Then + + verify(rm,never()).sendResponse(anyString()); + } +}