From fee44bc31f4d2b293e9079abeaf022af280f37fd Mon Sep 17 00:00:00 2001 From: DerenDesign <67876073+DerenDesign@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:37:13 -0700 Subject: [PATCH] Done --- .gitignore | 1 + DiscordBot_v2 | 1 + bin/.gitignore | 2 + build.gradle | 1 + src/main/java/org/jointheleague/Launcher.java | 2 +- .../jointheleague/discord_bot/DiscordBot.java | 12 +- .../examples/third_features/ImgurApi.java | 58 ++++++ .../examples/third_features/MazeGame.java | 196 ++++++++++++++++++ .../third_features/RockPaperSciApi.java | 99 +++++++++ 9 files changed, 369 insertions(+), 3 deletions(-) create mode 160000 DiscordBot_v2 create mode 100644 bin/.gitignore create mode 100644 src/main/java/org/jointheleague/features/examples/third_features/ImgurApi.java create mode 100644 src/main/java/org/jointheleague/features/examples/third_features/MazeGame.java create mode 100644 src/main/java/org/jointheleague/features/examples/third_features/RockPaperSciApi.java diff --git a/.gitignore b/.gitignore index 19f55917..281d47b4 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ src/main/java/org.jointheleague.Launcher.java .travis.yml run.sh appspec.yml +/.gradle/ diff --git a/DiscordBot_v2 b/DiscordBot_v2 new file mode 160000 index 00000000..cf1e8571 --- /dev/null +++ b/DiscordBot_v2 @@ -0,0 +1 @@ +Subproject commit cf1e8571985fea0fdc5813d86edf2a42bf5feace diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 00000000..06e4d12f --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,2 @@ +/test/ +/main/ diff --git a/build.gradle b/build.gradle index bee38ebc..46ef5ec5 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,7 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.7.0' testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.4.2' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2' + implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13' } group = 'org.jointheleague' diff --git a/src/main/java/org/jointheleague/Launcher.java b/src/main/java/org/jointheleague/Launcher.java index 629e352c..d49ed67e 100644 --- a/src/main/java/org/jointheleague/Launcher.java +++ b/src/main/java/org/jointheleague/Launcher.java @@ -9,7 +9,7 @@ public static void main(String[] args) { String channelName = System.getenv("CHANNEL_NAME"); String discordToken = System.getenv("DISCORD_TOKEN"); boolean printDiscordInvite = true; - + System.out.println("Running :)"); //Instantiate DiscordBot and connect DiscordBot discordBot = new DiscordBot(discordToken, channelName); discordBot.connect(printDiscordInvite); diff --git a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java index fa40f61f..4f9b2bf8 100644 --- a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java +++ b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java @@ -1,11 +1,15 @@ package org.jointheleague.discord_bot; import org.javacord.api.DiscordApi; + import org.javacord.api.DiscordApiBuilder; 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.ImgurApi; +import org.jointheleague.features.examples.third_features.MazeGame; import org.jointheleague.features.examples.third_features.NewsApi; +import org.jointheleague.features.examples.third_features.RockPaperSciApi; import org.jointheleague.features.examples.first_features.CurrentTime; import org.jointheleague.features.examples.first_features.RandomNumber; import org.jointheleague.features.help_embed.HelpListener; @@ -24,12 +28,13 @@ public DiscordBot(String token, String channelName) { this.token = token; this.channelName = channelName; helpListener = new HelpListener(channelName); + System.out.println(channelName); } public void connect(boolean printInvite) { - api = new DiscordApiBuilder().setToken(token).login().join(); - + api = new DiscordApiBuilder().setAllIntents().setToken(token).login().join(); + //Print the URL to invite the bot if (printInvite) { System.out.println("To authorize your bot, send your teacher this link: " + api.createBotInvite() @@ -48,6 +53,9 @@ public void connect(boolean printInvite) { addFeature(new HighLowGame(channelName)); addFeature(new NewsApi(channelName)); addFeature(new CatFactsApi(channelName)); + addFeature(new ImgurApi(channelName)); + addFeature(new RockPaperSciApi(channelName)); + addFeature(new MazeGame(channelName)); } private void addFeature(Feature feature){ diff --git a/src/main/java/org/jointheleague/features/examples/third_features/ImgurApi.java b/src/main/java/org/jointheleague/features/examples/third_features/ImgurApi.java new file mode 100644 index 00000000..8e694786 --- /dev/null +++ b/src/main/java/org/jointheleague/features/examples/third_features/ImgurApi.java @@ -0,0 +1,58 @@ +package org.jointheleague.features.examples.third_features; + +import java.awt.Event; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.concurrent.CompletableFuture; +import org.javacord.api.entity.message.Message; +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.springframework.web.reactive.function.client.WebClient; + + +public class ImgurApi extends Feature { + + public final String IMAGE_COMMAND = "!image"; + private WebClient webClient; + private static final String baseUrl = "https://imgur.com"; + static String imageName = ""; + String clientID = "4d7979a11b2ca0c"; + String clientSecret = "249c3c725c980eae4dcaeb1ef7e2617fabee7e94"; + + public ImgurApi(String channelName) { + super(channelName); + + //Create a help embed to describe feature when !help command is sent + helpEmbed = new HelpEmbed( + IMAGE_COMMAND, + "Use this method to search for any image or video you'd like!" + ); + this.webClient = WebClient + .builder() + .baseUrl(baseUrl) + .build(); + } + + @Override + public void handle(MessageCreateEvent event) { + String messageContent = event.getMessageContent(); + if (messageContent.contains(IMAGE_COMMAND)) { + //respond to message here! + //System.out.println("Hi!"); + imageName = event.getMessageContent().replaceAll("!image ", ""); + + String output = "https://imgur.com/search?q="+imageName; + + event.getChannel().sendMessage(output); + + + } + + } + +} diff --git a/src/main/java/org/jointheleague/features/examples/third_features/MazeGame.java b/src/main/java/org/jointheleague/features/examples/third_features/MazeGame.java new file mode 100644 index 00000000..e0c0bcb0 --- /dev/null +++ b/src/main/java/org/jointheleague/features/examples/third_features/MazeGame.java @@ -0,0 +1,196 @@ +package org.jointheleague.features.examples.third_features; + +import java.awt.Event; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.Random; +import java.util.concurrent.CompletableFuture; +import org.javacord.api.entity.message.Message; +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.springframework.web.reactive.function.client.WebClient; + +import io.netty.channel.Channel; + + +public class MazeGame extends Feature { + + public final String IMAGE_COMMAND = "!MazeGame"; + private WebClient webClient; + int row = 4; + int col = 0; + private static final String baseUrl = ""; + String[][] Array = {{"⬜","⬛","⬛","⬛","⬛","🟩"}, + {"⬛","⬛","⬛","⬛","⬛","⬛"}, + {"⬛","⬛","⬜","⬛","⬜","⬜"}, + {"⬛","⬛","⬛","⬛","⬛","⬜"}, + {"🟨","⬜","⬜","⬜","⬛","⬛"}}; + String pos = Array[row][col]; + + public MazeGame(String channelName) { + super(channelName); + + //Create a help embed to describe feature when !help command is sent + helpEmbed = new HelpEmbed( + IMAGE_COMMAND, + "Use this method to a maze game!" + ); + this.webClient = WebClient + .builder() + .baseUrl(baseUrl) + .build(); + } + + @Override + public void handle(MessageCreateEvent event) { + + String messageContent = event.getMessageContent(); + if (messageContent.contains(IMAGE_COMMAND)) { + //respond to message here! + String maze = ""; + for (int i = 0; i < Array.length; i++) { + + // Loop through all elements of current row + for (int j = 0; j < Array[i].length; j++) { + maze += Array[i][j] + " "; + + } + maze = maze + "\n"; + + } + event.getChannel().sendMessage(maze); + } + String message = event.getMessageContent(); + if (message.contains(IMAGE_COMMAND)) { + + //respond to message here! + String direction = event.getMessageContent().replaceAll("!MazeGame ", ""); + if(direction.equalsIgnoreCase("left")) { + //1 Check if can move (barrier or goes out of bounds) + //2 If can't, print can't + String oldpos = Array[row][col]; + //3 If can, replace that square with black, and the new one with yello + if(col-1 >= 0) { + col = col-1; + } + else { + event.getChannel().sendMessage("You can't move here! Out of bounds"); + } + String newpositon = Array[row][col]; + + if(newpositon == "⬛") { + Array[row][col]= "🟨"; + Array[row][col+1] = "⬛"; + + event.getChannel().sendMessage(printMaze(Array)); + } + else if(newpositon == "🟩") { + event.getChannel().sendMessage("You won!"); + } + + + } + if(direction.equalsIgnoreCase("right")) { + String oldpos = Array[row][col]; + System.out.println("Array val:" + Array[row][0].length()); + //3 If can, replace that square with black, and the new one with yello + if(col+1 < 6) { + col = col+1; + } + else { + event.getChannel().sendMessage("You can't move here! Out of bounds"); + } + String newpositon = Array[row][col]; + + if(newpositon == "⬛") { + + Array[row][col] = "🟨"; + Array[row][col-1] = "⬛"; + + event.getChannel().sendMessage(printMaze(Array)); + } + else if(newpositon == "🟩") { + event.getChannel().sendMessage("You won!"); + } + System.out.println("Col: " + col); + + } + if(direction.equalsIgnoreCase("up")) { + String oldpos = Array[row][col]; + System.out.println(row); + //3 If can, replace that square with black, and the new one with yello + if(row-1 >= 0) { + row = row-1; + + } + else { + event.getChannel().sendMessage("You can't move here! Out of bounds."); + + + } + String newpositon = Array[row][col]; + + if(newpositon == "⬛") { + Array[row][col] = "🟨"; + Array[row+1][col] = "⬛"; + + + } + else if(newpositon == "🟩") { + event.getChannel().sendMessage("You won!"); + } + + event.getChannel().sendMessage(printMaze(Array)); + System.out.println(row); + + } + if(direction.equalsIgnoreCase("down")) { + String oldpos = Array[row][col]; + //3 If can, replace that square with black, and the new one with yello + if(row-1 < Array[0][col].length()) { + row = row+1; + } + else { + event.getChannel().sendMessage("You can't move here! Out of bounds"); + } + String newpositon = Array[row][col]; + + if(newpositon == "⬛") { + + Array[row][col] = "🟨"; + Array[row-1][col] = "⬛"; + + event.getChannel().sendMessage(printMaze(Array)); + } + + else if(newpositon == "🟩") { + event.getChannel().sendMessage("You won!"); + } + + + } + + } + + + + } + public String printMaze(String[][] Array) { + String maze = ""; + for (int i = 0; i < Array.length; i++) { + + // Loop through all elements of current row + for (int j = 0; j < Array[i].length; j++) { + + maze += Array[i][j]+""; + } + maze+= "\n"; + } + return maze; + } + } diff --git a/src/main/java/org/jointheleague/features/examples/third_features/RockPaperSciApi.java b/src/main/java/org/jointheleague/features/examples/third_features/RockPaperSciApi.java new file mode 100644 index 00000000..aefd21bf --- /dev/null +++ b/src/main/java/org/jointheleague/features/examples/third_features/RockPaperSciApi.java @@ -0,0 +1,99 @@ +package org.jointheleague.features.examples.third_features; + +import java.awt.Event; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.Random; +import java.util.concurrent.CompletableFuture; +import org.javacord.api.entity.message.Message; +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.springframework.web.reactive.function.client.WebClient; + + +public class RockPaperSciApi extends Feature { + + public final String IMAGE_COMMAND = "!rps"; + private WebClient webClient; + private static final String baseUrl = ""; + + + public RockPaperSciApi(String channelName) { + super(channelName); + + //Create a help embed to describe feature when !help command is sent + helpEmbed = new HelpEmbed( + IMAGE_COMMAND, + "Use this method to play rock, paper, scissors!" + ); + this.webClient = WebClient + .builder() + .baseUrl(baseUrl) + .build(); + } + + @Override + public void handle(MessageCreateEvent event) { + String messageContent = event.getMessageContent(); + //System.out.println("Inside method"); + if (messageContent.contains(IMAGE_COMMAND)) { + //respond to message here! + System.out.println("Printing - Inside If"); + String getUserInput = event.getMessageContent().replaceAll("!rps ", ""); + String robotInput = ""; + String gameResult = ""; + Random ran = new Random(); + int random = ran.nextInt(3); + //System.out.println(random); + if(random == 0) { + robotInput = "Rock"; + } + if(random == 1) { + robotInput = "Paper"; + } + if(random == 2) { + robotInput = "Scissors"; + } + //System.out.println("Choosing bot output"); + + event.getChannel().sendMessage(robotInput); + + if(getUserInput.equalsIgnoreCase("Rock") && robotInput.equals("Rock")) { + gameResult = "Tie"; + } + if(getUserInput.equalsIgnoreCase("Paper") && robotInput.equals("Rock")) { + gameResult = "Win"; + } + if(getUserInput.equalsIgnoreCase("Scissors") && robotInput.equals("Rock")) { + gameResult = "Lose"; + } + if(getUserInput.equalsIgnoreCase("Rock") && robotInput.equals("Scissors")) { + gameResult = "Win"; + } + if(getUserInput.equalsIgnoreCase("Rock") && robotInput.equals("Paper")) { + gameResult = "Win"; + } + if(getUserInput.equalsIgnoreCase("Paper") && robotInput.equals("Scissors")) { + gameResult = "Lose"; + } + if(getUserInput.equalsIgnoreCase("Paper") && robotInput.equals("Paper")) { + gameResult = "Tie"; + } + if(getUserInput.equalsIgnoreCase("Scissors") && robotInput.equals("Scissors")) { + gameResult = "Tie"; + } + if(getUserInput.equalsIgnoreCase("Scissors") && robotInput.equals("Paper")) { + gameResult = "Win"; + } + + event.getChannel().sendMessage(gameResult); + } + + } + +}