Search in sources :

Example 11 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.

the class BlacklistCommand method reset.

@Command(value = "reset", description = "Reset the blacklist for a specific role/user in a channel")
@CommandId(181)
@Examples({ "blacklist reset #channel", "blacklist reset all" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void reset(Sx4CommandEvent event, @Argument(value = "channel", 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.whitelisted", "holder")), Collections.EMPTY_LIST)), Collections.EMPTY_LIST, List.of(Operators.removeObject("$$this", "blacklisted")))))));
    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 blacklisted in this server").queue();
                return;
            }
            event.replySuccess("Reset **" + result.getModifiedCount() + "** channels of their blacklist 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 blacklisted in that channel").queue();
                return;
            }
            event.replySuccess("That channel no longer has any blacklists").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 12 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions 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 13 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions 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)

Example 14 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.

the class FishingRodCommand method repair.

@Command(value = "repair", description = "Repair your current fishing rod with the material it is made from")
@CommandId(383)
@Examples({ "fishing rod repair 10", "fishing rod repair all" })
public void repair(Sx4CommandEvent event, @Argument(value = "durability") @AlternativeOptions("all") Alternative<Integer> option) {
    Bson filter = Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.type", ItemType.ROD.getId()));
    Document data = event.getMongo().getItem(filter, Projections.include("item"));
    if (data == null) {
        event.replyFailure("You do not have a fishing rod").queue();
        return;
    }
    Rod rod = Rod.fromData(event.getBot().getEconomyManager(), data.get("item", Document.class));
    CraftItem item = rod.getRepairItem();
    if (item == null) {
        event.replyFailure("That fishing rod is not repairable").queue();
        return;
    }
    int maxDurability = rod.getMaxDurability() - rod.getDurability();
    if (maxDurability <= 0) {
        event.replyFailure("Your fishing rod is already at full durability").queue();
        return;
    }
    int durability;
    if (option.isAlternative()) {
        durability = maxDurability;
    } else {
        int amount = option.getValue();
        if (amount > maxDurability) {
            event.reply("You can only repair your fishing rod by **" + maxDurability + "** durability :no_entry:").queue();
            return;
        }
        durability = amount;
    }
    String acceptId = new CustomButtonId.Builder().setType(ButtonType.FISHING_ROD_REPAIR_CONFIRM).setTimeout(60).setOwners(event.getAuthor().getIdLong()).setArguments(item.getId(), rod.getId(), rod.getDurability(), durability).getId();
    String rejectId = new CustomButtonId.Builder().setType(ButtonType.GENERIC_REJECT).setTimeout(60).setOwners(event.getAuthor().getIdLong()).getId();
    List<Button> buttons = List.of(Button.success(acceptId, "Yes"), Button.danger(rejectId, "No"));
    int itemCount = (int) Math.ceil((((double) rod.getPrice() / item.getPrice()) / rod.getMaxDurability()) * durability);
    event.reply("It will cost you `" + itemCount + " " + item.getName() + "` to repair your fishing rod by **" + durability + "** durability, are you sure you want to repair it?").setActionRow(buttons).queue();
}
Also used : Button(net.dv8tion.jda.api.interactions.components.buttons.Button) Rod(com.sx4.bot.entities.economy.item.Rod) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) CustomButtonId(com.sx4.bot.entities.interaction.CustomButtonId) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) CraftItem(com.sx4.bot.entities.economy.item.CraftItem) Bson(org.bson.conversions.Bson) 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 15 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.

the class AxeCommand method repair.

@Command(value = "repair", description = "Repair your current axe with the material it is made from")
@CommandId(392)
@Examples({ "axe repair 10", "axe repair all" })
public void repair(Sx4CommandEvent event, @Argument(value = "durability") @AlternativeOptions("all") Alternative<Integer> option) {
    Bson filter = Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.type", ItemType.AXE.getId()));
    Document data = event.getMongo().getItem(filter, Projections.include("item"));
    if (data == null) {
        event.replyFailure("You do not have a axe").queue();
        return;
    }
    Axe axe = Axe.fromData(event.getBot().getEconomyManager(), data.get("item", Document.class));
    CraftItem item = axe.getRepairItem();
    if (item == null) {
        event.replyFailure("That axe is not repairable").queue();
        return;
    }
    int maxDurability = axe.getMaxDurability() - axe.getDurability();
    if (maxDurability <= 0) {
        event.replyFailure("Your axe is already at full durability").queue();
        return;
    }
    int durability;
    if (option.isAlternative()) {
        durability = maxDurability;
    } else {
        int amount = option.getValue();
        if (amount > maxDurability) {
            event.reply("You can only repair your axe by **" + maxDurability + "** durability :no_entry:").queue();
            return;
        }
        durability = amount;
    }
    String acceptId = new CustomButtonId.Builder().setType(ButtonType.AXE_REPAIR_CONFIRM).setTimeout(60).setOwners(event.getAuthor().getIdLong()).setArguments(item.getId(), axe.getId(), axe.getDurability(), durability).getId();
    String rejectId = new CustomButtonId.Builder().setType(ButtonType.GENERIC_REJECT).setTimeout(60).setOwners(event.getAuthor().getIdLong()).getId();
    List<Button> buttons = List.of(Button.success(acceptId, "Yes"), Button.danger(rejectId, "No"));
    int itemCount = (int) Math.ceil((((double) axe.getPrice() / item.getPrice()) / axe.getMaxDurability()) * durability);
    event.reply("It will cost you `" + itemCount + " " + item.getName() + "` to repair your axe by **" + durability + "** durability, are you sure you want to repair it?").setActionRow(buttons).queue();
}
Also used : Button(net.dv8tion.jda.api.interactions.components.buttons.Button) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) CustomButtonId(com.sx4.bot.entities.interaction.CustomButtonId) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) CraftItem(com.sx4.bot.entities.economy.item.CraftItem) Bson(org.bson.conversions.Bson) Axe(com.sx4.bot.entities.economy.item.Axe) 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

Command (com.jockie.bot.core.command.Command)26 Sx4Command (com.sx4.bot.core.Sx4Command)26 Bson (org.bson.conversions.Bson)21 CommandId (com.sx4.bot.annotations.command.CommandId)16 Examples (com.sx4.bot.annotations.command.Examples)16 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)11 Document (org.bson.Document)11 Button (net.dv8tion.jda.api.interactions.components.buttons.Button)10 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)9 TextChannel (net.dv8tion.jda.api.entities.TextChannel)8 Argument (com.jockie.bot.core.argument.Argument)7 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)7 ModuleCategory (com.sx4.bot.category.ModuleCategory)7 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)7 Alternative (com.sx4.bot.entities.argument.Alternative)7 CustomButtonId (com.sx4.bot.entities.interaction.CustomButtonId)6 PagedResult (com.sx4.bot.paged.PagedResult)6 List (java.util.List)6 Filters (com.mongodb.client.model.Filters)5 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)5