Search in sources :

Example 1 with RedditAPIDto

use of toby.dto.web.RedditAPIDto in project toby-bot by ml404.

the class MemeCommand method handle.

@Override
public void handle(CommandContext ctx, String prefix, UserDto requestingUserDto, Integer deleteDelay) {
    ICommand.deleteAfter(ctx.getMessage(), deleteDelay);
    Gson gson = new Gson();
    final TextChannel channel = ctx.getChannel();
    List<String> args = ctx.getArgs();
    User member = ctx.getAuthor();
    if (!requestingUserDto.hasMemePermission()) {
        channel.sendMessageFormat("You do not have adequate permissions to use this command, talk to the server owner: %s", ctx.getGuild().getOwner().getNickname()).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        return;
    }
    if (args.size() == 0) {
        channel.sendMessage(getHelp(prefix)).queue();
    } else {
        String subredditArg = args.get(0);
        String timePeriod;
        int limit;
        try {
            timePeriod = RedditAPIDto.TimePeriod.valueOf(args.get(1).toUpperCase()).toString().toLowerCase();
        } catch (IndexOutOfBoundsException e) {
            timePeriod = "day";
        } catch (IllegalArgumentException e) {
            timePeriod = "day";
            channel.sendMessage(String.format("You entered a time period not supported: **%s**\\. Please use one of: %s \n", args.get(1), Arrays.stream(RedditAPIDto.TimePeriod.values()).map(Enum::name).map(String::toLowerCase).collect(Collectors.joining("/"))) + String.format("Using default time period of %s", timePeriod)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        }
        try {
            limit = Integer.parseInt(args.get(2));
        } catch (IndexOutOfBoundsException e) {
            limit = 5;
        } catch (NumberFormatException e) {
            limit = 5;
            channel.sendMessage(String.format("Invalid number supplied, using default value %d", limit)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        }
        if (subredditArg.equals("sneakybackgroundfeet")) {
            channel.sendMessage("Don't talk to me.").queue(message -> ICommand.deleteAfter(message, deleteDelay));
        } else {
            WebUtils.ins.getJSONObject(String.format(RedditAPIDto.redditPrefix, subredditArg, limit, timePeriod)).async((json) -> {
                if ((json.get("data").get("dist").asInt() == 0)) {
                    channel.sendMessage(String.format("I think you typo'd the subreddit: '%s', I couldn't get anything from the reddit API", subredditArg)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
                    return;
                }
                final JsonNode parentData = json.get("data");
                final JsonNode children = parentData.get("children");
                Random random = new Random();
                JsonNode meme = children.get(random.nextInt(children.size()));
                RedditAPIDto redditAPIDto = gson.fromJson(meme.get("data").toString(), RedditAPIDto.class);
                if (redditAPIDto.isNsfw()) {
                    channel.sendMessage(String.format("I received a NSFW subreddit from %s, or reddit gave me a NSFW meme, either way somebody shoot that guy", member)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
                } else if (redditAPIDto.getVideo()) {
                    channel.sendMessage("I pulled back a video, whoops. Try again maybe? Or not, up to you.").queue(message -> ICommand.deleteAfter(message, deleteDelay));
                } else {
                    String title = redditAPIDto.getTitle();
                    String url = redditAPIDto.getUrl();
                    String image = redditAPIDto.getImage();
                    EmbedBuilder embed = EmbedUtils.embedImageWithTitle(title, String.format(RedditAPIDto.commentsPrefix, url), image);
                    embed.addField("Subreddit", subredditArg, false);
                    channel.sendMessage(embed.build()).queue();
                }
            });
        }
    }
}
Also used : CommandContext(toby.command.CommandContext) EmbedUtils(me.duncte123.botcommons.messaging.EmbedUtils) Arrays(java.util.Arrays) UserDto(toby.jpa.dto.UserDto) Random(java.util.Random) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Collectors(java.util.stream.Collectors) User(net.dv8tion.jda.api.entities.User) List(java.util.List) Gson(com.google.gson.Gson) ICommand(toby.command.ICommand) JsonNode(com.fasterxml.jackson.databind.JsonNode) RedditAPIDto(toby.dto.web.RedditAPIDto) WebUtils(me.duncte123.botcommons.web.WebUtils) User(net.dv8tion.jda.api.entities.User) Gson(com.google.gson.Gson) JsonNode(com.fasterxml.jackson.databind.JsonNode) RedditAPIDto(toby.dto.web.RedditAPIDto) TextChannel(net.dv8tion.jda.api.entities.TextChannel) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Random(java.util.Random)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Gson (com.google.gson.Gson)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Random (java.util.Random)1 Collectors (java.util.stream.Collectors)1 EmbedUtils (me.duncte123.botcommons.messaging.EmbedUtils)1 WebUtils (me.duncte123.botcommons.web.WebUtils)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 User (net.dv8tion.jda.api.entities.User)1 CommandContext (toby.command.CommandContext)1 ICommand (toby.command.ICommand)1 RedditAPIDto (toby.dto.web.RedditAPIDto)1 UserDto (toby.jpa.dto.UserDto)1