Search in sources :

Example 1 with ItemType

use of com.sx4.bot.entities.economy.item.ItemType in project Sx4 by sx4-discord-bot.

the class ShopCommand method list.

@Command(value = "list", description = "View what items the bot has")
@CommandId(455)
@Examples({ "shop list" })
public void list(Sx4CommandEvent event) {
    List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("userId", event.getSelfUser().getIdLong()), Filters.ne("amount", 0))), Aggregates.project(Projections.fields(Projections.computed("name", "$item.name"), Projections.computed("type", "$item.type"), Projections.include("item", "amount"))), Aggregates.sort(Sorts.descending("amount")));
    event.getMongo().aggregateItems(pipeline).whenComplete((items, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        EmbedBuilder embed = new EmbedBuilder().setAuthor("Shop List", null, event.getSelfUser().getEffectiveAvatarUrl()).setColor(event.getSelfMember().getColorRaw());
        if (items.isEmpty()) {
            event.replyFailure("That user does not have any items").queue();
            return;
        }
        Map<ItemType, StringJoiner> types = new HashMap<>();
        for (Document item : items) {
            ItemType type = ItemType.fromId(item.getInteger("type"));
            ItemStack<?> stack = new ItemStack<>(event.getBot().getEconomyManager(), item);
            types.compute(type, (key, value) -> (value == null ? new StringJoiner("\n") : value).add(stack.toString()));
        }
        types.forEach((type, joiner) -> embed.addField(type.getName(), joiner.toString(), true));
        event.reply(embed.build()).queue();
    });
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Document(org.bson.Document) 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 2 with ItemType

use of com.sx4.bot.entities.economy.item.ItemType in project Sx4 by sx4-discord-bot.

the class ItemsCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member) {
    Member effectiveMember = member == null ? event.getMember() : member;
    List<Bson> usersPipeline = List.of(Aggregates.match(Filters.eq("_id", effectiveMember.getIdLong())), Aggregates.project(Projections.computed("balance", Operators.ifNull("$economy.balance", 0L))), Aggregates.addFields(new Field<>("name", "balance")));
    List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("userId", effectiveMember.getIdLong()), Filters.ne("amount", 0))), Aggregates.project(Projections.fields(Projections.computed("name", "$item.name"), Projections.computed("type", "$item.type"), Projections.include("item", "amount"))), Aggregates.sort(Sorts.descending("amount")), Aggregates.unionWith("users", usersPipeline));
    event.getMongo().aggregateItems(pipeline).whenComplete((items, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        EmbedBuilder embed = new EmbedBuilder().setAuthor("Items", null, effectiveMember.getUser().getEffectiveAvatarUrl()).setColor(effectiveMember.getColorRaw());
        if (items.isEmpty()) {
            event.replyFailure("That user does not have any items").queue();
            return;
        }
        String footerText = "If a category isn't shown it means you have no items in that category | Balance: $";
        StringBuilder footer = new StringBuilder(footerText);
        Map<ItemType, StringJoiner> types = new HashMap<>();
        for (Document item : items) {
            String name = item.getString("name");
            if (name.equals("balance")) {
                if (items.size() == 1) {
                    event.replyFailure("That user does not have any items").queue();
                    return;
                }
                footer.append(String.format("%,d", item.get("balance", 0L)));
                continue;
            }
            ItemType type = ItemType.fromId(item.getInteger("type"));
            ItemStack<?> stack = new ItemStack<>(event.getBot().getEconomyManager(), item);
            types.compute(type, (key, value) -> (value == null ? new StringJoiner("\n") : value).add(stack.toString()));
        }
        if (footer.length() == footerText.length()) {
            footer.append("0");
        }
        types.forEach((type, joiner) -> embed.addField(type.getName(), joiner.toString(), true));
        embed.setFooter(footer.toString());
        event.reply(embed.build()).queue();
    });
}
Also used : ItemType(com.sx4.bot.entities.economy.item.ItemType) Document(org.bson.Document) Bson(org.bson.conversions.Bson) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ItemStack(com.sx4.bot.entities.economy.item.ItemStack) Member(net.dv8tion.jda.api.entities.Member)

Aggregations

EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 Document (org.bson.Document)2 Bson (org.bson.conversions.Bson)2 Command (com.jockie.bot.core.command.Command)1 CommandId (com.sx4.bot.annotations.command.CommandId)1 Examples (com.sx4.bot.annotations.command.Examples)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 ItemStack (com.sx4.bot.entities.economy.item.ItemStack)1 ItemType (com.sx4.bot.entities.economy.item.ItemType)1 Member (net.dv8tion.jda.api.entities.Member)1