Search in sources :

Example 1 with AmountArgument

use of com.sx4.bot.entities.argument.AmountArgument in project Sx4 by sx4-discord-bot.

the class GiveCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "user") Member member, @Argument(value = "amount") AmountArgument amount) {
    User user = member == null ? event.getAuthor() : member.getUser();
    if (user.isBot() && user.getIdLong() != event.getSelfUser().getIdLong()) {
        event.replyFailure("You can not give money to bots").queue();
        return;
    }
    boolean tax = user.getIdLong() == event.getSelfUser().getIdLong();
    event.getMongo().withTransaction(session -> {
        FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().projection(Projections.include("economy.balance")).returnDocument(ReturnDocument.BEFORE);
        Document authorData = event.getMongo().getUsers().findOneAndUpdate(session, Filters.eq("_id", event.getAuthor().getIdLong()), List.of(EconomyUtility.decreaseBalanceUpdate(amount)), options);
        if (authorData == null) {
            event.replyFailure("You do not have any money").queue();
            session.abortTransaction();
            return null;
        }
        long authorBalance = authorData.getEmbedded(List.of("economy", "balance"), 0L);
        if (authorBalance == 0L) {
            event.replyFailure("You do not have any money").queue();
            session.abortTransaction();
            return null;
        }
        long effectiveAmount = amount.getEffectiveAmount(authorBalance);
        if (authorBalance < effectiveAmount) {
            event.replyFailure(String.format("You do not have **$%,d**", authorBalance)).queue();
            session.abortTransaction();
            return null;
        }
        long amountGiven = tax ? effectiveAmount : (long) (effectiveAmount * 0.95D), taxAmount = tax ? effectiveAmount : (long) Math.ceil(effectiveAmount * 0.05D);
        Document userData;
        if (tax) {
            userData = event.getMongo().getUsers().findOneAndUpdate(session, Filters.eq("_id", event.getSelfUser().getIdLong()), Updates.inc("economy.balance", amountGiven), options);
        } else {
            userData = event.getMongo().getUsers().findOneAndUpdate(session, Filters.eq("_id", user.getIdLong()), Updates.inc("economy.balance", amountGiven), options.upsert(true));
            event.getMongo().getUsers().updateOne(session, Filters.eq("_id", event.getSelfUser().getIdLong()), Updates.inc("economy.balance", taxAmount));
        }
        long userBalance = userData == null ? 0L : userData.getEmbedded(List.of("economy", "balance"), 0L);
        EmbedBuilder embed = new EmbedBuilder().setAuthor(event.getAuthor().getName() + " → " + user.getName(), null, "https://cdn0.iconfinder.com/data/icons/social-messaging-ui-color-shapes/128/money-circle-green-3-512.png").setColor(event.getMember().getColor()).setDescription(String.format("You have gifted **$%,d** to **%s**\n\n%s's new balance: **$%,d**\n%s's new balance: **$%,d**", amountGiven, user.getName(), event.getAuthor().getName(), authorBalance - effectiveAmount, user.getName(), userBalance + amountGiven)).setFooter(String.format("$%,d (%d%%) tax was taken", taxAmount, Math.round((double) taxAmount / effectiveAmount * 100)), null);
        return embed.build();
    }).whenComplete((embed, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (embed == null) {
            return;
        }
        event.reply(embed).queue();
    });
}
Also used : Document(org.bson.Document) EconomyUtility(com.sx4.bot.utility.EconomyUtility) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Member(net.dv8tion.jda.api.entities.Member) AmountArgument(com.sx4.bot.entities.argument.AmountArgument) User(net.dv8tion.jda.api.entities.User) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) com.mongodb.client.model(com.mongodb.client.model) Argument(com.jockie.bot.core.argument.Argument) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) User(net.dv8tion.jda.api.entities.User) Document(org.bson.Document)

Example 2 with AmountArgument

use of com.sx4.bot.entities.argument.AmountArgument in project Sx4 by sx4-discord-bot.

the class SlotCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "bet") AmountArgument amount) {
    Slot[] slots = Slot.getSlots(event.getBot().getEconomyManager().getRandom());
    Slot firstSlot = slots[0], secondSlot = slots[1], thirdSlot = slots[2];
    boolean won = firstSlot == secondSlot && firstSlot == thirdSlot;
    Document betData = new Document("bet", amount.hasDecimal() ? Operators.toLong(Operators.ceil(Operators.multiply(amount.getDecimal(), "$$balance"))) : amount.getAmount());
    List<Bson> update = List.of(Operators.set("economy.winnings", Operators.let(new Document("winnings", Operators.ifNull("$economy.winnings", 0L)).append("balance", Operators.ifNull("$economy.balance", 0L)), Operators.let(betData, Operators.cond(Operators.lt("$$balance", "$$bet"), "$$winnings", Operators.cond(won, Operators.add("$$winnings", Operators.subtract(Operators.toLong(Operators.round(Operators.multiply("$$bet", firstSlot.getMultiplier()))), "$$bet")), Operators.subtract("$$winnings", "$$bet")))))), Operators.set("economy.balance", Operators.let(new Document("balance", Operators.ifNull("$economy.balance", 0L)), Operators.let(betData, Operators.cond(Operators.lt("$$balance", "$$bet"), "$$balance", Operators.cond(won, Operators.add("$$balance", Operators.subtract(Operators.toLong(Operators.round(Operators.multiply("$$bet", firstSlot.getMultiplier()))), "$$bet")), Operators.subtract("$$balance", "$$bet")))))));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.BEFORE).projection(Projections.include("economy.balance")).upsert(true);
    event.getMongo().findAndUpdateUserById(event.getAuthor().getIdLong(), update, options).whenComplete((data, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (data == null) {
            event.replyFailure("You do not have any money").queue();
            return;
        }
        long balance = data.getEmbedded(List.of("economy", "balance"), 0L);
        if (balance == 0L) {
            event.replyFailure("You do not have any money").queue();
            return;
        }
        long bet = amount.getEffectiveAmount(balance);
        if (balance < bet) {
            event.replyFormat("You do not have **$%,d** %s", bet, event.getConfig().getFailureEmote()).queue();
            return;
        }
        long winnings = Math.round(bet * firstSlot.getMultiplier());
        EmbedBuilder embed = new EmbedBuilder().setAuthor("🎰 Slot Machine 🎰").setFooter(event.getAuthor().getAsTag(), event.getAuthor().getEffectiveAvatarUrl()).setThumbnail("https://images.emojiterra.com/twitter/512px/1f3b0.png").setDescription(firstSlot.getAbove().getEmote() + secondSlot.getAbove().getEmote() + thirdSlot.getAbove().getEmote() + "\n" + firstSlot.getEmote() + secondSlot.getEmote() + thirdSlot.getEmote() + "\n" + firstSlot.getBelow().getEmote() + secondSlot.getBelow().getEmote() + thirdSlot.getBelow().getEmote());
        embed.appendDescription(String.format("\n\nYou " + (won ? "won" : "lost") + " **$%,d**!", won ? winnings : bet));
        event.reply(embed.build()).queue();
        Document gameData = new Document("userId", event.getAuthor().getIdLong()).append("slots", List.of(firstSlot.name(), secondSlot.name(), thirdSlot.name())).append("bet", bet).append("winnings", won ? winnings - bet : -bet).append("state", won ? GameState.WIN.getId() : GameState.LOSS.getId()).append("gameId", ObjectId.get()).append("type", GameType.SLOT.getId());
        event.getMongo().insertGame(gameData).whenComplete(MongoDatabase.exceptionally(event));
    });
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Slot(com.sx4.bot.entities.economy.Slot) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) Bson(org.bson.conversions.Bson)

Aggregations

EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 Document (org.bson.Document)2 Argument (com.jockie.bot.core.argument.Argument)1 com.mongodb.client.model (com.mongodb.client.model)1 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)1 ReturnDocument (com.mongodb.client.model.ReturnDocument)1 ModuleCategory (com.sx4.bot.category.ModuleCategory)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)1 AmountArgument (com.sx4.bot.entities.argument.AmountArgument)1 Slot (com.sx4.bot.entities.economy.Slot)1 EconomyUtility (com.sx4.bot.utility.EconomyUtility)1 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)1 List (java.util.List)1 Permission (net.dv8tion.jda.api.Permission)1 Member (net.dv8tion.jda.api.entities.Member)1 User (net.dv8tion.jda.api.entities.User)1 Bson (org.bson.conversions.Bson)1