From 4db157d1d1d9460af01c4cc54e56ce27d24cd1bf Mon Sep 17 00:00:00 2001 From: SebClem Date: Thu, 23 Jun 2022 15:43:49 +0200 Subject: [PATCH] :hammer: Rebuild http request with new client --- src/main/java/net/Broken/MainBot.java | 3 -- .../java/net/Broken/SlashCommands/Cat.java | 30 +++++++++++-------- .../Broken/SlashCommands/Over18/Madame.java | 4 +-- .../Tools/Command/NumberedSlashCommand.java | 4 +-- .../DayListener/Listeners/DailyMadame.java | 2 +- .../Broken/Tools/FindContentOnWebPage.java | 28 +++++++++-------- 6 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/Broken/MainBot.java b/src/main/java/net/Broken/MainBot.java index db53fc6..7a74655 100644 --- a/src/main/java/net/Broken/MainBot.java +++ b/src/main/java/net/Broken/MainBot.java @@ -17,10 +17,8 @@ import java.util.HashMap; @ConfigurationPropertiesScan public class MainBot { - public static HashMap commandes = new HashMap<>(); public static final HashMap slashCommands = new HashMap<>(); public static HashMap mutualGuildCount = new HashMap<>(); - public static boolean roleFlag = false; public static JDA jda; public static boolean ready = false; @@ -46,7 +44,6 @@ public class MainBot { return 1; })); } - Init.polish(jda, config); ready = true; } diff --git a/src/main/java/net/Broken/SlashCommands/Cat.java b/src/main/java/net/Broken/SlashCommands/Cat.java index b9f75a2..315e273 100644 --- a/src/main/java/net/Broken/SlashCommands/Cat.java +++ b/src/main/java/net/Broken/SlashCommands/Cat.java @@ -15,8 +15,12 @@ import org.apache.logging.log4j.Logger; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.net.URI; import java.net.URL; import java.net.URLConnection; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; @@ -30,23 +34,25 @@ public class Cat implements SlashCommand { @Override public void action(SlashCommandEvent event) { try { - URL urlC = new URL("http://aws.random.cat/meo"); - URLConnection yc = urlC.openConnection(); - BufferedReader in = new BufferedReader(new InputStreamReader( - yc.getInputStream(), StandardCharsets.UTF_8)); - String inputLine; - StringBuilder a = new StringBuilder(); - while ((inputLine = in.readLine()) != null) - a.append(inputLine); - in.close(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://aws.random.cat/meow")) + .GET() + .build(); + + + HttpClient client = HttpClient.newHttpClient(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + if (response.statusCode() != 200) { + logger.warn("[CAT] Fail to fetch cat: Status Code: " + response.statusCode() + " Body:" + response.body()); + throw new IOException(); + } TypeReference> typeRef = new TypeReference<>() {}; ObjectMapper mapper = new ObjectMapper(); - HashMap json = mapper.readValue(a.toString(), typeRef); - + HashMap json = mapper.readValue(response.body(), typeRef); event.reply(json.get("file")).queue(); - } catch (IOException e) { + } catch (InterruptedException | IOException e) { logger.catching(e); event.reply(new MessageBuilder().setEmbeds(EmbedMessageUtils.getInternalError()).build()).setEphemeral(true).queue(); } diff --git a/src/main/java/net/Broken/SlashCommands/Over18/Madame.java b/src/main/java/net/Broken/SlashCommands/Over18/Madame.java index b7854c7..377eb68 100644 --- a/src/main/java/net/Broken/SlashCommands/Over18/Madame.java +++ b/src/main/java/net/Broken/SlashCommands/Over18/Madame.java @@ -23,7 +23,7 @@ public class Madame extends NumberedSlashCommand { * @throws StringIndexOutOfBoundsException * @throws IOException */ - private boolean scanPageForTipeee(String url, Logger logger) throws StringIndexOutOfBoundsException, IOException { + private boolean scanPageForTipeee(String url, Logger logger) throws StringIndexOutOfBoundsException, IOException, InterruptedException { String content = FindContentOnWebPage.getSourceUrl(url); String imgClickLink = content.substring(content.indexOf("class=\"post-content")); imgClickLink = imgClickLink.substring(imgClickLink.indexOf(" response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + return response.body(); } } \ No newline at end of file