Search in sources :

Example 1 with IGDBSort

use of com.sx4.bot.entities.info.IGDBSort in project Sx4 by sx4-discord-bot.

the class IGDBCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "game", endless = true, nullDefault = true) String game, @Option(value = "sort", description = "Sort results by `name` (default), `rating` and `release`") IGDBSort sort, @Option(value = "reverse", description = "Reverses sorting order") boolean reverse) {
    IGDBParser parser = new IGDBParser().setFilter("category = 0").limit(500).addFields("name", "total_rating", "total_rating_count", "first_release_date", "genres.name", "url", "summary", "cover.image_id", "platforms.name");
    if (game != null && sort == null && !reverse) {
        parser.search(String.format("\"%s\"", game));
    } else {
        parser.sort(sort == null ? "name" : sort.getName(), !reverse).appendFilter(filter -> IGDBFilter.and(filter, (sort == null ? "name" : sort.getName()) + " != n"));
        if (game != null) {
            parser.appendFilter(filter -> IGDBFilter.and(filter, String.format("name ~ \"%s\"*", game)));
        }
    }
    Request request = new Request.Builder().url("https://api.igdb.com/v4/games/").post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), parser.parse())).addHeader("Client-ID", event.getConfig().getTwitchClientId()).addHeader("Authorization", "Bearer " + event.getConfig().getTwitch()).build();
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        String body = String.format("{\"data\":%s}", response.body().string());
        List<Document> results = Document.parse(body).getList("data", Document.class);
        if (results.isEmpty()) {
            event.replyFailure("I could not find any games with that filter").queue();
            return;
        }
        PagedResult<Document> paged = new PagedResult<>(event.getBot(), results).setAutoSelect(true).setIncreasedIndex(true).setAuthor("IGDB Search", null, "http://bit.ly/2NXGwMz").setDisplayFunction(data -> data.getString("name"));
        paged.onSelect(select -> {
            Document data = select.getSelected();
            EmbedBuilder embed = new EmbedBuilder();
            embed.setAuthor(data.getString("name"), data.getString("url"), "http://bit.ly/2NXGwMz");
            embed.setDescription(StringUtility.limit(data.get("summary", "This game has no description :("), MessageEmbed.DESCRIPTION_MAX_LENGTH, "... [Read More](" + data.getString("url") + ")"));
            embed.setThumbnail(data.containsKey("cover") ? String.format("https://images.igdb.com/igdb/image/upload/t_thumb/%s.jpg", data.getEmbedded(List.of("cover", "image_id"), String.class)) : null);
            int ratings = data.get("total_rating_count", 0);
            embed.addField("Rating", data.containsKey("total_rating") ? String.format("%.2f out of %,d rating%s", data.getDouble("total_rating"), ratings, ratings == 1 ? "" : "s") : "Unknown", true);
            embed.addField("Release Date", data.containsKey("first_release_date") ? Instant.ofEpochSecond(data.getInteger("first_release_date")).atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("dd LLLL yyyy")) : "Unknown", true);
            embed.addField("Genres", data.containsKey("genres") ? data.getList("genres", Document.class).stream().map(genre -> genre.getString("name")).collect(Collectors.joining("\n")) : "None", true);
            embed.addField("Platforms", data.containsKey("platforms") ? data.getList("platforms", Document.class).stream().map(platform -> platform.getString("name")).collect(Collectors.joining("\n")) : "None", true);
            event.reply(embed.build()).queue();
        });
        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) IGDBFilter(com.sx4.bot.entities.info.IGDBFilter) IGDBSort(com.sx4.bot.entities.info.IGDBSort) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Instant(java.time.Instant) PagedResult(com.sx4.bot.paged.PagedResult) Collectors(java.util.stream.Collectors) RequestBody(okhttp3.RequestBody) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) IGDBParser(com.sx4.bot.entities.info.IGDBParser) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) DateTimeFormatter(java.time.format.DateTimeFormatter) Option(com.jockie.bot.core.option.Option) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) ZoneOffset(java.time.ZoneOffset) StringUtility(com.sx4.bot.utility.StringUtility) MediaType(okhttp3.MediaType) Argument(com.jockie.bot.core.argument.Argument) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Request(okhttp3.Request) List(java.util.List) IGDBParser(com.sx4.bot.entities.info.IGDBParser) Document(org.bson.Document) PagedResult(com.sx4.bot.paged.PagedResult)

Aggregations

Argument (com.jockie.bot.core.argument.Argument)1 Option (com.jockie.bot.core.option.Option)1 ModuleCategory (com.sx4.bot.category.ModuleCategory)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)1 IGDBFilter (com.sx4.bot.entities.info.IGDBFilter)1 IGDBParser (com.sx4.bot.entities.info.IGDBParser)1 IGDBSort (com.sx4.bot.entities.info.IGDBSort)1 HttpCallback (com.sx4.bot.http.HttpCallback)1 PagedResult (com.sx4.bot.paged.PagedResult)1 StringUtility (com.sx4.bot.utility.StringUtility)1 Instant (java.time.Instant)1 ZoneOffset (java.time.ZoneOffset)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 Permission (net.dv8tion.jda.api.Permission)1 MessageEmbed (net.dv8tion.jda.api.entities.MessageEmbed)1 MediaType (okhttp3.MediaType)1