use of net.dv8tion.jda.api.exceptions.ErrorHandler 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 net.dv8tion.jda.api.exceptions.ErrorHandler in project Levelbot by Kaktushose.
the class Levelbot method sendItemExpiredInformation.
public void sendItemExpiredInformation(long userId, int itemId, long buyTime) {
User user = jda.getUserById(userId);
Item item = levelService.getItem(itemId);
EmbedBuilder embed = embedCache.getEmbed("itemExpired").injectValue("item", item.getName()).injectValue("date", new SimpleDateFormat("dd.MM.yyyy HH:mm").format(new Date(buyTime))).toEmbedBuilder();
user.openPrivateChannel().flatMap(privateChannel -> privateChannel.sendMessageEmbeds(embed.build())).queue(null, new ErrorHandler().handle(ErrorResponse.CANNOT_SEND_TO_USER, exception -> {
TextChannel channel = getBotChannel();
channel.sendMessage(user.getAsMention()).and(channel.sendMessageEmbeds(embed.build())).queue();
}));
}
use of net.dv8tion.jda.api.exceptions.ErrorHandler in project cds by iamysko.
the class MessageListener method validateBanRequest.
/**
* Validate a ban request by checking if the:
* Correct format is used
* User ID exists
* User ID doesn't belong to a staff member
*
* @param message the message
*/
private void validateBanRequest(final Message message) {
if (!message.getContentRaw().matches("(?si)^;(?:force)?ban\\s\\d{17,19}\\s.+$")) {
message.reply("Incorrect ban request format. Please use `;ban <user id> <reason>`").mentionRepliedUser(true).setActionRow(Button.primary("DeleteMessage", "Hide Alert")).queue();
} else {
Pattern regexPattern = Pattern.compile("(?si)^;(?:force)?ban\\s(\\d{17,19})\\s.+$");
Matcher matchedResults = regexPattern.matcher(message.getContentRaw());
matchedResults.find();
message.getJDA().getGuildById(Properties.GUILD_ROBLOX_DISCORD_ID).retrieveMemberById(matchedResults.group(1)).queue(targetedMember -> {
if (RoleUtils.isAnyRole(targetedMember, RoleUtils.ROLE_LEAD, RoleUtils.ROLE_SERVER_MANAGER, RoleUtils.ROLE_MODERATOR, RoleUtils.ROLE_SENIOR_MODERATOR, RoleUtils.ROLE_BOT, RoleUtils.ROLE_TRIAL_MODERATOR)) {
message.reply("The provided ID belongs to a staff member.").mentionRepliedUser(true).setActionRow(Button.primary("DeleteMessage", "Hide Alert")).queue();
}
}, new ErrorHandler().ignore(ErrorResponse.UNKNOWN_MEMBER).handle(ErrorResponse.UNKNOWN_USER, (error) -> message.reply("The provided ID is not a valid user ID").mentionRepliedUser(true).setActionRow(Button.primary("DeleteMessage", "Hide Alert")).queue()));
}
}
use of net.dv8tion.jda.api.exceptions.ErrorHandler in project Robertify-Bot by bombies.
the class ReminderScheduler method dmReminder.
private void dmReminder(Guild guild, long user, String reminder) {
User u = Robertify.shardManager.retrieveUserById(user).complete();
u.openPrivateChannel().queue(channel -> channel.sendMessageEmbeds(RobertifyEmbedUtils.embedMessageWithTitle(guild, "Reminder", reminder).setFooter("Reminder from: " + guild.getName()).setTimestamp(Instant.now()).build()).queue(null, new ErrorHandler().handle(ErrorResponse.CANNOT_SEND_TO_USER, ignored -> {
})));
}
use of net.dv8tion.jda.api.exceptions.ErrorHandler in project Robertify-Bot by bombies.
the class UnbanCommand method handleUnban.
private EmbedBuilder handleUnban(Guild guild, User user) {
if (!new GuildConfig().isBannedUser(guild.getIdLong(), user.getIdLong()))
return RobertifyEmbedUtils.embedMessage(guild, "This user is not banned.");
new GuildConfig().unbanUser(guild.getIdLong(), user.getIdLong());
user.openPrivateChannel().queue(channel -> channel.sendMessageEmbeds(RobertifyEmbedUtils.embedMessage(guild, "You have been unbanned from **" + guild.getName() + "**").build()).queue(success -> {
}, new ErrorHandler().handle(ErrorResponse.CANNOT_SEND_TO_USER, e -> Listener.logger.warn("Was not able to send an unban message to " + user.getAsTag() + "(" + user.getIdLong() + ")"))));
return RobertifyEmbedUtils.embedMessage(guild, "You have unbanned " + user.getAsMention());
}
Aggregations