use of net.kodehawa.lib.imageboards.ImageBoard in project MantaroBot by Mantaro.
the class ImageboardUtils method getImage.
public static void getImage(ImageBoard<?> api, ImageRequestType type, boolean nsfwOnly, String imageboard, String[] args, String content, GuildMessageReceivedEvent event) {
Rating rating = Rating.SAFE;
boolean needRating = args.length >= 3;
final TextChannel channel = event.getChannel();
final Player player = MantaroData.db().getPlayer(event.getAuthor());
final PlayerData playerData = player.getData();
if (needRating && !nsfwOnly)
rating = Rating.lookupFromString(args[2]);
if (nsfwOnly)
rating = Rating.EXPLICIT;
if (rating == null) {
channel.sendMessage(EmoteReference.ERROR + "You provided an invalid rating (Available types: questionable, explicit, safe)!").queue();
return;
}
final Rating finalRating = rating;
if (!nsfwCheck(event, nsfwOnly, false, finalRating)) {
channel.sendMessage(EmoteReference.ERROR + "Cannot send a NSFW image in a non-nsfw channel :(").queue();
return;
}
int page = Math.max(1, r.nextInt(25));
String queryRating = nsfwOnly ? null : rating.getLongName();
switch(type) {
case GET:
try {
String arguments = content.replace("get ", "");
String[] argumentsSplit = arguments.split(" ");
api.get(page, queryRating).async(requestedImages -> {
if (isListNull(requestedImages, event))
return;
try {
int number;
List<BoardImage> images = (List<BoardImage>) requestedImages;
if (!nsfwOnly)
images = requestedImages.stream().filter(data -> data.getRating().equals(finalRating)).collect(Collectors.toList());
if (images.isEmpty()) {
channel.sendMessage(EmoteReference.SAD + "There are no images matching your search criteria...").queue();
return;
}
try {
number = Integer.parseInt(argumentsSplit[0]);
} catch (Exception e) {
number = r.nextInt(images.size());
}
BoardImage image = images.get(number);
String tags = image.getTags().stream().collect(Collectors.joining(", "));
if (foundMinorTags(event, tags, image.getRating())) {
return;
}
imageEmbed(image.getURL(), String.valueOf(image.getWidth()), String.valueOf(image.getHeight()), tags, image.getRating(), imageboard, channel);
if (image.getRating().equals(Rating.EXPLICIT)) {
if (playerData.addBadgeIfAbsent(Badge.LEWDIE)) {
player.saveAsync();
}
TextChannelGround.of(event).dropItemWithChance(13, 3);
}
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "**There aren't any more images or no results found**! Please try with a lower " + "number or another search.").queue();
}
}, failure -> event.getChannel().sendMessage(EmoteReference.SAD + "There was an error while looking for an image...").queue());
} catch (NumberFormatException ne) {
channel.sendMessage(EmoteReference.ERROR + "Wrong argument type. Check ~>help " + imageboard).queue(message -> message.delete().queueAfter(10, TimeUnit.SECONDS));
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.SAD + "There was an error while looking an image...").queue();
}
break;
case TAGS:
try {
String sNoArgs = content.replace("tags ", "");
String[] arguments = sNoArgs.split(" ");
String tags = arguments[0];
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
if (dbGuild.getData().getBlackListedImageTags().contains(tags.toLowerCase())) {
event.getChannel().sendMessage(EmoteReference.ERROR + "This image tag has been blacklisted here by an administrator.").queue();
return;
}
api.search(tags, queryRating).async(requestedImages -> {
// account for this
if (isListNull(requestedImages, event))
return;
try {
List<BoardImage> filter = (List<BoardImage>) requestedImages;
if (!nsfwOnly)
filter = requestedImages.stream().filter(data -> data.getRating().equals(finalRating)).collect(Collectors.toList());
if (filter.isEmpty()) {
channel.sendMessage(EmoteReference.SAD + "There are no images matching your search criteria...").queue();
return;
}
int number;
try {
number = Integer.parseInt(arguments[1]);
} catch (Exception e) {
number = r.nextInt(filter.size() > 0 ? filter.size() - 1 : filter.size());
}
BoardImage image = filter.get(number);
String imageTags = image.getTags().stream().collect(Collectors.joining(", "));
if (foundMinorTags(event, imageTags, image.getRating())) {
return;
}
imageEmbed(image.getURL(), String.valueOf(image.getWidth()), String.valueOf(image.getHeight()), imageTags, image.getRating(), imageboard, channel);
if (image.getRating().equals(Rating.EXPLICIT)) {
if (playerData.addBadgeIfAbsent(Badge.LEWDIE)) {
player.saveAsync();
}
TextChannelGround.of(event).dropItemWithChance(13, 3);
}
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "**There aren't any more images or no results found**! Please try with a lower " + "number or another search.").queue();
}
}, failure -> event.getChannel().sendMessage(EmoteReference.SAD + "There was an error while looking for this tag...").queue());
} catch (NumberFormatException numberEx) {
channel.sendMessage(EmoteReference.ERROR + "Wrong argument type. Check ~>help " + imageboard).queue(message -> message.delete().queueAfter(10, TimeUnit.SECONDS));
} catch (Exception exception) {
event.getChannel().sendMessage(EmoteReference.SAD + "There was an error while looking for this tag...").queue();
}
break;
case RANDOM:
api.get(page, queryRating).async(requestedImages -> {
try {
if (isListNull(requestedImages, event))
return;
List<BoardImage> filter = (List<BoardImage>) requestedImages;
if (!nsfwOnly)
filter = requestedImages.stream().filter(data -> data.getRating().equals(finalRating)).collect(Collectors.toList());
if (filter.isEmpty()) {
channel.sendMessage(EmoteReference.SAD + "There are no images matching your search criteria...").queue();
return;
}
int number = r.nextInt(filter.size());
BoardImage image = filter.get(number);
String tags = image.getTags().stream().collect(Collectors.joining(", "));
imageEmbed(image.getURL(), String.valueOf(image.getWidth()), String.valueOf(image.getHeight()), tags, image.getRating(), imageboard, channel);
if (image.getRating().equals(Rating.EXPLICIT)) {
if (playerData.addBadgeIfAbsent(Badge.LEWDIE)) {
player.saveAsync();
}
TextChannelGround.of(event).dropItemWithChance(13, 3);
}
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.SAD + "There was an unknown error while looking for a random image...").queue();
}
}, failure -> event.getChannel().sendMessage(EmoteReference.SAD + "There was an error while looking for a random image...").queue());
break;
}
}
Aggregations