Search in sources :

Example 1 with YandereImageData

use of net.kodehawa.mantarobot.commands.image.YandereImageData in project MantaroBot by Mantaro.

the class ImageCmds method getImage.

private static EmbedBuilder getImage(int argsCount, String requestType, String url, String rating, String[] messageArray, GuildMessageReceivedEvent event) {
    EmbedBuilder builder = new EmbedBuilder();
    if (!nsfwCheck(event, false, false))
        return builder.setDescription("Cannot send a lewd image in a non-nsfw channel.");
    String json = Utils.wget(url, event);
    try {
        YandereImageData[] imageData = GsonDataManager.GSON_PRETTY.fromJson(json, YandereImageData[].class);
        List<YandereImageData> filter = new ArrayList<>(Arrays.asList(imageData)).stream().filter(data -> rating.equals(data.rating)).collect(Collectors.toList());
        int get;
        try {
            get = requestType.equals("tags") ? argsCount >= 4 ? number : r.nextInt(filter.size()) : argsCount <= 2 ? Integer.parseInt(messageArray[2]) : r.nextInt(filter.size());
        } catch (IndexOutOfBoundsException e) {
            get = r.nextInt(filter.size());
        } catch (IllegalArgumentException e) {
            if (e.getMessage().equals("bound must be positive"))
                return builder.setDescription("No results found.");
            else
                return builder.setDescription("Query not valid.");
        }
        String AUTHOR = filter.get(get).getAuthor();
        String tags = filter.get(get).getTags().stream().collect(Collectors.joining(", "));
        if (!smallRequest) {
            return builder.setAuthor("Found image", filter.get(get).getFile_url(), null).setDescription("Image uploaded by: " + (AUTHOR == null ? "not found" : AUTHOR) + ", with a rating of: **" + nRating.inverseBidiMap().get(filter.get(get).getRating()) + "**").setImage(filter.get(get).getFile_url()).addField("Height", String.valueOf(filter.get(get).getHeight()), true).addField("Width", String.valueOf(filter.get(get).getWidth()), true).addField("Tags", "``" + (tags == null ? "None" : tags) + "``", false).setFooter("If the image doesn't load, click the title.", null);
        }
        return builder.setAuthor("Found image", filter.get(get).getFile_url(), null).setDescription("Image uploaded by: " + (AUTHOR == null ? "not found" : AUTHOR) + ", with a rating of: **" + nRating.inverseBidiMap().get(filter.get(get).getRating()) + "**").setImage(filter.get(get).getFile_url()).addField("Width", String.valueOf(filter.get(get).getHeight()), true).addField("Height", String.valueOf(filter.get(get).getWidth()), true).addField("Tags", "``" + (tags == null ? "None" : tags) + "``", false).setFooter("If the image doesn't load, click the title.", null);
    } catch (Exception ex) {
        if (ex instanceof NullPointerException)
            return builder.setDescription(EmoteReference.ERROR + "Wrong syntax.");
        return builder.setDescription(EmoteReference.ERROR + "There are no images here, just dust.");
    }
}
Also used : SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Color(java.awt.Color) Arrays(java.util.Arrays) YandereImageData(net.kodehawa.mantarobot.commands.image.YandereImageData) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Utils(net.kodehawa.mantarobot.utils.Utils) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) Module(net.kodehawa.mantarobot.modules.Module) Wallpaper(net.kodehawa.lib.imageboards.konachan.main.entities.Wallpaper) Random(java.util.Random) ArrayList(java.util.ArrayList) Unirest(com.mashape.unirest.http.Unirest) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) OptsCmd.registerOption(net.kodehawa.mantarobot.commands.OptsCmd.registerOption) URLCache(net.kodehawa.mantarobot.utils.cache.URLCache) JSONObject(org.json.JSONObject) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) Command(net.kodehawa.mantarobot.modules.Command) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) BidiMap(org.apache.commons.collections4.BidiMap) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) PostLoadEvent(net.kodehawa.mantarobot.modules.events.PostLoadEvent) DualHashBidiMap(org.apache.commons.collections4.bidimap.DualHashBidiMap) Category(net.kodehawa.mantarobot.modules.commands.base.Category) GsonDataManager(net.kodehawa.mantarobot.utils.data.GsonDataManager) Collectors(java.util.stream.Collectors) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) URLEncoder(java.net.URLEncoder) List(java.util.List) CollectionUtils(br.com.brjdevs.java.utils.collections.CollectionUtils) Rule34(net.kodehawa.lib.imageboards.rule34.Rule34) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) net.kodehawa.lib.imageboards.e621.e621(net.kodehawa.lib.imageboards.e621.e621) Konachan(net.kodehawa.lib.imageboards.konachan.Konachan) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) YandereImageData(net.kodehawa.mantarobot.commands.image.YandereImageData) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

CollectionUtils (br.com.brjdevs.java.utils.collections.CollectionUtils)1 Unirest (com.mashape.unirest.http.Unirest)1 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)1 Color (java.awt.Color)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URLEncoder (java.net.URLEncoder)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 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 MessageBuilder (net.dv8tion.jda.core.MessageBuilder)1 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)1 net.kodehawa.lib.imageboards.e621.e621 (net.kodehawa.lib.imageboards.e621.e621)1 Konachan (net.kodehawa.lib.imageboards.konachan.Konachan)1 Wallpaper (net.kodehawa.lib.imageboards.konachan.main.entities.Wallpaper)1