Search in sources :

Example 56 with PagedResult

use of com.sx4.bot.paged.PagedResult in project Sx4 by sx4-discord-bot.

the class UrbanDictionaryCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "query", endless = true) String query) {
    Request request = new Request.Builder().url("http://api.urbandictionary.com/v0/define?term=" + URLEncoder.encode(query, StandardCharsets.UTF_8)).build();
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        Document json = Document.parse(response.body().string());
        List<Document> list = json.getList("list", Document.class, Collections.emptyList());
        if (list.isEmpty()) {
            event.replyFailure("I could not find any results for that query").queue();
            return;
        }
        PagedResult<Document> paged = new PagedResult<>(event.getBot(), list).setPerPage(1).setCustomFunction(page -> {
            EmbedBuilder embed = new EmbedBuilder();
            embed.setFooter(PagedResult.DEFAULT_FOOTER_TEXT, null);
            embed.setTitle("Page " + page.getPage() + "/" + page.getMaxPage());
            page.forEach((data, index) -> {
                String readMore = "... [Read more](" + data.getString("permalink") + ")";
                String definition = this.getUrbanText(data.getString("definition"), readMore);
                String example = this.getUrbanText(data.getString("example"), readMore);
                embed.setAuthor(data.getString("word"), data.getString("permalink"), null);
                embed.addField("Definition", definition, false);
                if (!example.isEmpty()) {
                    embed.addField("Example", example, false);
                }
                embed.addField("Upvotes", data.getInteger("thumbs_up") + " 👍", true);
                embed.addField("Downvotes", data.getInteger("thumbs_down") + " 👎", true);
            });
            return new MessageBuilder().setEmbeds(embed.build());
        });
        paged.execute(event);
    });
}
Also used : Document(org.bson.Document) Request(okhttp3.Request) HttpCallback(com.sx4.bot.http.HttpCallback) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) PagedResult(com.sx4.bot.paged.PagedResult) StandardCharsets(java.nio.charset.StandardCharsets) ModuleCategory(com.sx4.bot.category.ModuleCategory) URLEncoder(java.net.URLEncoder) List(java.util.List) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Collections(java.util.Collections) Argument(com.jockie.bot.core.argument.Argument) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Request(okhttp3.Request) List(java.util.List) Document(org.bson.Document) PagedResult(com.sx4.bot.paged.PagedResult)

Example 57 with PagedResult

use of com.sx4.bot.paged.PagedResult in project Sx4 by sx4-discord-bot.

the class WarnCommand method list.

@Command(value = "list", description = "Lists all the warned users in the server and how many warnings they have")
@CommandId(258)
@Examples({ "warn list" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void list(Sx4CommandEvent event) {
    List<Bson> pipeline = List.of(Aggregates.match(Filters.eq("guildId", event.getGuild().getIdLong())), Aggregates.project(Projections.fields(Projections.include("userId"), Projections.computed("warnings", Operators.cond(Operators.or(Operators.isNull("$reset"), Operators.isNull("$warnings")), Operators.ifNull("$warnings", 0), Operators.max(0, Operators.subtract("$warnings", Operators.multiply(Operators.toInt(Operators.floor(Operators.divide(Operators.subtract(Operators.nowEpochSecond(), "$lastWarning"), "$reset.after"))), "$reset.amount"))))))), Aggregates.match(Filters.ne("warnings", 0)), Aggregates.sort(Sorts.descending("warnings")));
    event.getMongo().aggregateWarnings(pipeline).whenComplete((users, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (users.isEmpty()) {
            event.replyFailure("There are no users with warnings in this server").queue();
            return;
        }
        PagedResult<Document> paged = new PagedResult<>(event.getBot(), users).setAuthor("Warned Users", null, event.getGuild().getIconUrl()).setIndexed(false).setDisplayFunction(data -> {
            long userId = data.getLong("userId");
            User user = event.getShardManager().getUserById(userId);
            return "`" + (user == null ? "Anonymous#0000 (" + userId + ")" : MarkdownSanitizer.escape(user.getAsTag())) + "` - Warning **#" + data.getInteger("warnings") + "**";
        });
        paged.execute(event);
    });
}
Also used : User(net.dv8tion.jda.api.entities.User) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 58 with PagedResult

use of com.sx4.bot.paged.PagedResult in project Sx4 by sx4-discord-bot.

the class RedditCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "subreddit") String subreddit, @Option(value = "sort", description = "What to sort the subreddit by") @Options({ "new", "hot", "top" }) @DefaultString("hot") @Lowercase String sort, @Option(value = "time", description = "If sort is top will choose the top posts after the set amount of time") @Options({ "day", "week", "month", "year", "all" }) @DefaultString("day") @Lowercase String time, @Option(value = "limit", description = "How many posts to show") @DefaultNumber(25) @Limit(min = 1, max = 100) int limit) {
    Request request = new Request.Builder().url("https://reddit.com/r/" + subreddit + "/" + sort + ".json?t=" + time + "&limit=" + limit).build();
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        if (response.code() == 302 || response.code() == 404) {
            event.replyFailure("I could not find that subreddit").queue();
            return;
        }
        List<Document> posts = Document.parse(response.body().string()).getEmbedded(List.of("data", "children"), Collections.emptyList());
        posts = posts.stream().filter(post -> {
            if (!event.getTextChannel().isNSFW()) {
                return !post.getEmbedded(List.of("data", "over_18"), false);
            }
            return true;
        }).map(post -> post.get("data", Document.class)).collect(Collectors.toList());
        if (posts.isEmpty()) {
            event.replyFailure("I could not find any posts with those filters").queue();
            return;
        }
        PagedResult<Document> paged = new PagedResult<>(event.getBot(), posts).setPerPage(1).setSelect().setCustomFunction(page -> {
            MessageBuilder builder = new MessageBuilder();
            EmbedBuilder embed = new EmbedBuilder().setTitle("Page " + page.getPage() + "/" + page.getMaxPage());
            page.forEach((data, index) -> {
                String url = "https://reddit.com" + data.getString("permalink");
                embed.setFooter(data.getString("subreddit_name_prefixed") + " | Upvotes: " + data.getInteger("ups"));
                embed.setAuthor(StringUtility.limit(data.getString("title"), 50, "..."), url, "http://i.imgur.com/sdO8tAw.png");
                String postHint = data.getString("post_hint");
                if (postHint != null && postHint.equals("image")) {
                    embed.setImage(data.getString("url"));
                } else if (postHint != null && postHint.equals("hosted:video")) {
                    embed.setImage(data.getString("thumbnail"));
                    builder.setContent("<" + data.getEmbedded(List.of("media", "reddit_video", "fallback_url"), String.class) + ">");
                } else if (postHint != null && postHint.equals("rich:video")) {
                    embed.setImage(data.getEmbedded(List.of("media", "oembed", "thumbnail"), String.class));
                    builder.setContent("<" + data.getEmbedded(List.of("secure_media_embed", "media_domain_url"), String.class) + ">");
                }
                if (data.containsKey("selftext")) {
                    embed.setDescription(StringUtility.limit(data.getString("selftext"), MessageEmbed.DESCRIPTION_MAX_LENGTH, "... [Read more](" + url + ")"));
                }
            });
            return builder.setEmbeds(embed.build());
        });
        paged.execute(event);
    });
}
Also used : Document(org.bson.Document) Request(okhttp3.Request) HttpCallback(com.sx4.bot.http.HttpCallback) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) PagedResult(com.sx4.bot.paged.PagedResult) Collectors(java.util.stream.Collectors) com.sx4.bot.annotations.argument(com.sx4.bot.annotations.argument) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Option(com.jockie.bot.core.option.Option) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) StringUtility(com.sx4.bot.utility.StringUtility) Collections(java.util.Collections) Argument(com.jockie.bot.core.argument.Argument) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Request(okhttp3.Request) List(java.util.List) PagedResult(com.sx4.bot.paged.PagedResult)

Example 59 with PagedResult

use of com.sx4.bot.paged.PagedResult in project Sx4 by sx4-discord-bot.

the class PrivacyPolicyCommand method onCommand.

public void onCommand(Sx4CommandEvent event) {
    PagedResult<Document> paged = new PagedResult<>(event.getBot(), event.getConfig().getPolicies()).setPerPage(1).setCustomFunction(page -> {
        MessageBuilder builder = new MessageBuilder();
        EmbedBuilder embed = new EmbedBuilder();
        embed.setFooter(PagedResult.DEFAULT_FOOTER_TEXT, null);
        page.forEach((policy, index) -> {
            embed.setTitle("(" + page.getPage() + "/" + page.getMaxPage() + ") " + policy.getString("title"));
            embed.setDescription(policy.getString("description"));
        });
        return builder.setEmbeds(embed.build());
    });
    paged.execute(event);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Document(org.bson.Document) PagedResult(com.sx4.bot.paged.PagedResult)

Aggregations

Sx4Command (com.sx4.bot.core.Sx4Command)54 PagedResult (com.sx4.bot.paged.PagedResult)50 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)43 Document (org.bson.Document)43 Command (com.jockie.bot.core.command.Command)41 CommandId (com.sx4.bot.annotations.command.CommandId)35 Examples (com.sx4.bot.annotations.command.Examples)33 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)32 ModuleCategory (com.sx4.bot.category.ModuleCategory)26 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)26 User (net.dv8tion.jda.api.entities.User)26 Argument (com.jockie.bot.core.argument.Argument)25 Bson (org.bson.conversions.Bson)23 Permission (net.dv8tion.jda.api.Permission)22 BotPermissions (com.sx4.bot.annotations.command.BotPermissions)20 List (java.util.List)19 ArrayList (java.util.ArrayList)18 HttpCallback (com.sx4.bot.http.HttpCallback)15 Request (okhttp3.Request)15 Collectors (java.util.stream.Collectors)13