Search in sources :

Example 21 with Argument

use of com.jockie.bot.core.argument.Argument in project Sx4 by sx4-discord-bot.

the class PickaxeCommand method upgrade.

@Command(value = "upgrade", description = "Upgrade your pickaxe by a certain attribute")
@CommandId(427)
@Examples({ "pickaxe upgrade money", "pickaxe upgrade multiplier 10", "pickaxe upgrade durability 5" })
public void upgrade(Sx4CommandEvent event, @Argument(value = "upgrade") Upgrade upgrade, @Argument(value = "upgrades") @DefaultNumber(1) @Limit(min = 1, max = 100) int upgrades) {
    if (!upgrade.containsType(ItemType.PICKAXE)) {
        event.replyFailure("You can not use that upgrade on a pickaxe").queue();
        return;
    }
    event.getMongo().withTransaction(session -> {
        Document data = event.getMongo().getItems().find(session, Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.type", ItemType.PICKAXE.getId()))).first();
        if (data == null) {
            event.replyFailure("You do not have a pickaxe").queue();
            session.abortTransaction();
            return null;
        }
        Document item = data.get("item", Document.class);
        Pickaxe defaultPickaxe = event.getBot().getEconomyManager().getItemById(item.getInteger("id"), Pickaxe.class);
        Pickaxe pickaxe = new Pickaxe(item, defaultPickaxe);
        int currentUpgrades = pickaxe.getUpgrades();
        long price = 0;
        for (int i = 0; i < upgrades; i++) {
            price += Math.round(0.015D * defaultPickaxe.getPrice() * currentUpgrades++ + 0.025D * defaultPickaxe.getPrice());
        }
        UpdateResult result = event.getMongo().getUsers().updateOne(session, Filters.eq("_id", event.getAuthor().getIdLong()), List.of(EconomyUtility.decreaseBalanceUpdate(price)));
        if (result.getModifiedCount() == 0) {
            event.replyFormat("You do not have **$%,d** %s", price, event.getConfig().getFailureEmote()).queue();
            session.abortTransaction();
            return null;
        }
        List<Bson> update = new ArrayList<>();
        update.add(Operators.set("item.upgrades", Operators.add(Operators.ifNull("$item.upgrades", 0), upgrades)));
        update.add(Operators.set("item.price", Operators.add("$item.price", Math.round(defaultPickaxe.getPrice() * 0.015D) * upgrades)));
        if (upgrade == Upgrade.MONEY) {
            int increase = (int) Math.round(defaultPickaxe.getMinYield() * upgrade.getValue()) * upgrades;
            update.add(Operators.set("item.minYield", Operators.add("$item.minYield", increase)));
            update.add(Operators.set("item.maxYield", Operators.add("$item.maxYield", increase)));
        } else if (upgrade == Upgrade.DURABILITY) {
            int increase = (int) upgrade.getValue() * upgrades;
            update.add(Operators.set("item.durability", Operators.add("$item.durability", increase)));
            update.add(Operators.set("item.maxDurability", Operators.add("$item.maxDurability", increase)));
        } else if (upgrade == Upgrade.MULTIPLIER) {
            double increase = defaultPickaxe.getMultiplier() * upgrade.getValue() * upgrades;
            update.add(Operators.set("item.multiplier", Operators.add("$item.multiplier", increase)));
        }
        event.getMongo().getItems().updateOne(session, Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.id", pickaxe.getId())), update);
        return String.format("You just upgraded %s %d time%s for your `%s` for **$%,d**", upgrade.getName().toLowerCase(), upgrades, (upgrades == 1 ? "" : "s"), pickaxe.getName(), price);
    }).whenComplete((message, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception) || message == null) {
            return;
        }
        event.replySuccess(message).queue();
    });
}
Also used : Document(org.bson.Document) EconomyUtility(com.sx4.bot.utility.EconomyUtility) Arrays(java.util.Arrays) CancelException(com.sx4.bot.waiter.exception.CancelException) Command(com.jockie.bot.core.command.Command) ButtonClickEvent(net.dv8tion.jda.api.events.interaction.ButtonClickEvent) Permission(net.dv8tion.jda.api.Permission) CraftItem(com.sx4.bot.entities.economy.item.CraftItem) CommandId(com.sx4.bot.annotations.command.CommandId) CompletableFuture(java.util.concurrent.CompletableFuture) Member(net.dv8tion.jda.api.entities.Member) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) Bson(org.bson.conversions.Bson) Alternative(com.sx4.bot.entities.argument.Alternative) ItemStack(com.sx4.bot.entities.economy.item.ItemStack) ButtonUtility(com.sx4.bot.utility.ButtonUtility) UpdateResult(com.mongodb.client.result.UpdateResult) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Pickaxe(com.sx4.bot.entities.economy.item.Pickaxe) Button(net.dv8tion.jda.api.interactions.components.Button) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) Waiter(com.sx4.bot.waiter.Waiter) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) com.mongodb.client.model(com.mongodb.client.model) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) EnumSet(java.util.EnumSet) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) Upgrade(com.sx4.bot.entities.economy.upgrade.Upgrade) Operators(com.sx4.bot.database.mongo.model.Operators) ItemType(com.sx4.bot.entities.economy.item.ItemType) Sx4Command(com.sx4.bot.core.Sx4Command) NumberUtility(com.sx4.bot.utility.NumberUtility) CompletionException(java.util.concurrent.CompletionException) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Collectors(java.util.stream.Collectors) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) DefaultNumber(com.sx4.bot.annotations.argument.DefaultNumber) Examples(com.sx4.bot.annotations.command.Examples) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Pickaxe(com.sx4.bot.entities.economy.item.Pickaxe) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) 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 22 with Argument

use of com.jockie.bot.core.argument.Argument in project Sx4 by sx4-discord-bot.

the class TradeCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "user", endless = true) Member member) {
    User user = member.getUser();
    if (user.isBot()) {
        event.replyFailure("You can not trade with bots").queue();
        return;
    }
    if (user.getIdLong() == event.getAuthor().getIdLong()) {
        event.replyFailure("You can not trade with yourself").queue();
        return;
    }
    String prompt = "What %s the user? Make sure you put a space between every thing you want to offer, for example: `2 gold, 200, 5 titanium, 1 coal factory` would offer $200, 5 Titanium, 2 Gold and 1 Coal Factory (Respond Below)";
    AtomicLong userMoneyAtomic = new AtomicLong(0), authorMoneyAtomic = new AtomicLong(0);
    Map<Item, Long> userItems = new HashMap<>(), authorItems = new HashMap<>();
    event.reply(String.format(prompt, "are you offering to")).submit().thenCompose(message -> {
        return new Waiter<>(event.getBot(), MessageReceivedEvent.class).setPredicate(e -> !e.getMessage().getContentRaw().equalsIgnoreCase("cancel")).setCancelPredicate(e -> e.getMessage().getContentRaw().equalsIgnoreCase("cancel")).setUnique(event.getAuthor().getIdLong(), event.getChannel().getIdLong()).setTimeout(60).start();
    }).thenCompose(e -> {
        Map.Entry<Long, Map<Item, Long>> trade = this.getTrade(event.getBot().getEconomyManager(), e.getMessage().getContentRaw());
        Map<Item, Long> items = trade.getValue();
        long money = trade.getKey();
        if (items.isEmpty() && money == 0) {
            throw new IllegalArgumentException("No valid or tradeable items or money were supplied");
        }
        userItems.putAll(trade.getValue());
        userMoneyAtomic.set(trade.getKey());
        StringJoiner content = new StringJoiner("\n");
        items.forEach((key, value) -> content.add(value + " " + key.getName()));
        EmbedBuilder embed = new EmbedBuilder().setTitle("What you are offering to " + user.getAsTag()).setDescription((money == 0 ? "" : String.format("$%,d", money)) + (items.isEmpty() ? "" : "\n") + content);
        MessageBuilder message = new MessageBuilder().setEmbeds(embed.build()).setContent(String.format(prompt, "would you like from"));
        return event.reply(message.build()).submit();
    }).thenCompose(message -> {
        return new Waiter<>(event.getBot(), MessageReceivedEvent.class).setPredicate(e -> !e.getMessage().getContentRaw().equalsIgnoreCase("cancel")).setCancelPredicate(e -> e.getMessage().getContentRaw().equalsIgnoreCase("cancel")).setUnique(event.getAuthor().getIdLong(), event.getChannel().getIdLong()).setTimeout(60).start();
    }).thenCompose(e -> {
        Map.Entry<Long, Map<Item, Long>> trade = this.getTrade(event.getBot().getEconomyManager(), e.getMessage().getContentRaw());
        authorItems.putAll(trade.getValue());
        long authorMoney = trade.getKey();
        if (authorItems.isEmpty() && authorMoney == 0) {
            throw new IllegalArgumentException("No valid or tradeable items or money were supplied");
        }
        authorMoneyAtomic.set(authorMoney);
        long userMoney = userMoneyAtomic.get();
        StringJoiner authorContent = new StringJoiner("\n");
        authorItems.forEach((key, value) -> authorContent.add(value + " " + key.getName()));
        StringJoiner userContent = new StringJoiner("\n");
        userItems.forEach((key, value) -> userContent.add(value + " " + key.getName()));
        EmbedBuilder embed = new EmbedBuilder().setTitle("Final Trade").addField(user.getAsTag() + " Gets", (userMoney == 0 ? "" : String.format("$%,d", userMoney)) + (userItems.isEmpty() ? "" : "\n") + userContent, false).addField(event.getAuthor().getAsTag() + " Gets", (authorMoney == 0 ? "" : String.format("$%,d", authorMoney)) + (authorItems.isEmpty() ? "" : "\n") + authorContent, false);
        MessageBuilder message = new MessageBuilder().setEmbeds(embed.build()).setActionRows(ActionRow.of(List.of(Button.success("yes", "Yes"), Button.danger("no", "No")))).setAllowedMentions(EnumSet.of(Message.MentionType.USER)).setContent(user.getAsMention() + ", do you accept this trade?");
        return event.reply(message.build()).submit();
    }).thenCompose(message -> {
        return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, user)).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, user)).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
    }).whenCompleteAsync((e, exception) -> {
        Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
        if (cause instanceof CancelException) {
            GenericEvent cancelEvent = ((CancelException) cause).getEvent();
            if (cancelEvent != null) {
                ((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
            }
            return;
        } else if (cause instanceof TimeoutException) {
            event.reply("Timed out :stopwatch:").queue();
            return;
        } else if (cause instanceof IllegalArgumentException) {
            event.replyFailure(cause.getMessage()).queue();
            return;
        }
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        event.getMongo().withTransaction(session -> {
            Map<String, Long> types = new HashMap<>();
            Map<Item, Long> combined = new HashMap<>();
            long authorMoney = authorMoneyAtomic.get(), userMoney = userMoneyAtomic.get();
            long totalAuthorWorth = authorMoney;
            for (Item item : authorItems.keySet()) {
                if (item instanceof Envelope) {
                    e.reply("You can not trade envelopes " + event.getConfig().getFailureEmote()).queue();
                    session.abortTransaction();
                    return;
                } else if (item instanceof Tool) {
                    e.reply("You can not trade tools " + event.getConfig().getFailureEmote()).queue();
                    session.abortTransaction();
                    return;
                }
                long amount = authorItems.get(item);
                totalAuthorWorth += item.getPrice() * amount;
                types.compute(item.getName(), (key, value) -> value == null ? amount : value + amount);
                combined.compute(item, (key, value) -> value == null ? -amount : value - amount);
            }
            long totalUserWorth = userMoney;
            for (Item item : userItems.keySet()) {
                if (item instanceof Envelope) {
                    e.reply("You can not trade envelopes " + event.getConfig().getFailureEmote()).queue();
                    session.abortTransaction();
                    return;
                } else if (item instanceof Tool) {
                    e.reply("You can not trade tools " + event.getConfig().getFailureEmote()).queue();
                    session.abortTransaction();
                    return;
                }
                long amount = userItems.get(item);
                totalUserWorth += item.getPrice() * amount;
                types.compute(item.getName(), (key, value) -> value == null ? amount : value + amount);
                combined.compute(item, (key, value) -> value == null ? amount : value + amount);
            }
            types.put("Money", authorMoney + userMoney);
            Map.Entry<String, Long> max = types.entrySet().stream().max(Map.Entry.comparingByValue()).get();
            if ((double) max.getValue() / (totalUserWorth + totalAuthorWorth) >= 0.7D) {
                e.reply(max.getKey() + " cannot make up more than 70% of the trades value " + event.getConfig().getFailureEmote()).queue();
                session.abortTransaction();
                return;
            }
            if (totalUserWorth / totalAuthorWorth > 5 || totalAuthorWorth / totalUserWorth > 5) {
                e.reply("You have to trade at least 20% the worth of the other persons trade " + event.getConfig().getFailureEmote()).queue();
                session.abortTransaction();
                return;
            }
            if (userMoney - authorMoney != 0) {
                List<Bson> authorUpdate = List.of(Operators.set("economy.balance", Operators.let(new Document("balance", Operators.ifNull("$economy.balance", 0L)), Operators.let(new Document("newBalance", Operators.add("$$balance", authorMoney - userMoney)), Operators.cond(Operators.lt("$$newBalance", 0L), "$$balance", "$$newBalance")))));
                UpdateResult authorResult = event.getMongo().getUsers().updateOne(session, Filters.eq("_id", event.getAuthor().getIdLong()), authorUpdate);
                if (authorResult.getModifiedCount() == 0) {
                    e.replyFormat("%s does not have **$%,d** %s", event.getAuthor().getAsTag(), userMoney - authorMoney, event.getConfig().getFailureEmote()).queue();
                    session.abortTransaction();
                    return;
                }
                List<Bson> userUpdate = List.of(Operators.set("economy.balance", Operators.let(new Document("balance", Operators.ifNull("$economy.balance", 0L)), Operators.let(new Document("newBalance", Operators.add("$$balance", userMoney - authorMoney)), Operators.cond(Operators.lt("$$newBalance", 0L), "$$balance", "$$newBalance")))));
                UpdateResult userResult = event.getMongo().getUsers().updateOne(session, Filters.eq("_id", user.getIdLong()), userUpdate);
                if (userResult.getModifiedCount() == 0) {
                    e.replyFormat("%s does not have **$%,d** %s", event.getAuthor().getAsTag(), authorMoney - userMoney, event.getConfig().getFailureEmote()).queue();
                    session.abortTransaction();
                    return;
                }
            }
            for (Item item : combined.keySet()) {
                long signedAmount = combined.get(item);
                if (signedAmount == 0) {
                    continue;
                }
                boolean author = signedAmount < 0;
                long amount = Math.abs(signedAmount);
                List<Bson> addUpdate = List.of(Operators.set("item", item.toData()), Operators.set("amount", Operators.add(Operators.ifNull("$amount", 0L), amount)));
                event.getMongo().getItems().updateOne(session, Filters.and(Filters.eq("userId", author ? event.getAuthor().getIdLong() : user.getIdLong()), Filters.eq("item.id", item.getId())), addUpdate, new UpdateOptions().upsert(true));
                List<Bson> removeUpdate = List.of(Operators.set("amount", Operators.let(new Document("amount", Operators.ifNull("$amount", 0L)), Operators.cond(Operators.lt(Operators.subtract("$$amount", Operators.sum(Operators.map(Operators.filter(Operators.ifNull("$resets", Collections.EMPTY_LIST), Operators.gt("$$this.time", Operators.nowEpochSecond())), "$$this.amount"))), amount), "$$amount", Operators.subtract("$$amount", amount)))));
                FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().projection(Projections.include("amount", "resets")).returnDocument(ReturnDocument.BEFORE);
                Document data = event.getMongo().getItems().findOneAndUpdate(session, Filters.and(Filters.eq("userId", author ? user.getIdLong() : event.getAuthor().getIdLong()), Filters.eq("item.id", item.getId())), removeUpdate, options);
                long userAmount = data == null ? 0L : data.get("amount", 0L);
                if (userAmount < amount) {
                    e.reply((author ? user.getAsTag() : event.getAuthor().getAsTag()) + " does not have `" + amount + " " + item.getName() + "` " + event.getConfig().getFailureEmote()).queue();
                    session.abortTransaction();
                    return;
                }
                CooldownItemStack<Item> cooldownStack = new CooldownItemStack<>(item, data);
                long cooldownAmount = cooldownStack.getCooldownAmount();
                if (userAmount - cooldownAmount < amount) {
                    e.replyFormat("%s has `%,d %s` but **%,d** %s on cooldown %s", author ? user.getAsTag() : event.getAuthor().getAsTag(), userAmount, item.getName(), cooldownAmount, cooldownAmount == 1 ? "is" : "are", event.getConfig().getFailureEmote()).queue();
                    session.abortTransaction();
                    return;
                }
            }
        }).whenComplete((updated, databaseException) -> {
            if (ExceptionUtility.sendExceptionally(event, databaseException) || !updated) {
                return;
            }
            e.reply("All money and items have been traded " + event.getConfig().getSuccessEmote()).queue();
        });
    });
}
Also used : Document(org.bson.Document) java.util(java.util) CancelException(com.sx4.bot.waiter.exception.CancelException) ButtonClickEvent(net.dv8tion.jda.api.events.interaction.ButtonClickEvent) Permission(net.dv8tion.jda.api.Permission) Member(net.dv8tion.jda.api.entities.Member) User(net.dv8tion.jda.api.entities.User) Bson(org.bson.conversions.Bson) ButtonUtility(com.sx4.bot.utility.ButtonUtility) UpdateResult(com.mongodb.client.result.UpdateResult) com.sx4.bot.entities.economy.item(com.sx4.bot.entities.economy.item) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Button(net.dv8tion.jda.api.interactions.components.Button) Waiter(com.sx4.bot.waiter.Waiter) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) ActionRow(net.dv8tion.jda.api.interactions.components.ActionRow) com.mongodb.client.model(com.mongodb.client.model) Argument(com.jockie.bot.core.argument.Argument) Message(net.dv8tion.jda.api.entities.Message) Operators(com.sx4.bot.database.mongo.model.Operators) Sx4Command(com.sx4.bot.core.Sx4Command) EconomyManager(com.sx4.bot.managers.EconomyManager) CompletionException(java.util.concurrent.CompletionException) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ModuleCategory(com.sx4.bot.category.ModuleCategory) AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) MessageReceivedEvent(net.dv8tion.jda.api.events.message.MessageReceivedEvent) User(net.dv8tion.jda.api.entities.User) Document(org.bson.Document) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) UpdateResult(com.mongodb.client.result.UpdateResult) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) AtomicLong(java.util.concurrent.atomic.AtomicLong) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) CompletionException(java.util.concurrent.CompletionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) CancelException(com.sx4.bot.waiter.exception.CancelException) Waiter(com.sx4.bot.waiter.Waiter)

Example 23 with Argument

use of com.jockie.bot.core.argument.Argument in project Sx4 by sx4-discord-bot.

the class MarriageCommand method remove.

@Command(value = "remove", description = "Divorce someone you are currently married to")
@CommandId(269)
@Redirects({ "divorce" })
@Examples({ "marriage remove @Shea#6653", "marriage remove Shea", "marriage remove all" })
public void remove(Sx4CommandEvent event, @Argument(value = "user | all", endless = true, nullDefault = true) @AlternativeOptions("all") Alternative<Member> option) {
    User author = event.getAuthor();
    if (option == null) {
        Bson filter = Filters.or(Filters.eq("proposerId", author.getIdLong()), Filters.eq("partnerId", author.getIdLong()));
        List<Document> marriages = event.getMongo().getMarriages(filter, Projections.include("proposerId", "partnerId")).into(new ArrayList<>());
        if (marriages.isEmpty()) {
            event.replyFailure("You are not married to anyone").queue();
            return;
        }
        List<Long> userIds = marriages.stream().map(marriage -> {
            long partnerId = marriage.getLong("partnerId");
            return partnerId == author.getIdLong() ? marriage.getLong("proposerId") : partnerId;
        }).collect(Collectors.toList());
        PagedResult<Long> paged = new PagedResult<>(event.getBot(), userIds).setAuthor("Divorce", null, author.getEffectiveAvatarUrl()).setTimeout(60).setDisplayFunction(userId -> {
            User other = event.getShardManager().getUserById(userId);
            return (other == null ? "Anonymous#0000" : other.getAsTag()) + " (" + userId + ")";
        });
        paged.onTimeout(() -> event.reply("Timed out :stopwatch:").queue());
        paged.onSelect(select -> {
            long userId = select.getSelected();
            Bson deleteFilter = Filters.or(Filters.and(Filters.eq("proposerId", userId), Filters.eq("partnerId", author.getIdLong())), Filters.and(Filters.eq("proposerId", author.getIdLong()), Filters.eq("partnerId", userId)));
            event.getMongo().deleteMarriage(deleteFilter).whenComplete((result, exception) -> {
                if (ExceptionUtility.sendExceptionally(event, exception)) {
                    return;
                }
                User user = event.getShardManager().getUserById(userId);
                event.replySuccess("You are no longer married to **" + (user == null ? "Anonymous#0000" : user.getAsTag()) + "**").queue();
            });
        });
        paged.execute(event);
    } else if (option.isAlternative()) {
        List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
        event.reply(author.getName() + ", are you sure you want to divorce everyone you are currently married to?").setActionRow(buttons).submit().thenCompose(message -> {
            return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
        }).whenComplete((e, exception) -> {
            Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
            if (cause instanceof CancelException) {
                GenericEvent cancelEvent = ((CancelException) cause).getEvent();
                if (cancelEvent != null) {
                    ((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
                }
                return;
            } else if (cause instanceof TimeoutException) {
                event.reply("Timed out :stopwatch:").queue();
                return;
            } else if (ExceptionUtility.sendExceptionally(event, exception)) {
                return;
            }
            Bson filter = Filters.or(Filters.eq("proposerId", author.getIdLong()), Filters.eq("partnerId", author.getIdLong()));
            event.getMongo().deleteManyMarriages(filter).whenComplete((result, databaseException) -> {
                if (ExceptionUtility.sendExceptionally(event, databaseException)) {
                    return;
                }
                if (result.getDeletedCount() == 0) {
                    e.reply("You are not married to anyone " + event.getConfig().getFailureEmote()).queue();
                    return;
                }
                e.reply("You are no longer married to anyone " + event.getConfig().getSuccessEmote()).queue();
            });
        });
    } else {
        Member member = option.getValue();
        Bson filter = Filters.or(Filters.and(Filters.eq("proposerId", member.getIdLong()), Filters.eq("partnerId", author.getIdLong())), Filters.and(Filters.eq("proposerId", author.getIdLong()), Filters.eq("partnerId", member.getIdLong())));
        event.getMongo().deleteMarriage(filter).whenComplete((result, exception) -> {
            if (ExceptionUtility.sendExceptionally(event, exception)) {
                return;
            }
            if (result.getDeletedCount() == 0) {
                event.replyFailure("You are not married to that user").queue();
                return;
            }
            event.replySuccess("You are no longer married to **" + member.getUser().getAsTag() + "**").queue();
        });
    }
}
Also used : Document(org.bson.Document) CancelException(com.sx4.bot.waiter.exception.CancelException) Command(com.jockie.bot.core.command.Command) ButtonClickEvent(net.dv8tion.jda.api.events.interaction.ButtonClickEvent) Projections(com.mongodb.client.model.Projections) Cooldown(com.jockie.bot.core.command.Command.Cooldown) CommandId(com.sx4.bot.annotations.command.CommandId) Member(net.dv8tion.jda.api.entities.Member) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) Redirects(com.sx4.bot.annotations.command.Redirects) Alternative(com.sx4.bot.entities.argument.Alternative) ButtonUtility(com.sx4.bot.utility.ButtonUtility) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Button(net.dv8tion.jda.api.interactions.components.Button) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) Waiter(com.sx4.bot.waiter.Waiter) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) ZoneOffset(java.time.ZoneOffset) EnumSet(java.util.EnumSet) Argument(com.jockie.bot.core.argument.Argument) Message(net.dv8tion.jda.api.entities.Message) Sx4Command(com.sx4.bot.core.Sx4Command) Updates(com.mongodb.client.model.Updates) CooldownMessage(com.sx4.bot.annotations.command.CooldownMessage) CompletionException(java.util.concurrent.CompletionException) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Examples(com.sx4.bot.annotations.command.Examples) OffsetDateTime(java.time.OffsetDateTime) Sorts(com.mongodb.client.model.Sorts) DateTimeFormatter(java.time.format.DateTimeFormatter) StringJoiner(java.util.StringJoiner) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) User(net.dv8tion.jda.api.entities.User) ButtonClickEvent(net.dv8tion.jda.api.events.interaction.ButtonClickEvent) Document(org.bson.Document) Bson(org.bson.conversions.Bson) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) CompletionException(java.util.concurrent.CompletionException) ArrayList(java.util.ArrayList) List(java.util.List) CancelException(com.sx4.bot.waiter.exception.CancelException) Member(net.dv8tion.jda.api.entities.Member) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) 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 24 with Argument

use of com.jockie.bot.core.argument.Argument in project Sx4 by sx4-discord-bot.

the class MarriageCommand method add.

@Command(value = "add", description = "Propose to a user and marry them if they accept")
@CommandId(268)
@Redirects({ "marry" })
@Cooldown(60)
@CooldownMessage("You already have a pending marriage :no_entry:")
@Examples({ "marriage add @Shea#6653", "marriage add Shea", "marriage add 402557516728369153" })
public void add(Sx4CommandEvent event, @Argument(value = "user", endless = true) Member member) {
    User author = event.getAuthor();
    if (member.getUser().isBot()) {
        event.replyFailure("You cannot marry bots").queue();
        return;
    }
    Bson checkFilter = Filters.or(Filters.eq("proposerId", author.getIdLong()), Filters.eq("partnerId", author.getIdLong()), Filters.eq("proposerId", member.getIdLong()), Filters.eq("partnerId", member.getIdLong()));
    List<Document> marriages = event.getMongo().getMarriages(checkFilter, Projections.include("partnerId", "proposerId")).into(new ArrayList<>());
    long userCount = marriages.stream().filter(d -> d.getLong("proposerId") == author.getIdLong() || d.getLong("partnerId") == author.getIdLong()).count();
    if (userCount >= 5) {
        event.removeCooldown();
        event.replyFailure("You cannot marry more than 5 users").queue();
        return;
    }
    long memberCount = marriages.stream().filter(d -> d.getLong("proposerId") == member.getIdLong() || d.getLong("partnerId") == member.getIdLong()).count();
    if (memberCount >= 5) {
        event.removeCooldown();
        event.replyFailure("That user is already married to 5 users").queue();
        return;
    }
    List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
    event.reply(member.getAsMention() + ", **" + author.getName() + "** would like to marry you! Do you accept?").allowedMentions(EnumSet.of(Message.MentionType.USER)).setActionRow(buttons).submit().thenCompose(message -> {
        return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, member.getUser())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, member.getUser())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
    }).whenComplete((e, exception) -> {
        event.removeCooldown();
        Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
        if (cause instanceof CancelException) {
            GenericEvent cancelEvent = ((CancelException) cause).getEvent();
            if (cancelEvent != null) {
                ((ButtonClickEvent) cancelEvent).reply("Better luck next time " + author.getName() + " :broken_heart:").queue();
            }
            return;
        } else if (cause instanceof TimeoutException) {
            event.reply("Timed out :stopwatch:").queue();
            return;
        } else if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        Bson filter = Filters.or(Filters.and(Filters.eq("proposerId", member.getIdLong()), Filters.eq("partnerId", author.getIdLong())), Filters.and(Filters.eq("proposerId", author.getIdLong()), Filters.eq("partnerId", member.getIdLong())));
        event.getMongo().updateMarriage(filter, Updates.combine(Updates.setOnInsert("proposerId", author.getIdLong()), Updates.setOnInsert("partnerId", member.getIdLong()))).whenComplete((result, databaseException) -> {
            if (ExceptionUtility.sendExceptionally(event, databaseException)) {
                return;
            }
            if (result.getMatchedCount() != 0) {
                e.reply("You're already married to that user " + event.getConfig().getFailureEmote()).queue();
                return;
            }
            e.reply("You're now married to " + member.getAsMention() + " :tada: :heart:").queue();
        });
    });
}
Also used : Document(org.bson.Document) CancelException(com.sx4.bot.waiter.exception.CancelException) Command(com.jockie.bot.core.command.Command) ButtonClickEvent(net.dv8tion.jda.api.events.interaction.ButtonClickEvent) Projections(com.mongodb.client.model.Projections) Cooldown(com.jockie.bot.core.command.Command.Cooldown) CommandId(com.sx4.bot.annotations.command.CommandId) Member(net.dv8tion.jda.api.entities.Member) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) Redirects(com.sx4.bot.annotations.command.Redirects) Alternative(com.sx4.bot.entities.argument.Alternative) ButtonUtility(com.sx4.bot.utility.ButtonUtility) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Button(net.dv8tion.jda.api.interactions.components.Button) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) Waiter(com.sx4.bot.waiter.Waiter) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) ZoneOffset(java.time.ZoneOffset) EnumSet(java.util.EnumSet) Argument(com.jockie.bot.core.argument.Argument) Message(net.dv8tion.jda.api.entities.Message) Sx4Command(com.sx4.bot.core.Sx4Command) Updates(com.mongodb.client.model.Updates) CooldownMessage(com.sx4.bot.annotations.command.CooldownMessage) CompletionException(java.util.concurrent.CompletionException) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Examples(com.sx4.bot.annotations.command.Examples) OffsetDateTime(java.time.OffsetDateTime) Sorts(com.mongodb.client.model.Sorts) DateTimeFormatter(java.time.format.DateTimeFormatter) StringJoiner(java.util.StringJoiner) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) User(net.dv8tion.jda.api.entities.User) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Button(net.dv8tion.jda.api.interactions.components.Button) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) CompletionException(java.util.concurrent.CompletionException) CancelException(com.sx4.bot.waiter.exception.CancelException) Waiter(com.sx4.bot.waiter.Waiter) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) Redirects(com.sx4.bot.annotations.command.Redirects) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) Cooldown(com.jockie.bot.core.command.Command.Cooldown) CooldownMessage(com.sx4.bot.annotations.command.CooldownMessage) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 25 with Argument

use of com.jockie.bot.core.argument.Argument in project Sx4 by sx4-discord-bot.

the class ReputationCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "user", endless = true) Member member) {
    User user = member.getUser();
    if (user.getIdLong() == event.getAuthor().getIdLong()) {
        event.replyFailure("You can not give reputation to yourself").queue();
        return;
    }
    if (user.isBot()) {
        event.replyFailure("You can not give reputation to bots").queue();
        return;
    }
    event.getMongo().withTransaction(session -> {
        List<Bson> update = List.of(Operators.set("reputation.resets", Operators.let(new Document("resets", Operators.ifNull("$reputation.resets", 0L)), Operators.cond(Operators.lt(Operators.nowEpochSecond(), "$$resets"), "$$resets", Operators.add(Operators.nowEpochSecond(), ReputationCommand.COOLDOWN)))));
        FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().projection(Projections.include("reputation.resets")).returnDocument(ReturnDocument.BEFORE).upsert(true);
        Document data = event.getMongo().getUsers().findOneAndUpdate(session, Filters.eq("_id", event.getAuthor().getIdLong()), update, options);
        long now = Clock.systemUTC().instant().getEpochSecond(), resets = data == null ? 0L : data.getEmbedded(List.of("reputation", "resets"), 0L);
        if (now < resets) {
            event.reply("Slow down! You can give out reputation in " + TimeUtility.LONG_TIME_FORMATTER.parse(resets - now) + " :stopwatch:").queue();
            session.abortTransaction();
            return;
        }
        event.getMongo().getUsers().updateOne(session, Filters.eq("_id", user.getIdLong()), Updates.inc("reputation.amount", 1), new UpdateOptions().upsert(true));
    }).whenComplete((updated, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception) || !updated) {
            return;
        }
        event.replySuccess("**+1**, " + user.getName() + " has gained reputation").queue();
    });
}
Also used : Document(org.bson.Document) MarkdownSanitizer(net.dv8tion.jda.api.utils.MarkdownSanitizer) Command(com.jockie.bot.core.command.Command) Permission(net.dv8tion.jda.api.Permission) CommandId(com.sx4.bot.annotations.command.CommandId) Member(net.dv8tion.jda.api.entities.Member) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) Bson(org.bson.conversions.Bson) Redirects(com.sx4.bot.annotations.command.Redirects) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) TimeUtility(com.sx4.bot.utility.TimeUtility) Map(java.util.Map) Option(com.jockie.bot.core.option.Option) com.mongodb.client.model(com.mongodb.client.model) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Argument(com.jockie.bot.core.argument.Argument) Operators(com.sx4.bot.database.mongo.model.Operators) Sx4Command(com.sx4.bot.core.Sx4Command) NumberUtility(com.sx4.bot.utility.NumberUtility) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Examples(com.sx4.bot.annotations.command.Examples) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Clock(java.time.Clock) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.bson.Document)

Aggregations

Argument (com.jockie.bot.core.argument.Argument)109 ModuleCategory (com.sx4.bot.category.ModuleCategory)109 Sx4Command (com.sx4.bot.core.Sx4Command)109 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)109 Permission (net.dv8tion.jda.api.Permission)96 Document (org.bson.Document)67 PagedResult (com.sx4.bot.paged.PagedResult)57 HttpCallback (com.sx4.bot.http.HttpCallback)54 Request (okhttp3.Request)53 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)50 Command (com.jockie.bot.core.command.Command)48 List (java.util.List)47 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)42 Bson (org.bson.conversions.Bson)42 Operators (com.sx4.bot.database.mongo.model.Operators)40 User (net.dv8tion.jda.api.entities.User)40 CommandId (com.sx4.bot.annotations.command.CommandId)39 Examples (com.sx4.bot.annotations.command.Examples)39 Alternative (com.sx4.bot.entities.argument.Alternative)37 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)36