Search in sources :

Example 1 with Item

use of de.kaktushose.levelbot.shop.data.items.Item in project Levelbot by Kaktushose.

the class Levelbot method generateRankInfo.

public EmbedBuilder generateRankInfo(User target, boolean dm) {
    BotUser botUser = userService.getUserById(target.getIdLong());
    Rank currentRank = levelService.getCurrentRank(botUser.getUserId());
    Rank nextRank = levelService.getNextRank(botUser.getUserId());
    long nextRankXp = nextRank.getBound() - botUser.getXp();
    long xpGain = botUser.getXp() - botUser.getStartXp();
    long coinsGain = botUser.getCoins() - botUser.getStartCoins();
    long diamondsGain = botUser.getDiamonds() - botUser.getStartDiamonds();
    // if event is active, load another template
    String embed = eventService.isCollectEventActive(guildId) ? "eventRankInfo" : "rankInfo";
    String currentRankInfo = dm ? currentRank.getName() : String.format("<@&%d>", currentRank.getRoleId());
    String nextRankInfo = dm ? String.format("%s (noch %d XP)", nextRank.getName(), nextRankXp) : String.format("<@&%d> (noch %d XP)", nextRank.getRoleId(), nextRankXp);
    nextRankInfo = currentRank.equals(nextRank) ? "N/A" : nextRankInfo;
    String url = target.getAvatarUrl() == null ? "https://cdn.discordapp.com/embed/avatars/0.png" : target.getAvatarUrl();
    EmbedDTO embedDTO = embedCache.getEmbed(embed).injectValue("user", target.getAsMention()).injectValue("color", currentRank.getColor()).injectValue("currentRank", currentRankInfo).injectValue("nextRank", nextRankInfo).injectValue("avatarUrl", url).injectValue("xpGain", xpGain).injectValue("coinsGain", coinsGain).injectValue("diamondsGain", diamondsGain).injectFields(botUser);
    if (eventService.isCollectEventActive(guildId)) {
        CollectEvent collectEvent = eventService.getActiveCollectEvent(guildId);
        Role collectEventRole = guild.getRoleById(collectEvent.getRoleId());
        long eventPoints = botUser.getEventPoints();
        embedDTO.injectValue("eventName", collectEvent.getName()).injectValue("currencyName", collectEvent.getCurrencyName()).injectValue("currencyEmote", collectEvent.getCurrencyEmote()).injectValue("currencyPoints", eventPoints);
        if (eventPoints >= collectEvent.getItemBound()) {
            embedDTO.injectValue("eventRewards", collectEvent.getItem().getName() + "\n:blue_circle: Eventrolle " + collectEventRole.getName());
        } else if (eventPoints >= collectEvent.getRoleBound()) {
            embedDTO.injectValue("eventRewards", ":blue_circle: Eventrolle " + collectEventRole.getName());
        } else {
            embedDTO.injectValue("eventRewards", ":x: noch keine");
        }
    }
    EmbedBuilder embedBuilder = embedDTO.toEmbedBuilder();
    if (botUser.getTransactions().isEmpty()) {
        embedBuilder.addField("Keine Items in Besitz", "", false);
    }
    botUser.getTransactions().forEach(transaction -> {
        Item item = transaction.getItem();
        embedBuilder.addField(item.getName(), "noch " + item.getRemainingTimeAsDate(transaction.getBuyTime()), false);
    });
    return embedBuilder;
}
Also used : Item(de.kaktushose.levelbot.shop.data.items.Item) BotUser(de.kaktushose.levelbot.database.model.BotUser) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) EmbedDTO(com.github.kaktushose.jda.commands.embeds.EmbedDTO) CollectEvent(de.kaktushose.levelbot.database.model.CollectEvent) Rank(de.kaktushose.levelbot.database.model.Rank)

Example 2 with Item

use of de.kaktushose.levelbot.shop.data.items.Item in project Levelbot by Kaktushose.

the class Levelbot method removeItemRole.

public void removeItemRole(long userId, int itemId) {
    Item item = levelService.getItem(itemId);
    if (item.getRoleId() == -1) {
        return;
    }
    guild.removeRoleFromMember(userId, guild.getRoleById(item.getRoleId())).queue();
}
Also used : Item(de.kaktushose.levelbot.shop.data.items.Item)

Example 3 with Item

use of de.kaktushose.levelbot.shop.data.items.Item in project Levelbot by Kaktushose.

the class LevelService method setItemPrice.

public boolean setItemPrice(int itemId, int price) {
    if (!itemRepository.existsById(itemId)) {
        return false;
    }
    Item item = getItem(itemId);
    item.setPrice(price);
    itemRepository.save(item);
    return true;
}
Also used : Item(de.kaktushose.levelbot.shop.data.items.Item)

Example 4 with Item

use of de.kaktushose.levelbot.shop.data.items.Item in project Levelbot by Kaktushose.

the class ShopListener method onGuildMessageReactionAdd.

@Override
public void onGuildMessageReactionAdd(@NotNull GuildMessageReactionAddEvent event) {
    // bots should just be ignored
    if (event.getUser().isBot()) {
        return;
    }
    // must be in channel #levelsystem
    if (event.getChannel().getIdLong() != LEVEL_SYSTEM_CHANNEL_ID) {
        return;
    }
    if (levelbot.getUserService().getMutedUsers().contains(event.getUser().getIdLong())) {
        event.getReaction().removeReaction(event.getUser()).queue(null, new ErrorHandler().ignore(UNKNOWN_MESSAGE));
        return;
    }
    // indicates active purchase
    if (activeUsers.contains(event.getUser().getIdLong())) {
        event.getReaction().removeReaction(event.getUser()).queue(null, new ErrorHandler().ignore(UNKNOWN_MESSAGE));
        return;
    }
    Member member = event.getMember();
    ItemCategory itemCategory;
    switch(event.getMessageId()) {
        case PREMIUM_MESSAGE_ID:
            itemCategory = ItemCategory.PREMIUM;
            break;
        case DJ_MESSAGE_ID:
            itemCategory = ItemCategory.DJ;
            break;
        case NICKNAME_MESSAGE_ID:
            itemCategory = ItemCategory.NICKNAME;
            break;
        case COINS_BOOSTER_MESSAGE_ID:
            itemCategory = ItemCategory.COINS_BOOSTER;
            break;
        case XP_BOOSTER_MESSAGE_ID:
            itemCategory = ItemCategory.XP_BOOSTER;
            break;
        default:
            event.getReaction().removeReaction(member.getUser()).queue(null, new ErrorHandler().ignore(UNKNOWN_MESSAGE));
            return;
    }
    int variant;
    switch(event.getReactionEmote().getName()) {
        case NumberEmojis.ONE:
            variant = 0;
            break;
        case NumberEmojis.TWO:
            variant = 1;
            break;
        case NumberEmojis.THREE:
            variant = 2;
            break;
        default:
            event.getReaction().removeReaction(member.getUser()).queue();
            return;
    }
    if ((itemCategory == ItemCategory.COINS_BOOSTER || itemCategory == ItemCategory.XP_BOOSTER) && variant == 2) {
        event.getReaction().removeReaction(member.getUser()).queue();
        return;
    }
    Item item = levelService.getItemsByCategoryId(itemCategory.id).get(variant);
    BotUser botUser = userService.getUserById(member.getIdLong());
    String fail = null;
    if (shopService.hasItemOfCategory(member.getIdLong(), item.getCategoryId())) {
        fail = "Du besitzt dieses Item bereits!";
    }
    if (botUser.getCoins() < item.getPrice()) {
        fail = "Du hast nicht genug Münzen!";
    }
    TextChannel channel = event.getChannel();
    Consumer<Message> delete = success -> success.delete().queueAfter(30, TimeUnit.SECONDS, null, new ErrorHandler().ignore(UNKNOWN_MESSAGE));
    MessageBuilder builder = new MessageBuilder().append(member.getAsMention());
    if (fail != null) {
        // transaction failed
        channel.sendMessage(builder.setEmbeds(embedCache.getEmbed("shopError").injectValue("message", fail).toMessageEmbed()).build()).queue(delete);
    } else {
        activeUsers.add(member.getIdLong());
        // confirm transaction
        channel.sendMessage(builder.setEmbeds(embedCache.getEmbed("shopConfirm").injectValue("item", item.getName()).injectValue("price", item.getPrice()).toMessageEmbed()).build()).queue(confirmMessage -> {
            // wait for reactions
            // delete confirm message after 30 secs if nothing happens
            confirmMessage.delete().queueAfter(30, TimeUnit.SECONDS, success -> activeUsers.remove(member.getIdLong()), new ErrorHandler().ignore(UNKNOWN_MESSAGE));
            confirmMessage.addReaction(CONFIRM).and(confirmMessage.addReaction(CANCEL)).queue();
            ReactionWaiter waiter = new ReactionWaiter(confirmMessage, event.getMember(), CONFIRM, CANCEL);
            waiter.onEvent(reactionEvent -> {
                // on reaction confirm or cancel emoji
                confirmMessage.delete().queue();
                if (reactionEvent.getEmote().equals(CONFIRM)) {
                    shopService.buyItem(botUser.getUserId(), item.getItemId());
                    // successful transaction
                    channel.sendMessageEmbeds(embedCache.getEmbed("shopSuccess").injectValue("item", item.getName()).injectValue("days", TimeUnit.MILLISECONDS.toDays(item.getDuration())).toMessageEmbed()).queue(delete);
                    levelbot.getLogChannel().sendMessageEmbeds(embedCache.getEmbed("buyLog").injectValue("user", member.getAsMention()).injectValue("item", item.getName()).toMessageEmbed()).queue();
                }
                activeUsers.remove(member.getIdLong());
                waiter.stopWaiting(false);
            });
        });
    }
    event.getReaction().removeReaction(member.getUser()).queue();
}
Also used : Message(net.dv8tion.jda.api.entities.Message) BotUser(de.kaktushose.levelbot.database.model.BotUser) LevelService(de.kaktushose.levelbot.database.services.LevelService) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) Set(java.util.Set) UNKNOWN_MESSAGE(net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_MESSAGE) Levelbot(de.kaktushose.levelbot.bot.Levelbot) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) UserService(de.kaktushose.levelbot.database.services.UserService) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ShopService(de.kaktushose.levelbot.shop.data.ShopService) EmbedCache(com.github.kaktushose.jda.commands.embeds.EmbedCache) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) ReactionWaiter(de.kaktushose.discord.reactionwaiter.ReactionWaiter) GuildMessageReactionAddEvent(net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent) Item(de.kaktushose.levelbot.shop.data.items.Item) NotNull(org.jetbrains.annotations.NotNull) NumberEmojis(de.kaktushose.levelbot.util.NumberEmojis) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) BotUser(de.kaktushose.levelbot.database.model.BotUser) Message(net.dv8tion.jda.api.entities.Message) ReactionWaiter(de.kaktushose.discord.reactionwaiter.ReactionWaiter) Item(de.kaktushose.levelbot.shop.data.items.Item) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Member(net.dv8tion.jda.api.entities.Member)

Example 5 with Item

use of de.kaktushose.levelbot.shop.data.items.Item in project Levelbot by Kaktushose.

the class AddItemCommand method sendSpecificShop.

private void sendSpecificShop(CommandEvent event, Message shopMessage, Member target, ItemCategory itemCategory) {
    EmbedBuilder embedBuilder = specificShops.get(itemCategory);
    List<Item> items = levelService.getItemsByCategoryId(itemCategory.id);
    long amount = items.stream().filter(Item::isVisible).count();
    Consumer<Message> success = message -> {
        message.clearReactions().queue();
        for (int i = 0; i < amount; i++) {
            message.addReaction(EmoteType.getNumber(i + 1).unicode).queue();
        }
        message.addReaction(BACK).and(message.addReaction(CANCEL)).queue();
        ReactionWaiter reactionWaiter = new ReactionWaiter(message, event.getMember(), BACK, CANCEL, NumberEmojis.ONE, NumberEmojis.TWO, NumberEmojis.THREE, NumberEmojis.FOUR);
        reactionWaiter.onEvent(reactionEvent -> {
            int variant = 0;
            switch(reactionEvent.getEmote()) {
                case NumberEmojis.ONE:
                    variant = 1;
                    break;
                case NumberEmojis.TWO:
                    variant = 2;
                    break;
                case NumberEmojis.THREE:
                    variant = 3;
                    break;
                case NumberEmojis.FOUR:
                    variant = 4;
                    break;
                case BACK:
                    sendDefaultShop(event, message, target);
                    reactionWaiter.stopWaiting(false);
                    return;
                case CANCEL:
                    message.delete().and(event.getMessage().delete()).queue();
                    reactionWaiter.stopWaiting(false);
                    return;
            }
            if (amount < variant) {
                return;
            }
            Item item = items.get(variant - 1);
            Optional<String> buyResult = addItem(target, item);
            if (buyResult.isEmpty()) {
                message.editMessage(embedCache.getEmbed("addItemSuccess").injectValue("user", target.getAsMention()).injectValue("item", item.getName()).injectValue("days", TimeUnit.MILLISECONDS.toDays(item.getDuration())).toMessageEmbed()).and(message.clearReactions()).queue();
            } else {
                message.editMessage(embedCache.getEmbed("shopError").injectValue("message", buyResult.get()).toMessageEmbed()).queue(errorMessage -> {
                    errorMessage.clearReactions().queue();
                    errorMessage.addReaction(BACK).and(errorMessage.addReaction(CANCEL)).queue();
                    ReactionWaiter waiter = new ReactionWaiter(message, event.getMember(), BACK, CANCEL);
                    waiter.onEvent(errorMessageEvent -> {
                        if (errorMessageEvent.getEmote().equals(BACK)) {
                            sendDefaultShop(event, message, target);
                        } else {
                            message.delete().and(event.getMessage().delete()).queue();
                        }
                        waiter.stopWaiting(false);
                    });
                });
            }
            reactionWaiter.stopWaiting(false);
        });
    };
    if (shopMessage == null) {
        event.reply(embedBuilder, success);
    } else {
        shopMessage.editMessage(embedBuilder.build()).queue(success);
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) CommandEvent(com.github.kaktushose.jda.commands.dispatching.CommandEvent) BotUser(de.kaktushose.levelbot.database.model.BotUser) EmoteType(de.kaktushose.discord.reactionwaiter.EmoteType) LevelService(de.kaktushose.levelbot.database.services.LevelService) Levelbot(de.kaktushose.levelbot.bot.Levelbot) HashMap(java.util.HashMap) Inject(com.github.kaktushose.jda.commands.annotations.Inject) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Member(net.dv8tion.jda.api.entities.Member) UserService(de.kaktushose.levelbot.database.services.UserService) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) ShopService(de.kaktushose.levelbot.shop.data.ShopService) EmbedCache(com.github.kaktushose.jda.commands.embeds.EmbedCache) Command(com.github.kaktushose.jda.commands.annotations.Command) ReactionWaiter(de.kaktushose.discord.reactionwaiter.ReactionWaiter) Permission(com.github.kaktushose.jda.commands.annotations.Permission) Map(java.util.Map) Optional(java.util.Optional) Item(de.kaktushose.levelbot.shop.data.items.Item) CommandController(com.github.kaktushose.jda.commands.annotations.CommandController) NumberEmojis(de.kaktushose.levelbot.util.NumberEmojis) Item(de.kaktushose.levelbot.shop.data.items.Item) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Message(net.dv8tion.jda.api.entities.Message) Optional(java.util.Optional) ReactionWaiter(de.kaktushose.discord.reactionwaiter.ReactionWaiter)

Aggregations

Item (de.kaktushose.levelbot.shop.data.items.Item)9 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)5 BotUser (de.kaktushose.levelbot.database.model.BotUser)4 EmbedCache (com.github.kaktushose.jda.commands.embeds.EmbedCache)3 ReactionWaiter (de.kaktushose.discord.reactionwaiter.ReactionWaiter)3 ShopService (de.kaktushose.levelbot.shop.data.ShopService)3 TimeUnit (java.util.concurrent.TimeUnit)3 Command (com.github.kaktushose.jda.commands.annotations.Command)2 EmbedDTO (com.github.kaktushose.jda.commands.embeds.EmbedDTO)2 Levelbot (de.kaktushose.levelbot.bot.Levelbot)2 CollectEvent (de.kaktushose.levelbot.database.model.CollectEvent)2 Rank (de.kaktushose.levelbot.database.model.Rank)2 LevelService (de.kaktushose.levelbot.database.services.LevelService)2 UserService (de.kaktushose.levelbot.database.services.UserService)2 NumberEmojis (de.kaktushose.levelbot.util.NumberEmojis)2 List (java.util.List)2 Consumer (java.util.function.Consumer)2 Member (net.dv8tion.jda.api.entities.Member)2 Message (net.dv8tion.jda.api.entities.Message)2 ErrorHandler (net.dv8tion.jda.api.exceptions.ErrorHandler)2