Search in sources :

Example 1 with CommandEvent

use of com.github.kaktushose.jda.commands.dispatching.CommandEvent 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 2 with CommandEvent

use of com.github.kaktushose.jda.commands.dispatching.CommandEvent 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

Command (com.github.kaktushose.jda.commands.annotations.Command)2 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 EmbedCache (com.github.kaktushose.jda.commands.embeds.EmbedCache)2 ReactionWaiter (de.kaktushose.discord.reactionwaiter.ReactionWaiter)2 LevelService (de.kaktushose.levelbot.database.services.LevelService)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Consumer (java.util.function.Consumer)2 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 Message (net.dv8tion.jda.api.entities.Message)2 Permission (com.github.kaktushose.jda.commands.annotations.Permission)1 EmoteType (de.kaktushose.discord.reactionwaiter.EmoteType)1 Levelbot (de.kaktushose.levelbot.bot.Levelbot)1 BotUser (de.kaktushose.levelbot.database.model.BotUser)1 UserService (de.kaktushose.levelbot.database.services.UserService)1 ShopService (de.kaktushose.levelbot.shop.data.ShopService)1 Item (de.kaktushose.levelbot.shop.data.items.Item)1