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);
});
});
}
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);
}
}
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();
}
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);
}
}
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()));
});
});
}
Aggregations