use of net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_MESSAGE in project SkyBot by duncte123.
the class PurgeUserCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
final List<Member> mentionedMembers = ctx.getMentionedArg(0);
if (mentionedMembers.isEmpty()) {
sendMsg(ctx, "I could not find any members with that name");
return;
}
final Member targetMember = mentionedMembers.get(0);
final User targetUser = targetMember.getUser();
final TextChannel channel = ctx.getChannel();
final Message message = ctx.getMessage();
channel.getIterableHistory().takeAsync(DEL_COUNT).thenApplyAsync((msgs) -> msgs.stream().filter((msg) -> msg.getAuthor().equals(targetUser)).collect(Collectors.toList())).thenApplyAsync((msgs) -> {
final List<CompletableFuture<Void>> futures = channel.purgeMessages(msgs);
sendSuccess(message);
try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get();
} catch (InterruptedException | ExecutionException e) {
Sentry.captureException(e);
return 0;
}
return msgs.size();
}).exceptionally((thr) -> {
String cause = "";
if (thr.getCause() != null) {
cause = " caused by: " + thr.getCause().getMessage();
}
sendMsg(ctx, "ERROR: " + thr.getMessage() + cause);
sendError(message);
return 0;
}).whenCompleteAsync((count, thr) -> {
sendMsg(DELETE_MESSAGE_AFTER_SECONDS.apply(5L).setChannel(ctx.getChannel()).setMessageFormat("Removed %d messages! (this message will auto delete in 5 seconds)", count).build());
channel.deleteMessageById(message.getIdLong()).queue(null, ignore(UNKNOWN_MESSAGE));
modLog(String.format("%d messages by %#s removed in %s by %#s", count, targetUser, channel, ctx.getAuthor()), ctx.getGuild());
});
}
use of net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_MESSAGE in project SkyBot by duncte123.
the class MessageListener method checkMessageForInvites.
// / <editor-fold desc="auto moderation" defaultstate="collapsed">
private void checkMessageForInvites(Guild guild, GuildMessageReceivedEvent event, GuildSetting settings, String raw) {
if (settings.isFilterInvites() && guild.getSelfMember().hasPermission(Permission.MANAGE_SERVER)) {
final Matcher matcher = Message.INVITE_PATTERN.matcher(raw);
if (matcher.find()) {
// Get the invite Id from the message
final String inviteID = matcher.group(matcher.groupCount());
// Prohibiting failure because the bot is currently banned from the other guild.
guild.retrieveInvites().queue((invites) -> {
// Check if the invite is for this guild, if it is not delete the message
if (invites.stream().noneMatch((invite) -> invite.getCode().equals(inviteID))) {
event.getMessage().delete().reason("Contained unauthorized invite.").queue((it) -> sendMsg(MessageConfig.Builder.fromEvent(event).setMessage(event.getAuthor().getAsMention() + ", please don't post invite links here.").setSuccessAction(m -> m.delete().queueAfter(4, TimeUnit.SECONDS)).build()), new ErrorHandler().ignore(UNKNOWN_MESSAGE, MISSING_PERMISSIONS));
}
});
}
}
}
use of net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_MESSAGE in project SkyBot by DuncteBot.
the class PurgeUserCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
final List<Member> mentionedMembers = ctx.getMentionedArg(0);
if (mentionedMembers.isEmpty()) {
sendMsg(ctx, "I could not find any members with that name");
return;
}
final Member targetMember = mentionedMembers.get(0);
final User targetUser = targetMember.getUser();
final TextChannel channel = ctx.getChannel();
final Message message = ctx.getMessage();
channel.getIterableHistory().takeAsync(DEL_COUNT).thenApplyAsync((msgs) -> msgs.stream().filter((msg) -> msg.getAuthor().equals(targetUser)).collect(Collectors.toList())).thenApplyAsync((msgs) -> {
final List<CompletableFuture<Void>> futures = channel.purgeMessages(msgs);
sendSuccess(message);
try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get();
} catch (InterruptedException | ExecutionException e) {
Sentry.captureException(e);
return 0;
}
return msgs.size();
}).exceptionally((thr) -> {
String cause = "";
if (thr.getCause() != null) {
cause = " caused by: " + thr.getCause().getMessage();
}
sendMsg(ctx, "ERROR: " + thr.getMessage() + cause);
sendError(message);
return 0;
}).whenCompleteAsync((count, thr) -> {
sendMsg(DELETE_MESSAGE_AFTER_SECONDS.apply(5L).setChannel(ctx.getChannel()).setMessageFormat("Removed %d messages! (this message will auto delete in 5 seconds)", count).build());
channel.deleteMessageById(message.getIdLong()).queue(null, ignore(UNKNOWN_MESSAGE));
modLog(String.format("%d messages by %#s removed in %s by %#s", count, targetUser, channel, ctx.getAuthor()), ctx.getGuild());
});
}
use of net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_MESSAGE in project SkyBot by DuncteBot.
the class MessageListener method checkMessageForInvites.
// / <editor-fold desc="auto moderation" defaultstate="collapsed">
private void checkMessageForInvites(Guild guild, GuildMessageReceivedEvent event, GuildSetting settings, String raw) {
if (settings.isFilterInvites() && guild.getSelfMember().hasPermission(Permission.MANAGE_SERVER)) {
final Matcher matcher = Message.INVITE_PATTERN.matcher(raw);
if (matcher.find()) {
// Get the invite Id from the message
final String inviteID = matcher.group(matcher.groupCount());
// Prohibiting failure because the bot is currently banned from the other guild.
guild.retrieveInvites().queue((invites) -> {
// Check if the invite is for this guild, if it is not delete the message
if (invites.stream().noneMatch((invite) -> invite.getCode().equals(inviteID))) {
event.getMessage().delete().reason("Contained unauthorized invite.").queue((it) -> sendMsg(MessageConfig.Builder.fromEvent(event).setMessage(event.getAuthor().getAsMention() + ", please don't post invite links here.").setSuccessAction(m -> m.delete().queueAfter(4, TimeUnit.SECONDS)).build()), new ErrorHandler().ignore(UNKNOWN_MESSAGE, MISSING_PERMISSIONS));
}
});
}
}
}
use of net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_MESSAGE 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();
}
Aggregations