Search in sources :

Example 1 with Rule34

use of net.kodehawa.lib.imageboards.rule34.Rule34 in project MantaroBot by Mantaro.

the class ImageCmds method rule34.

@Command
public static void rule34(CommandRegistry cr) {
    cr.register("rule34", new SimpleCommand(Category.IMAGE) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (!nsfwCheck(event, true, true))
                return;
            String noArgs = content.split(" ")[0];
            TextChannelGround.of(event).dropItemWithChance(13, 3);
            switch(noArgs) {
                case "get":
                    try {
                        String whole1 = content.replace("get ", "");
                        String[] wholeBeheaded = whole1.split(" ");
                        Rule34.get(60, image -> {
                            try {
                                int number;
                                try {
                                    number = Integer.parseInt(wholeBeheaded[0]);
                                } catch (Exception e) {
                                    number = r.nextInt(image.size());
                                }
                                String TAGS = image.get(number).getTags().replace(" ", " ,");
                                EmbedBuilder builder = new EmbedBuilder();
                                builder.setAuthor("Found image", null, "http:" + image.get(number - 1).getFile_url()).setImage("http:" + image.get(number - 1).getFile_url()).addField("Width", String.valueOf(image.get(number - 1).getWidth()), true).addField("Height", String.valueOf(image.get(number - 1).getHeight()), true).addField("Tags", "``" + (TAGS == null ? "None" : TAGS) + "``", false).setFooter("If the image doesn't load, click the title.", null);
                                event.getChannel().sendMessage(builder.build()).queue();
                            } catch (ArrayIndexOutOfBoundsException e) {
                                event.getChannel().sendMessage(EmoteReference.ERROR + "**There aren't more images or no results found**! Try with a lower number.").queue();
                            }
                        });
                    } catch (Exception exception) {
                        if (exception instanceof NumberFormatException)
                            event.getChannel().sendMessage(EmoteReference.ERROR + "Wrong argument type. Check ~>help rule34").queue(message -> message.delete().queueAfter(10, TimeUnit.SECONDS));
                    }
                    break;
                case "tags":
                    try {
                        try {
                            boolean isOldFormat = args[1].matches("^[0-9]*$");
                            if (isOldFormat) {
                                event.getChannel().sendMessage(EmoteReference.WARNING + "Now you don't need to specify the page number. Please use ~>rule34 tags <tag>").queue();
                                return;
                            }
                            String sNoArgs = content.replace("tags ", "");
                            String[] expectedNumber = sNoArgs.split(" ");
                            String tags = expectedNumber[0];
                            Rule34.onSearch(60, tags, images -> {
                                try {
                                    try {
                                        number1 = Integer.parseInt(expectedNumber[2]);
                                    } catch (Exception e) {
                                        number1 = r.nextInt(images.size() > 0 ? images.size() - 1 : images.size());
                                    }
                                    String TAGS = images.get(number).getTags() == null ? tags : images.get(number).getTags().replace(" ", " ,");
                                    EmbedBuilder builder = new EmbedBuilder();
                                    builder.setAuthor("Found image", null, "http:" + images.get(number1 - 1).getFile_url()).setImage("http:" + images.get(number1 - 1).getFile_url()).addField("Width", String.valueOf(images.get(number1 - 1).getWidth()), true).addField("Height", String.valueOf(images.get(number1 - 1).getHeight()), true).addField("Tags", "``" + (TAGS == null ? "None" : TAGS) + "``", false).setFooter("If the image doesn't load, click the title.", null);
                                    event.getChannel().sendMessage(builder.build()).queue();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    event.getChannel().sendMessage(EmoteReference.ERROR + "**There aren't more images or no results found**! Try with a lower number.").queue();
                                }
                            });
                        } catch (Exception exception) {
                            if (exception instanceof NumberFormatException)
                                event.getChannel().sendMessage(EmoteReference.ERROR + "Wrong argument type. Check ~>help rule34").queue(message -> message.delete().queueAfter(10, TimeUnit.SECONDS));
                        }
                    } catch (NullPointerException e) {
                        event.getChannel().sendMessage(EmoteReference.ERROR + "Rule34 decided to not fetch the image. Well, you can try with another number or tag.").queue();
                    }
                    break;
                default:
                    onHelp(event);
                    break;
            }
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "rule34.xxx commmand").setColor(Color.PINK).setDescription("**Retrieves images from the rule34 (hentai) image board.**").addField("Usage", "`~>rule34 get <imagenumber>` - **Gets an image based in parameters.**\n" + "`~>rule34 tags <tag> <imagenumber>` - **Gets an image based in the specified tag and parameters.**\n", false).addField("Parameters", "`page` - **Can be any value from 1 to the rule34 maximum page. Probably around 4000.**\n" + "`imagenumber` - **(OPTIONAL) Any number from 1 to the maximum possible images to get, specified by the first instance of the command.**\n" + "`tag` - **Any valid image tag. For example animal_ears or original.**", false).build();
        }
    });
}
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) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

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