Search in sources :

Example 1 with FreeGame

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

the class FreeGameManager method sendFreeGameNotifications.

public CompletableFuture<List<ReadonlyMessage>> sendFreeGameNotifications(List<? extends FreeGame<?>> games) {
    games.forEach(this::addAnnouncedGame);
    List<Document> gameData = games.stream().map(FreeGame::toData).collect(Collectors.toList());
    if (!gameData.isEmpty()) {
        this.bot.getMongo().insertManyAnnouncedGames(gameData).whenComplete(MongoDatabase.exceptionally());
    }
    List<Bson> guildPipeline = List.of(Aggregates.match(Operators.expr(Operators.eq("$_id", "$$guildId"))), Aggregates.project(Projections.computed("premium", Operators.lt(Operators.nowEpochSecond(), Operators.ifNull("$premium.endAt", 0L)))));
    List<Bson> pipeline = List.of(Aggregates.lookup("guilds", List.of(new Variable<>("guildId", "$guildId")), guildPipeline, "premium"), Aggregates.addFields(new Field<>("premium", Operators.cond(Operators.isEmpty("$premium"), false, Operators.get(Operators.arrayElemAt("$premium", 0), "premium")))));
    return this.bot.getMongo().aggregateFreeGameChannels(pipeline).thenComposeAsync(documents -> {
        List<WriteModel<Document>> bulkData = new ArrayList<>();
        List<CompletableFuture<List<ReadonlyMessage>>> futures = new ArrayList<>();
        for (Document data : documents) {
            if (!data.getBoolean("enabled", true)) {
                continue;
            }
            TextChannel channel = this.bot.getShardManager().getTextChannelById(data.getLong("channelId"));
            if (channel == null) {
                continue;
            }
            String avatar = channel.getJDA().getSelfUser().getEffectiveAvatarUrl();
            boolean premium = data.getBoolean("premium");
            Document webhookData = data.get("webhook", MongoDatabase.EMPTY_DOCUMENT);
            long platforms = data.get("platforms", FreeGameType.ALL);
            List<WebhookMessage> messages = new ArrayList<>();
            for (FreeGame<?> game : games) {
                long raw = game.getType().getRaw();
                if ((platforms & raw) != raw) {
                    continue;
                }
                Formatter<Document> formatter = new JsonFormatter(data.get("message", FreeGameManager.DEFAULT_MESSAGE)).addVariable("game", game);
                WebhookMessage message;
                try {
                    message = MessageUtility.fromJson(formatter.parse()).setAvatarUrl(premium ? webhookData.get("avatar", avatar) : avatar).setUsername(premium ? webhookData.get("name", "Sx4 - Free Games") : "Sx4 - Free Games").build();
                } catch (IllegalArgumentException e) {
                    bulkData.add(new UpdateOneModel<>(Filters.eq("_id", data.getObjectId("_id")), Updates.unset("message")));
                    continue;
                }
                messages.add(message);
            }
            if (!messages.isEmpty()) {
                futures.add(this.sendFreeGameNotificationMessages(channel, webhookData, messages));
            }
        }
        if (!bulkData.isEmpty()) {
            this.bot.getMongo().bulkWriteFreeGameChannels(bulkData).whenComplete(MongoDatabase.exceptionally());
        }
        return FutureUtility.allOf(futures).thenApply(list -> list.stream().flatMap(List::stream).collect(Collectors.toList()));
    });
}
Also used : Document(org.bson.Document) Bson(org.bson.conversions.Bson) WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) JsonFormatter(com.sx4.bot.formatter.JsonFormatter) TextChannel(net.dv8tion.jda.api.entities.TextChannel) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage)

Example 2 with FreeGame

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

the class FreeGamesCommand method list.

@Command(value = "list", description = "Lists the current free games")
@CommandId(473)
@Examples({ "free games list" })
public void list(Sx4CommandEvent event) {
    List<Document> games = event.getMongo().getAnnouncedGames(Filters.gte("promotion.end", Instant.now().getEpochSecond()), new Document()).into(new ArrayList<>());
    if (games.isEmpty()) {
        event.replyFailure("There are currently no free games").queue();
        return;
    }
    List<FreeGame<?>> freeGames = games.stream().map(FreeGameUtility::getFreeGame).collect(Collectors.toList());
    PagedResult<FreeGame<?>> paged = new PagedResult<>(event.getBot(), freeGames).setSelect().setPerPage(1).setCustomFunction(page -> {
        EmbedBuilder embed = new EmbedBuilder();
        embed.setFooter("Game " + page.getPage() + "/" + page.getMaxPage());
        page.forEach((game, index) -> this.setGameEmbed(embed, game));
        return new MessageBuilder().setEmbeds(embed.build());
    });
    paged.execute(event);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Document(org.bson.Document) EpicFreeGame(com.sx4.bot.entities.info.EpicFreeGame) FreeGame(com.sx4.bot.entities.info.FreeGame) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Aggregations

Document (org.bson.Document)2 WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)1 Command (com.jockie.bot.core.command.Command)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 EpicFreeGame (com.sx4.bot.entities.info.EpicFreeGame)1 FreeGame (com.sx4.bot.entities.info.FreeGame)1 ReadonlyMessage (com.sx4.bot.entities.webhook.ReadonlyMessage)1 JsonFormatter (com.sx4.bot.formatter.JsonFormatter)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 Bson (org.bson.conversions.Bson)1