Search in sources :

Example 1 with ImageBoard

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;
    }
}
Also used : Badge(net.kodehawa.mantarobot.commands.currency.profile.Badge) Player(net.kodehawa.mantarobot.db.entities.Player) PlayerData(net.kodehawa.mantarobot.db.entities.helpers.PlayerData) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Random(java.util.Random) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) Collectors(java.util.stream.Collectors) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) ImageBoard(net.kodehawa.lib.imageboards.ImageBoard) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) List(java.util.List) Rating(net.kodehawa.lib.imageboards.entities.Rating) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) BoardImage(net.kodehawa.lib.imageboards.entities.BoardImage) MantaroData(net.kodehawa.mantarobot.data.MantaroData) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) Player(net.kodehawa.mantarobot.db.entities.Player) BoardImage(net.kodehawa.lib.imageboards.entities.BoardImage) Rating(net.kodehawa.lib.imageboards.entities.Rating) TextChannel(net.dv8tion.jda.core.entities.TextChannel) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) List(java.util.List) PlayerData(net.kodehawa.mantarobot.db.entities.helpers.PlayerData)

Aggregations

List (java.util.List)1 Random (java.util.Random)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)1 ImageBoard (net.kodehawa.lib.imageboards.ImageBoard)1 BoardImage (net.kodehawa.lib.imageboards.entities.BoardImage)1 Rating (net.kodehawa.lib.imageboards.entities.Rating)1 TextChannelGround (net.kodehawa.mantarobot.commands.currency.TextChannelGround)1 Badge (net.kodehawa.mantarobot.commands.currency.profile.Badge)1 MantaroData (net.kodehawa.mantarobot.data.MantaroData)1 DBGuild (net.kodehawa.mantarobot.db.entities.DBGuild)1 Player (net.kodehawa.mantarobot.db.entities.Player)1 PlayerData (net.kodehawa.mantarobot.db.entities.helpers.PlayerData)1 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)1