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