Search in sources :

Example 71 with Examples

use of com.sx4.bot.annotations.command.Examples in project Sx4 by sx4-discord-bot.

the class PremiumCommand method perks.

@Command(value = "perks", description = "View the perks you get when you or a server has premium")
@CommandId(462)
@Examples({ "premium perks" })
public void perks(Sx4CommandEvent event) {
    List<String> userPerks = event.getConfig().getPremiumUserPerks(), serverPerks = event.getConfig().getPremiumServerPerks();
    EmbedBuilder embed = new EmbedBuilder().setAuthor("Premium Perks", null, event.getSelfUser().getEffectiveAvatarUrl()).addField("Personal Perks", "• " + String.join("\n• ", userPerks), false).addField("Server Perks", "• " + String.join("\n• ", serverPerks), false);
    event.reply(embed.build()).queue();
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 72 with Examples

use of com.sx4.bot.annotations.command.Examples in project Sx4 by sx4-discord-bot.

the class PremiumCommand method check.

@Command(value = "check", description = "Checks when the current premium in the server expires")
@CommandId(178)
@Examples({ "premium check" })
public void check(Sx4CommandEvent event) {
    long endAt = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("premium.endAt")).getEmbedded(List.of("premium", "endAt"), 0L);
    if (endAt == 0) {
        event.replyFailure("This server currently doesn't have premium, you can give it premium with credit <https://patreon.com/Sx4>").queue();
        return;
    }
    OffsetDateTime expire = OffsetDateTime.ofInstant(Instant.ofEpochSecond(endAt), ZoneOffset.UTC);
    if (expire.isBefore(OffsetDateTime.now(ZoneOffset.UTC))) {
        event.replyFailure("Premium for this server expired on the **" + String.format(expire.format(this.formatter), NumberUtility.getSuffix(expire.getDayOfMonth())) + "**, you can renew it with more credit <https://patreon.com/Sx4>").queue();
        return;
    }
    event.replyFormat("Premium for this server will expire on the **" + String.format(expire.format(this.formatter), NumberUtility.getSuffix(expire.getDayOfMonth())) + "**").queue();
}
Also used : OffsetDateTime(java.time.OffsetDateTime) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 73 with Examples

use of com.sx4.bot.annotations.command.Examples in project Sx4 by sx4-discord-bot.

the class PremiumCommand method leaderboard.

@Command(value = "leaderboard", aliases = { "lb" }, description = "Leaderboard for Sx4s biggest donors")
@CommandId(446)
@Redirects({ "lb premium", "leaderboard premium" })
@Examples({ "premium leaderboard" })
public void leaderboard(Sx4CommandEvent event, @Option(value = "server", aliases = { "guild" }, description = "Filters the results to only people in the current server") boolean guild) {
    List<Bson> pipeline = List.of(Aggregates.project(Projections.computed("total", "$premium.total")), Aggregates.match(Filters.and(Filters.exists("total"), Filters.ne("total", 0))), Aggregates.sort(Sorts.descending("total")));
    event.getMongoMain().aggregateUsers(pipeline).whenCompleteAsync((documents, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        List<Map.Entry<String, Integer>> users = new ArrayList<>();
        AtomicInteger userIndex = new AtomicInteger(-1);
        int i = 0;
        for (Document data : documents) {
            long id = data.getLong("_id");
            User user = event.getShardManager().getUserById(data.getLong("_id"));
            if ((user == null || !event.getGuild().isMember(user)) && guild) {
                continue;
            }
            i++;
            users.add(Map.entry(user == null ? "Anonymous#0000 (" + id + ")" : MarkdownSanitizer.escape(user.getAsTag()), data.getInteger("total")));
            if (user != null && user.getIdLong() == event.getAuthor().getIdLong()) {
                userIndex.set(i);
            }
        }
        if (users.isEmpty()) {
            event.replyFailure("There are no users which fit into this leaderboard").queue();
            return;
        }
        PagedResult<Map.Entry<String, Integer>> paged = new PagedResult<>(event.getBot(), users).setPerPage(10).setCustomFunction(page -> {
            int rank = userIndex.get();
            EmbedBuilder embed = new EmbedBuilder().setTitle("Donors Leaderboard").setFooter(event.getAuthor().getName() + "'s Rank: " + (rank == -1 ? "N/A" : NumberUtility.getSuffixed(rank)) + " | Page " + page.getPage() + "/" + page.getMaxPage(), event.getAuthor().getEffectiveAvatarUrl());
            page.forEach((entry, index) -> embed.appendDescription(String.format("%d. `%s` - $%,.2f\n", index + 1, entry.getKey(), entry.getValue() / 100D)));
            return new MessageBuilder().setEmbeds(embed.build());
        });
        paged.execute(event);
    });
}
Also used : User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) Document(org.bson.Document) Bson(org.bson.conversions.Bson) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PagedResult(com.sx4.bot.paged.PagedResult) Redirects(com.sx4.bot.annotations.command.Redirects) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 74 with Examples

use of com.sx4.bot.annotations.command.Examples in project Sx4 by sx4-discord-bot.

the class WhitelistCommand method reset.

@Command(value = "reset", description = "Reset the whitelist for a specific role/user in a channel")
@CommandId(186)
@Examples({ "whitelist reset #channel", "whitelist reset all" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void reset(Sx4CommandEvent event, @Argument(value = "channel | all", endless = true) @AlternativeOptions("all") Alternative<TextChannel> option) {
    List<Bson> update = List.of(Operators.set("holders", Operators.reduce(Operators.ifNull("$holders", Collections.EMPTY_LIST), Collections.EMPTY_LIST, Operators.concatArrays("$$value", Operators.cond(Operators.isEmpty(Operators.ifNull(Operators.first(Operators.map(List.of("$$this"), "$$holder.blacklisted", "holder")), Collections.EMPTY_LIST)), Collections.EMPTY_LIST, List.of(Operators.removeObject("$$this", "whitelisted")))))));
    if (option.isAlternative()) {
        event.getMongo().updateManyBlacklists(Filters.eq("guildId", event.getGuild().getIdLong()), update, new UpdateOptions()).whenComplete((result, exception) -> {
            if (ExceptionUtility.sendExceptionally(event, exception)) {
                return;
            }
            if (result.getModifiedCount() == 0) {
                event.replyFailure("Nothing was whitelisted in this server").queue();
                return;
            }
            event.replySuccess("Reset **" + result.getModifiedCount() + "** channels of their whitelist configurations").queue();
        });
    } else {
        TextChannel channel = option.getValue();
        event.getMongo().updateBlacklist(Filters.eq("channelId", channel.getIdLong()), update, new UpdateOptions()).whenComplete((result, exception) -> {
            if (ExceptionUtility.sendExceptionally(event, exception)) {
                return;
            }
            if (result.getModifiedCount() == 0) {
                event.replyFailure("Nothing was whitelisted in that channel").queue();
                return;
            }
            event.replySuccess("That channel no longer has any whitelists").queue();
        });
    }
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) Bson(org.bson.conversions.Bson) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 75 with Examples

use of com.sx4.bot.annotations.command.Examples in project Sx4 by sx4-discord-bot.

the class FactoryCommand method buy.

@Command(value = "buy", description = "Buy a factory with some materials")
@CommandId(396)
@Examples({ "factory buy 5 Shoe Factory", "factory buy Shoe Factory", "factory buy all" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void buy(Sx4CommandEvent event, @Argument(value = "factories", endless = true) @AlternativeOptions("all") Alternative<ItemStack<Factory>> option) {
    ItemStack<Factory> stack = option.getValue();
    event.getMongo().withTransaction(session -> {
        Bson userFilter = Filters.eq("userId", event.getAuthor().getIdLong()), filter;
        List<Factory> factories;
        if (stack == null) {
            filter = Filters.and(userFilter, Filters.eq("item.type", ItemType.MATERIAL.getId()));
            factories = event.getBot().getEconomyManager().getItems(Factory.class);
        } else {
            Factory factory = stack.getItem();
            filter = Filters.and(userFilter, Filters.eq("item.id", factory.getCost().getItem().getId()));
            factories = List.of(factory);
        }
        List<Document> materials = event.getMongo().getItems().find(session, filter).projection(Projections.include("amount", "item.id")).into(new ArrayList<>());
        List<ItemStack<Factory>> boughtFactories = new ArrayList<>();
        Factories: for (Factory factory : factories) {
            ItemStack<Material> cost = factory.getCost();
            Material costMaterial = cost.getItem();
            for (Document material : materials) {
                int id = material.getEmbedded(List.of("item", "id"), Integer.class);
                if (costMaterial.getId() == id) {
                    long buyableAmount = (long) Math.floor((double) material.getLong("amount") / cost.getAmount());
                    long amount = stack == null ? buyableAmount : stack.getAmount();
                    if (amount == 0 || amount > buyableAmount) {
                        continue Factories;
                    }
                    event.getMongo().getItems().updateOne(session, Filters.and(userFilter, Filters.eq("item.id", id)), Updates.inc("amount", -amount * cost.getAmount()));
                    List<Bson> update = List.of(Operators.set("item", factory.toData()), Operators.set("amount", Operators.add(Operators.ifNull("$amount", 0L), amount)));
                    event.getMongo().getItems().updateOne(session, Filters.and(userFilter, Filters.eq("item.id", factory.getId())), update, new UpdateOptions().upsert(true));
                    boughtFactories.add(new ItemStack<>(factory, amount));
                }
            }
        }
        return boughtFactories;
    }).whenComplete((factories, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (factories.isEmpty()) {
            event.replyFailure("You cannot afford " + (stack == null ? "any factories" : "`" + stack.getAmount() + " " + stack.getItem().getName() + "`")).queue();
            return;
        }
        String factoriesBought = factories.stream().sorted(Collections.reverseOrder(Comparator.comparingLong(ItemStack::getAmount))).map(ItemStack::toString).collect(Collectors.joining("\n• "));
        EmbedBuilder embed = new EmbedBuilder().setColor(event.getMember().getColor()).setAuthor(event.getAuthor().getName(), null, event.getAuthor().getEffectiveAvatarUrl()).setDescription("With all your materials you have bought the following factories\n\n• " + factoriesBought);
        event.reply(embed.build()).queue();
    });
}
Also used : Document(org.bson.Document) EconomyUtility(com.sx4.bot.utility.EconomyUtility) java.util(java.util) Command(com.jockie.bot.core.command.Command) Permission(net.dv8tion.jda.api.Permission) Projections(com.mongodb.client.model.Projections) CommandId(com.sx4.bot.annotations.command.CommandId) PagedResult(com.sx4.bot.paged.PagedResult) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) Alternative(com.sx4.bot.entities.argument.Alternative) com.sx4.bot.entities.economy.item(com.sx4.bot.entities.economy.item) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) TimeUtility(com.sx4.bot.utility.TimeUtility) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) UpdateOptions(com.mongodb.client.model.UpdateOptions) Argument(com.jockie.bot.core.argument.Argument) Operators(com.sx4.bot.database.mongo.model.Operators) Sx4Command(com.sx4.bot.core.Sx4Command) Updates(com.mongodb.client.model.Updates) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Collectors(java.util.stream.Collectors) ModuleCategory(com.sx4.bot.category.ModuleCategory) Examples(com.sx4.bot.annotations.command.Examples) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) Bson(org.bson.conversions.Bson) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Aggregations

Sx4Command (com.sx4.bot.core.Sx4Command)177 Command (com.jockie.bot.core.command.Command)175 CommandId (com.sx4.bot.annotations.command.CommandId)115 Examples (com.sx4.bot.annotations.command.Examples)115 Document (org.bson.Document)111 Bson (org.bson.conversions.Bson)96 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)82 PagedResult (com.sx4.bot.paged.PagedResult)71 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)59 Argument (com.jockie.bot.core.argument.Argument)53 ModuleCategory (com.sx4.bot.category.ModuleCategory)53 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)53 Permission (net.dv8tion.jda.api.Permission)53 BotPermissions (com.sx4.bot.annotations.command.BotPermissions)44 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)44 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)42 Operators (com.sx4.bot.database.mongo.model.Operators)41 User (net.dv8tion.jda.api.entities.User)40 Collectors (java.util.stream.Collectors)36 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)31