Skip to content

Commit

Permalink
* Fixed NSFW command Issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
DxsSucuk committed Jun 11, 2022
1 parent 0d7ee1a commit be21c6a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>de.presti</groupId>
<artifactId>Ree6</artifactId>
<version>1.7.11</version>
<version>1.7.12</version>
<packaging>jar</packaging>
<name>Ree6</name>
<description>Ree6 is an open-Source Discord Bot.</description>
Expand Down
48 changes: 27 additions & 21 deletions src/main/java/de/presti/ree6/commands/impl/nsfw/NSFW.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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") &&
Expand All @@ -43,27 +44,27 @@ public void onPerform(CommandEvent commandEvent) {

List<String> 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));
Expand All @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/presti/ree6/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit be21c6a

Please sign in to comment.