Search in sources :

Example 6 with ErrorHandler

use of net.dv8tion.jda.api.exceptions.ErrorHandler 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));
                }
            });
        }
    }
}
Also used : MessageData(ml.duncte123.skybot.objects.discord.MessageData) MessageBulkDeleteEvent(net.dv8tion.jda.api.events.message.MessageBulkDeleteEvent) EmbedUtils(me.duncte123.botcommons.messaging.EmbedUtils) StringUtils(me.duncte123.botcommons.StringUtils) Permission(net.dv8tion.jda.api.Permission) CommandCategory(ml.duncte123.skybot.objects.command.CommandCategory) PerspectiveApi(ml.duncte123.skybot.utils.PerspectiveApi) UNKNOWN_MESSAGE(net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_MESSAGE) SpamFilter(ml.duncte123.skybot.utils.SpamFilter) Matcher(java.util.regex.Matcher) GuildSettingsUtils(ml.duncte123.skybot.utils.GuildSettingsUtils) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent) GuildSetting(com.dunctebot.models.settings.GuildSetting) GuildMessageUpdateEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent) Variables(ml.duncte123.skybot.Variables) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) UnknownUser(ml.duncte123.skybot.objects.user.UnknownUser) Pattern(java.util.regex.Pattern) MISSING_PERMISSIONS(net.dv8tion.jda.api.requests.ErrorResponse.MISSING_PERMISSIONS) ICommand(ml.duncte123.skybot.objects.command.ICommand) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) BotCommons(me.duncte123.botcommons.BotCommons) java.util(java.util) JDA(net.dv8tion.jda.api.JDA) MarkdownSanitizer(net.dv8tion.jda.api.utils.MarkdownSanitizer) CustomCommand(ml.duncte123.skybot.objects.command.custom.CustomCommand) GuildMessageDeleteEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent) MessageUtils(me.duncte123.botcommons.messaging.MessageUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Sentry(io.sentry.Sentry) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) GenericGuildMessageEvent(net.dv8tion.jda.api.events.message.guild.GenericGuildMessageEvent) CommandUtils.isGuildPatron(ml.duncte123.skybot.utils.CommandUtils.isGuildPatron) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) MessageUtils.sendMsg(me.duncte123.botcommons.messaging.MessageUtils.sendMsg) CommandContext(ml.duncte123.skybot.objects.command.CommandContext) CommandUtils.isDev(ml.duncte123.skybot.utils.CommandUtils.isDev) ModerationUtils.modLog(ml.duncte123.skybot.utils.ModerationUtils.modLog) CommandManager(ml.duncte123.skybot.CommandManager) DunctebotGuild(ml.duncte123.skybot.entities.jda.DunctebotGuild) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AirUtils.setJDAContext(ml.duncte123.skybot.utils.AirUtils.setJDAContext) Triple(kotlin.Triple) Settings(ml.duncte123.skybot.Settings) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) RedisConnection(ml.duncte123.skybot.database.RedisConnection) DateTimeFormatter(java.time.format.DateTimeFormatter) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) Matcher(java.util.regex.Matcher)

Example 7 with ErrorHandler

use of net.dv8tion.jda.api.exceptions.ErrorHandler in project Sheikh-Pepe by ICAZ117.

the class Verify method canDM.

public boolean canDM(User user, GuildMessageReceivedEvent event) {
    canDM = true;
    inverted = false;
    user.openPrivateChannel().flatMap(channel -> channel.sendMessage("Assalamu3alaykum Warahmatullahi Wabarakatuh!\n")).delay(Duration.ofSeconds(30)).flatMap(// delete after 30 seconds
    Message::delete).queue(null, new ErrorHandler().ignore(// if delete fails that's fine
    ErrorResponse.UNKNOWN_MESSAGE).handle(// Fallback handling for blocked messages
    ErrorResponse.CANNOT_SEND_TO_USER, (e) -> invert()));
    long time = System.currentTimeMillis();
    while (System.currentTimeMillis() <= time + 1000) {
    }
    // System.out.printf("AFTER LOOP\ncanDM: %b\ntime: %d\n", canDM, System.currentTimeMillis());
    return canDM;
}
Also used : GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) java.util(java.util) Duration(java.time.Duration) Submission(Dependencies.Submission) Main(Main.Main) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) PrivateMessageReceivedEvent(net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) Logger(java.util.logging.Logger) Level(java.util.logging.Level) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler)

Example 8 with ErrorHandler

use of net.dv8tion.jda.api.exceptions.ErrorHandler in project SkyBot by DuncteBot.

the class MessageListener method checkSwearFilter.

private boolean checkSwearFilter(Message messageToCheck, GenericGuildMessageEvent event, DunctebotGuild guild) {
    final GuildSetting settings = guild.getSettings();
    if (settings.isEnableSwearFilter() && !topicContains(event.getChannel(), PROFANITY_DISABLE)) {
        final float score = PerspectiveApi.checkSwearFilter(messageToCheck.getContentRaw(), event.getChannel().getId(), variables.getConfig().apis.googl, settings.getFilterType(), variables.getJackson());
        // if the score is less than our target value it's not swearing
        if (score < settings.getAiSensitivity()) {
            return false;
        }
        final String display = messageToCheck.getContentDisplay();
        messageToCheck.delete().reason("Blocked for swearing: " + display).queue(null, new ErrorHandler().ignore(UNKNOWN_MESSAGE, MISSING_PERMISSIONS));
        sendMsg(new MessageConfig.Builder().setChannel(event.getChannel()).setMessageFormat(// TODO: allow patrons to customise this message
        "Hello there, %s please do not use cursive language within this server.", messageToCheck.getAuthor().getAsMention()).setSuccessAction((m) -> m.delete().queueAfter(5, TimeUnit.SECONDS, null, new ErrorHandler().ignore(UNKNOWN_MESSAGE, MISSING_PERMISSIONS))).build());
        modLog(String.format("Message with score %.2f by %#s deleted in %s for profanity, message content was:```\n%s```", score, messageToCheck.getAuthor(), event.getChannel().getAsMention(), // Just to be safe
        StringUtils.abbreviate(display, Message.MAX_CONTENT_LENGTH - 150)), guild);
        return true;
    }
    return false;
}
Also used : ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) GuildSetting(com.dunctebot.models.settings.GuildSetting)

Example 9 with ErrorHandler

use of net.dv8tion.jda.api.exceptions.ErrorHandler in project Levelbot by Kaktushose.

the class Levelbot method dmRankInfo.

public void dmRankInfo() {
    userService.getUsersByDailyEnabled().forEach(botUser -> {
        User user = jda.getUserById(botUser.getUserId());
        user.openPrivateChannel().flatMap(privateChannel -> privateChannel.sendMessageEmbeds(generateRankInfo(user, true).build())).queue(null, new ErrorHandler().ignore(ErrorResponse.CANNOT_SEND_TO_USER));
    });
}
Also used : LoginException(javax.security.auth.login.LoginException) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) BotUser(de.kaktushose.levelbot.database.model.BotUser) JDA(net.dv8tion.jda.api.JDA) Date(java.util.Date) ReactionListener(de.kaktushose.discord.reactionwaiter.ReactionListener) LoggerFactory(org.slf4j.LoggerFactory) Statistics(de.kaktushose.levelbot.util.Statistics) SimpleDateFormat(java.text.SimpleDateFormat) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) de.kaktushose.levelbot.listener(de.kaktushose.levelbot.listener) GatewayIntent(net.dv8tion.jda.api.requests.GatewayIntent) ShopService(de.kaktushose.levelbot.shop.data.ShopService) Calendar(java.util.Calendar) CollectEvent(de.kaktushose.levelbot.database.model.CollectEvent) JDABuilder(net.dv8tion.jda.api.JDABuilder) Bootstrapper(de.kaktushose.levelbot.Bootstrapper) CacheFlag(net.dv8tion.jda.api.utils.cache.CacheFlag) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) OnlineStatus(net.dv8tion.jda.api.OnlineStatus) MemberCachePolicy(net.dv8tion.jda.api.utils.MemberCachePolicy) ShopListener(de.kaktushose.levelbot.shop.ShopListener) Logger(org.slf4j.Logger) JsonErrorMessageFactory(com.github.kaktushose.jda.commands.embeds.error.JsonErrorMessageFactory) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) de.kaktushose.levelbot.database.services(de.kaktushose.levelbot.database.services) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) EmbedDTO(com.github.kaktushose.jda.commands.embeds.EmbedDTO) List(java.util.List) EmbedCache(com.github.kaktushose.jda.commands.embeds.EmbedCache) Rank(de.kaktushose.levelbot.database.model.Rank) JDACommands(com.github.kaktushose.jda.commands.JDACommands) JsonHelpMessageFactory(com.github.kaktushose.jda.commands.embeds.help.JsonHelpMessageFactory) WelcomeEmbedsCommand(de.kaktushose.levelbot.commands.moderation.WelcomeEmbedsCommand) Item(de.kaktushose.levelbot.shop.data.items.Item) ChunkingFilter(net.dv8tion.jda.api.utils.ChunkingFilter) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) BotUser(de.kaktushose.levelbot.database.model.BotUser)

Example 10 with ErrorHandler

use of net.dv8tion.jda.api.exceptions.ErrorHandler in project Levelbot by Kaktushose.

the class BulkDeleteCommand method onBulkDeleteMessages.

@Command(name = "Nachrichten löschen", usage = "{prefix}delete <amount>", desc = "Löscht die angegebene Zahl von Nachrichten aus einem Channel")
public void onBulkDeleteMessages(CommandEvent event, @Min(value = 1, message = "Muss mindestens eine Nachricht löschen") @Max(value = 100, message = "Kann maximal 100 Nachrichten gleichzeitig löschen") int amount) {
    // amount + 1 so we delete the command message as well
    // complete aka blocking to make sure that the success message is sent correctly
    List<Message> messageHistory = event.getChannel().getHistory().retrievePast(amount + 1).complete();
    messageHistory.forEach(message -> message.delete().complete());
    event.reply(embedCache.getEmbed("bulkDeleteSuccess").injectValue("amount", amount), message -> message.delete().queueAfter(10, TimeUnit.SECONDS, null, new ErrorHandler().ignore(ErrorResponse.UNKNOWN_MESSAGE)));
// delete success message after 10 secs
}
Also used : ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) Message(net.dv8tion.jda.api.entities.Message) Command(com.github.kaktushose.jda.commands.annotations.Command)

Aggregations

ErrorHandler (net.dv8tion.jda.api.exceptions.ErrorHandler)20 ErrorResponse (net.dv8tion.jda.api.requests.ErrorResponse)8 Instant (java.time.Instant)6 MessageConfig (me.duncte123.botcommons.messaging.MessageConfig)6 net.dv8tion.jda.api.entities (net.dv8tion.jda.api.entities)6 List (java.util.List)5 TimeUnit (java.util.concurrent.TimeUnit)5 Collectors (java.util.stream.Collectors)5 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)5 JDA (net.dv8tion.jda.api.JDA)5 Message (net.dv8tion.jda.api.entities.Message)5 TextChannel (net.dv8tion.jda.api.entities.TextChannel)5 NotNull (org.jetbrains.annotations.NotNull)5 GuildSetting (com.dunctebot.models.settings.GuildSetting)4 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 User (net.dv8tion.jda.api.entities.User)4 ListenerAdapter (net.dv8tion.jda.api.hooks.ListenerAdapter)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4