From be21c6a892b1fab0bd551af385e473663eb407be Mon Sep 17 00:00:00 2001 From: DxsSucuk Date: Sat, 11 Jun 2022 03:32:51 +0200 Subject: [PATCH] * Fixed NSFW command Issue. --- pom.xml | 2 +- .../presti/ree6/commands/impl/nsfw/NSFW.java | 48 +++++++++++-------- src/main/java/de/presti/ree6/main/Main.java | 4 +- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index a2cd12cb5..1adf1daa3 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ de.presti Ree6 - 1.7.11 + 1.7.12 jar Ree6 Ree6 is an open-Source Discord Bot. diff --git a/src/main/java/de/presti/ree6/commands/impl/nsfw/NSFW.java b/src/main/java/de/presti/ree6/commands/impl/nsfw/NSFW.java index 7328c06e7..6639caa48 100644 --- a/src/main/java/de/presti/ree6/commands/impl/nsfw/NSFW.java +++ b/src/main/java/de/presti/ree6/commands/impl/nsfw/NSFW.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.function.Consumer; @Command(name = "nsfw", description = "Get NSFW Image for reddit.com/r/hentai", category = Category.NSFW) public class NSFW implements ICommand { @@ -31,7 +32,7 @@ public void onPerform(CommandEvent commandEvent) { commandEvent.getInteractionHook().sendMessage("Searching for Image...").complete() : commandEvent.getTextChannel().sendMessage("Searching for Image...").complete(); - JsonElement jsonElement = RequestUtility.request(new RequestUtility.Request("https://www.reddit.com/r/hentai/new.json?sort=hot&limit=100")); + JsonElement jsonElement = RequestUtility.request(new RequestUtility.Request("https://www.reddit.com/r/hentai/new.json?sort=hot&limit=50")); if (jsonElement.isJsonObject() && jsonElement.getAsJsonObject().has("data") && @@ -43,27 +44,27 @@ public void onPerform(CommandEvent commandEvent) { List images = new ArrayList<>(); - children.forEach(post -> { - if (post.isJsonObject() && post.getAsJsonObject().has("data") && - post.getAsJsonObject().getAsJsonObject("data").has("url") && - post.getAsJsonObject().getAsJsonObject("data").has("post_hint") && - post.getAsJsonObject().getAsJsonObject("data").get("url").isJsonPrimitive() && - post.getAsJsonObject().getAsJsonObject("data").get("post_hint").isJsonPrimitive() && - post.getAsJsonObject().getAsJsonObject("data").getAsJsonPrimitive("url").isString() && - post.getAsJsonObject().getAsJsonObject("data").getAsJsonPrimitive("post_hint").isString()) { - - JsonObject postObject = post.getAsJsonObject().getAsJsonObject("data"); - - String postHint = postObject.getAsJsonPrimitive("post_hint").getAsString(), - fileUrl = postObject.getAsJsonObject("url").getAsString(); - if ((postHint.equalsIgnoreCase("image") || - postHint.equalsIgnoreCase("link") || - postHint.equalsIgnoreCase("rich:video")) && - !fileUrl.toLowerCase(Locale.ROOT).startsWith("https://www.reddit.com/gallery/")) { - images.add(fileUrl); + for (JsonElement child : children) { + if (child.isJsonObject() && + child.getAsJsonObject().has("data") && + child.getAsJsonObject().get("data").isJsonObject()) { + + JsonObject data = child.getAsJsonObject().getAsJsonObject("data"); + + if (data.get("url") != null && data.get("url").isJsonPrimitive() && + data.get("post_hint") != null && data.get("post_hint").isJsonPrimitive()) { + String postHint = data.getAsJsonPrimitive("post_hint").getAsString(), + fileUrl = data.getAsJsonPrimitive("url").getAsString(); + if ((postHint.equalsIgnoreCase("image") || + postHint.equalsIgnoreCase("link") || + postHint.equalsIgnoreCase("rich:video")) && + !fileUrl.toLowerCase(Locale.ROOT).startsWith("https://www.reddit.com/gallery/") && + !fileUrl.toLowerCase(Locale.ROOT).startsWith("https://redgifs.com/")) { + images.add(fileUrl); + } } } - }); + } if (!images.isEmpty()) { String randomUrl = images.get(RandomUtils.secureRandom.nextInt(images.size() - 1)); @@ -72,7 +73,12 @@ public void onPerform(CommandEvent commandEvent) { em.setImage(randomUrl); em.setFooter(commandEvent.getMember().getUser().getAsTag() + " - " + Data.ADVERTISEMENT, commandEvent.getMember().getUser().getAvatarUrl()); - message.editMessageEmbeds(em.build()).queue(); + if (commandEvent.isSlashCommand()) { + message.editMessage("Image found!").queue(); + Main.getInstance().getCommandManager().sendMessage(em, commandEvent.getTextChannel(), null); + } else { + message.editMessageEmbeds(em.build()).queue(message1 -> message1.editMessage("Image found!").queue()); + } } else { message.editMessage("We received an empty Image list from Reddit? Please try again later!").delay(Duration.ofSeconds(5)).flatMap(Message::delete).queue(); } diff --git a/src/main/java/de/presti/ree6/main/Main.java b/src/main/java/de/presti/ree6/main/Main.java index 520202c5e..56a9dba1a 100644 --- a/src/main/java/de/presti/ree6/main/Main.java +++ b/src/main/java/de/presti/ree6/main/Main.java @@ -98,7 +98,7 @@ public static void main(String[] args) { // Create a RayGun Client to send Exception to an external Service for Bug fixing. Thread.setDefaultUncaughtExceptionHandler((t, e) -> { RaygunClient raygunClient = new RaygunClient(instance.config.getConfiguration().getString("raygun.apitoken")); - raygunClient.setVersion("1.7.11"); + raygunClient.setVersion("1.7.12"); }); // Create a new connection between the Application and the SQL-Server. @@ -132,7 +132,7 @@ public static void main(String[] args) { // Create a new Instance of the Bot, as well as add the Events. try { - BotWorker.createBot(BotVersion.PUBLIC, "1.7.11"); + BotWorker.createBot(BotVersion.PUBLIC, "1.7.12"); instance.musicWorker = new MusicWorker(); instance.addEvents(); } catch (Exception ex) {