Search in sources :

Example 1 with ReactionWaiter

use of de.kaktushose.discord.reactionwaiter.ReactionWaiter in project Levelbot by Kaktushose.

the class ChangeDiamondsCommand method onChangeDiamonds.

@Command(name = "Diamanten tauschen", usage = "{prefix}tauschen <anzahl>", desc = "Tauscht Diamanten gegen Münzen ein. Ein Diamant ist 20 Münzen wert")
public void onChangeDiamonds(CommandEvent event, @Optional("1") long amount) {
    BotUser botUser = userService.getUserById(event.getAuthor().getIdLong());
    long diamonds = botUser.getDiamonds();
    long coins = amount * 20;
    if (diamonds == 0 || amount > diamonds) {
        event.reply(embedCache.getEmbed("missingCurrency").injectValue("currency", "Diamanten"));
        return;
    }
    event.reply(embedCache.getEmbed("confirmAction").injectValue("action", String.format("du %d Diamanten gegen %d Münzen tauschen möchtest?", amount, coins)), confirmMessage -> {
        confirmMessage.addReaction(EmoteType.THUMBSUP.unicode).and(confirmMessage.addReaction(EmoteType.THUMBSDOWN.unicode)).queue();
        ReactionWaiter reactionWaiter = new ReactionWaiter(confirmMessage, event.getMember(), EmoteType.THUMBSUP.unicode, EmoteType.THUMBSDOWN.unicode);
        reactionWaiter.onEvent(reactionEvent -> {
            if (reactionEvent.getEmote().equals(EmoteType.THUMBSUP.unicode)) {
                userService.exchangeDiamonds(botUser.getUserId(), amount);
                confirmMessage.editMessageEmbeds(embedCache.getEmbed("diamondChangeSuccess").injectValue("diamonds", amount).injectValue("coins", coins).toMessageEmbed()).queue();
            }
            reactionWaiter.stopWaiting(true);
        });
    });
}
Also used : BotUser(de.kaktushose.levelbot.database.model.BotUser) ReactionWaiter(de.kaktushose.discord.reactionwaiter.ReactionWaiter) Command(com.github.kaktushose.jda.commands.annotations.Command)

Example 2 with ReactionWaiter

use of de.kaktushose.discord.reactionwaiter.ReactionWaiter in project Levelbot by Kaktushose.

the class LeaderboardCommand method showLeaderboard.

public void showLeaderboard(CommandEvent event, Message sentMessage) {
    Pagination pagination = paginationMap.get(currencyType);
    Consumer<Message> success = message -> {
        addReactions(message, pagination.index(), pagination.pages());
        ReactionWaiter waiter = new ReactionWaiter(message, event.getMember(), BACK, FORTH, XP, COINS, DIAMONDS);
        waiter.onEvent(reactionEvent -> {
            switch(reactionEvent.getEmote()) {
                case BACK:
                    if (pagination.index() == 0) {
                        return;
                    }
                    pagination.previousPage();
                    break;
                case FORTH:
                    if (pagination.index() + 1 == pagination.pages()) {
                        return;
                    }
                    pagination.nextPage();
                    break;
                case XP:
                    if (currencyType == CurrencyType.XP) {
                        return;
                    }
                    currencyType = CurrencyType.XP;
                    break;
                case COINS:
                    if (currencyType == CurrencyType.COINS) {
                        return;
                    }
                    currencyType = CurrencyType.COINS;
                    break;
                case DIAMONDS:
                    if (currencyType == CurrencyType.DIAMONDS) {
                        return;
                    }
                    currencyType = CurrencyType.DIAMONDS;
                    break;
                default:
                    break;
            }
            showLeaderboard(event, message);
            waiter.stopWaiting(true);
        });
    };
    if (sentMessage == null) {
        event.reply(buildEmbed(pagination.getPage(), pagination.index(), pagination.pages()), success);
    } else {
        sentMessage.editMessage(buildEmbed(pagination.getPage(), pagination.index(), pagination.pages()).build()).queue(success);
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) CommandEvent(com.github.kaktushose.jda.commands.dispatching.CommandEvent) Pagination(de.kaktushose.levelbot.util.Pagination) LevelService(de.kaktushose.levelbot.database.services.LevelService) HashMap(java.util.HashMap) Inject(com.github.kaktushose.jda.commands.annotations.Inject) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Consumer(java.util.function.Consumer) List(java.util.List) Guild(net.dv8tion.jda.api.entities.Guild) EmbedCache(com.github.kaktushose.jda.commands.embeds.EmbedCache) Command(com.github.kaktushose.jda.commands.annotations.Command) ReactionWaiter(de.kaktushose.discord.reactionwaiter.ReactionWaiter) Map(java.util.Map) CommandController(com.github.kaktushose.jda.commands.annotations.CommandController) CurrencyType(de.kaktushose.levelbot.util.Pagination.CurrencyType) Pagination(de.kaktushose.levelbot.util.Pagination) Message(net.dv8tion.jda.api.entities.Message) ReactionWaiter(de.kaktushose.discord.reactionwaiter.ReactionWaiter)

Example 3 with ReactionWaiter

use of de.kaktushose.discord.reactionwaiter.ReactionWaiter 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 4 with ReactionWaiter

use of de.kaktushose.discord.reactionwaiter.ReactionWaiter 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)

Example 5 with ReactionWaiter

use of de.kaktushose.discord.reactionwaiter.ReactionWaiter in project Levelbot by Kaktushose.

the class RemoveItemCommand method onRemoveItem.

@Command(name = "Item entfernen", usage = "{prefix}remove <member>", desc = "Entfernt ein Item aus dem Besitz eines Benutzers", category = "Moderation")
public void onRemoveItem(CommandEvent event, Member member) {
    List<Item> items = shopService.getItems(member.getIdLong());
    AtomicInteger counter = new AtomicInteger(1);
    List<String> emotes = new ArrayList<>();
    EmbedBuilder builder = embedCache.getEmbed("removeItem").injectValue("user", member.getAsMention()).toEmbedBuilder();
    items.forEach(item -> {
        String emote = EmoteType.getNumber(counter.getAndIncrement()).unicode;
        emotes.add(emote);
        builder.addField(emote + ": " + item.getName(), "", false);
    });
    event.reply(builder, chooseMessage -> {
        emotes.forEach(emote -> chooseMessage.addReaction(emote).queue());
        ReactionWaiter reactionWaiter = new ReactionWaiter(chooseMessage, event.getMember(), emotes);
        reactionWaiter.onEvent(reactionEvent -> {
            Item forRemoval;
            switch(reactionEvent.getEmote()) {
                case NumberEmojis.ONE:
                    forRemoval = items.get(0);
                    break;
                case NumberEmojis.TWO:
                    forRemoval = items.get(1);
                    break;
                case NumberEmojis.THREE:
                    forRemoval = items.get(2);
                    break;
                case NumberEmojis.FOUR:
                    forRemoval = items.get(3);
                    break;
                case NumberEmojis.FIVE:
                    forRemoval = items.get(4);
                    break;
                default:
                    return;
            }
            shopService.removeItem(member.getIdLong(), forRemoval.getItemId());
            reactionWaiter.stopWaiting(false);
            chooseMessage.delete().queue();
            event.reply(embedCache.getEmbed("removeItemSuccess").injectValue("user", member.getAsMention()));
        });
    });
}
Also used : Item(de.kaktushose.levelbot.shop.data.items.Item) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ReactionWaiter(de.kaktushose.discord.reactionwaiter.ReactionWaiter) Command(com.github.kaktushose.jda.commands.annotations.Command)

Aggregations

ReactionWaiter (de.kaktushose.discord.reactionwaiter.ReactionWaiter)6 Command (com.github.kaktushose.jda.commands.annotations.Command)5 EmbedCache (com.github.kaktushose.jda.commands.embeds.EmbedCache)3 BotUser (de.kaktushose.levelbot.database.model.BotUser)3 LevelService (de.kaktushose.levelbot.database.services.LevelService)3 Item (de.kaktushose.levelbot.shop.data.items.Item)3 Consumer (java.util.function.Consumer)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 Message (net.dv8tion.jda.api.entities.Message)3 CommandController (com.github.kaktushose.jda.commands.annotations.CommandController)2 Inject (com.github.kaktushose.jda.commands.annotations.Inject)2 CommandEvent (com.github.kaktushose.jda.commands.dispatching.CommandEvent)2 Levelbot (de.kaktushose.levelbot.bot.Levelbot)2 UserService (de.kaktushose.levelbot.database.services.UserService)2 ShopService (de.kaktushose.levelbot.shop.data.ShopService)2 NumberEmojis (de.kaktushose.levelbot.util.NumberEmojis)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 TimeUnit (java.util.concurrent.TimeUnit)2