Search in sources :

Example 1 with ErrorResponse

use of net.dv8tion.jda.api.requests.ErrorResponse in project SkyBot by DuncteBot.

the class AirUtils method handleExpiredReminders.

public static void handleExpiredReminders(List<Reminder> reminders, DatabaseAdapter adapter) {
    // Get the shardManager and a list of ints to purge the ids for
    final ShardManager shardManager = SkyBot.getInstance().getShardManager();
    final List<Integer> toPurge = new ArrayList<>();
    for (final Reminder reminder : reminders) {
        // The reminder message template
        final String message = String.format("%s you asked me to remind you about \"%s\"", TimeFormat.RELATIVE.format(reminder.getCreate_date()), reminder.getReminder().trim());
        // If we have a channel send the message to that
        if (reminder.getIn_channel()) {
            final long channelId = reminder.getChannel_id();
            final TextChannel channel = shardManager.getTextChannelById(channelId);
            // skipping the continue statement makes sure that we roll into the dm part of this
            if (channel != null) {
                // Add the reminder to the list of the reminders to purge
                toPurge.add(reminder.getId());
                sendMsg(new MessageConfig.Builder().setChannel(channel).setMessage(String.format("<@%s>, %s", reminder.getUser_id(), message)).replyTo(reminder.getMessage_id()).build());
                // go to the next one and don't run the user code if a channel was found
                continue;
            }
        }
        try {
            Objects.requireNonNull(shardManager.getShardById(0)).openPrivateChannelById(reminder.getUser_id()).flatMap((c) -> c.sendMessage(message + "\n" + reminder.getJumpUrl())).complete();
            toPurge.add(reminder.getId());
        } catch (ErrorResponseException errorResponseEx) {
            final ErrorResponse errorResponse = errorResponseEx.getErrorResponse();
            if (// The account probably got deleted or something
            errorResponse == ErrorResponse.UNKNOWN_USER || // we cannot dm this user (has dms blocked?)
            errorResponse == ErrorResponse.CANNOT_SEND_TO_USER) {
                toPurge.add(reminder.getId());
            }
        } catch (Exception e) {
            Sentry.captureException(e);
        }
    }
    // get a date that is 2 days in the future
    final OffsetDateTime plusTwoDays = OffsetDateTime.now(ZoneOffset.UTC).plus(2L, ChronoUnit.DAYS);
    // Remove any reminders that have not been removed after 2 days
    final List<Integer> extraRemoval = reminders.stream().filter((reminder) -> reminder.getReminder_date().isAfter(plusTwoDays)).map(Reminder::getId).collect(Collectors.toList());
    toPurge.addAll(extraRemoval);
    if (!toPurge.isEmpty()) {
        adapter.purgeRemindersSync(toPurge);
    }
}
Also used : Arrays(java.util.Arrays) JDA(net.dv8tion.jda.api.JDA) StringUtils(me.duncte123.botcommons.StringUtils) ParsedDuration(me.duncte123.durationparser.ParsedDuration) Permission(net.dv8tion.jda.api.Permission) JSONRequestBody(me.duncte123.botcommons.web.requests.JSONRequestBody) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FinderUtil(com.jagrosh.jdautilities.commons.utils.FinderUtil) User(net.dv8tion.jda.api.entities.User) TemporalAccessor(java.time.temporal.TemporalAccessor) ArrayList(java.util.ArrayList) WebParserUtils.toJSONObject(me.duncte123.botcommons.web.WebParserUtils.toJSONObject) Guild(net.dv8tion.jda.api.entities.Guild) JDAImpl(net.dv8tion.jda.internal.JDAImpl) LavalinkManager(fredboat.audio.player.LavalinkManager) ZoneOffset(java.time.ZoneOffset) Sentry(io.sentry.Sentry) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) MessageUtils.sendMsg(me.duncte123.botcommons.messaging.MessageUtils.sendMsg) CommandContext(ml.duncte123.skybot.objects.command.CommandContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TimeFormat(net.dv8tion.jda.api.utils.TimeFormat) WebParserUtils(me.duncte123.botcommons.web.WebParserUtils) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) ChronoUnit(java.time.temporal.ChronoUnit) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) SkyBot(ml.duncte123.skybot.SkyBot) DatabaseAdapter(ml.duncte123.skybot.adapters.DatabaseAdapter) TLongObjectMap(gnu.trove.map.TLongObjectMap) FakeMember(ml.duncte123.skybot.entities.jda.FakeMember) RateLimiter(com.github.natanbc.reliqua.limiter.RateLimiter) WebUtils(me.duncte123.botcommons.web.WebUtils) PendingRequest(com.github.natanbc.reliqua.request.PendingRequest) GuildMusicManager(ml.duncte123.skybot.audio.GuildMusicManager) Reminder(ml.duncte123.skybot.objects.api.Reminder) FakePendingRequest(ml.duncte123.skybot.objects.FakePendingRequest) Reminder(ml.duncte123.skybot.objects.api.Reminder) ArrayList(java.util.ArrayList) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DateTimeParseException(java.time.format.DateTimeParseException) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) OffsetDateTime(java.time.OffsetDateTime) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException)

Example 2 with ErrorResponse

use of net.dv8tion.jda.api.requests.ErrorResponse in project SkyBot by duncte123.

the class AirUtils method handleExpiredReminders.

public static void handleExpiredReminders(List<Reminder> reminders, DatabaseAdapter adapter) {
    // Get the shardManager and a list of ints to purge the ids for
    final ShardManager shardManager = SkyBot.getInstance().getShardManager();
    final List<Integer> toPurge = new ArrayList<>();
    for (final Reminder reminder : reminders) {
        // The reminder message template
        final String message = String.format("%s you asked me to remind you about \"%s\"", TimeFormat.RELATIVE.format(reminder.getCreate_date()), reminder.getReminder().trim());
        // If we have a channel send the message to that
        if (reminder.getIn_channel()) {
            final long channelId = reminder.getChannel_id();
            final TextChannel channel = shardManager.getTextChannelById(channelId);
            // skipping the continue statement makes sure that we roll into the dm part of this
            if (channel != null) {
                // Add the reminder to the list of the reminders to purge
                toPurge.add(reminder.getId());
                sendMsg(new MessageConfig.Builder().setChannel(channel).setMessage(String.format("<@%s>, %s", reminder.getUser_id(), message)).replyTo(reminder.getMessage_id()).build());
                // go to the next one and don't run the user code if a channel was found
                continue;
            }
        }
        try {
            Objects.requireNonNull(shardManager.getShardById(0)).openPrivateChannelById(reminder.getUser_id()).flatMap((c) -> c.sendMessage(message + "\n" + reminder.getJumpUrl())).complete();
            toPurge.add(reminder.getId());
        } catch (ErrorResponseException errorResponseEx) {
            final ErrorResponse errorResponse = errorResponseEx.getErrorResponse();
            if (// The account probably got deleted or something
            errorResponse == ErrorResponse.UNKNOWN_USER || // we cannot dm this user (has dms blocked?)
            errorResponse == ErrorResponse.CANNOT_SEND_TO_USER) {
                toPurge.add(reminder.getId());
            }
        } catch (Exception e) {
            Sentry.captureException(e);
        }
    }
    // get a date that is 2 days in the future
    final OffsetDateTime plusTwoDays = OffsetDateTime.now(ZoneOffset.UTC).plus(2L, ChronoUnit.DAYS);
    // Remove any reminders that have not been removed after 2 days
    final List<Integer> extraRemoval = reminders.stream().filter((reminder) -> reminder.getReminder_date().isAfter(plusTwoDays)).map(Reminder::getId).collect(Collectors.toList());
    toPurge.addAll(extraRemoval);
    if (!toPurge.isEmpty()) {
        adapter.purgeRemindersSync(toPurge);
    }
}
Also used : Arrays(java.util.Arrays) JDA(net.dv8tion.jda.api.JDA) StringUtils(me.duncte123.botcommons.StringUtils) ParsedDuration(me.duncte123.durationparser.ParsedDuration) Permission(net.dv8tion.jda.api.Permission) JSONRequestBody(me.duncte123.botcommons.web.requests.JSONRequestBody) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FinderUtil(com.jagrosh.jdautilities.commons.utils.FinderUtil) User(net.dv8tion.jda.api.entities.User) TemporalAccessor(java.time.temporal.TemporalAccessor) ArrayList(java.util.ArrayList) WebParserUtils.toJSONObject(me.duncte123.botcommons.web.WebParserUtils.toJSONObject) Guild(net.dv8tion.jda.api.entities.Guild) JDAImpl(net.dv8tion.jda.internal.JDAImpl) LavalinkManager(fredboat.audio.player.LavalinkManager) ZoneOffset(java.time.ZoneOffset) Sentry(io.sentry.Sentry) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) MessageUtils.sendMsg(me.duncte123.botcommons.messaging.MessageUtils.sendMsg) CommandContext(ml.duncte123.skybot.objects.command.CommandContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TimeFormat(net.dv8tion.jda.api.utils.TimeFormat) WebParserUtils(me.duncte123.botcommons.web.WebParserUtils) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) ChronoUnit(java.time.temporal.ChronoUnit) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) SkyBot(ml.duncte123.skybot.SkyBot) DatabaseAdapter(ml.duncte123.skybot.adapters.DatabaseAdapter) TLongObjectMap(gnu.trove.map.TLongObjectMap) FakeMember(ml.duncte123.skybot.entities.jda.FakeMember) WebUtils(me.duncte123.botcommons.web.WebUtils) PendingRequest(com.github.natanbc.reliqua.request.PendingRequest) GuildMusicManager(ml.duncte123.skybot.audio.GuildMusicManager) Reminder(ml.duncte123.skybot.objects.api.Reminder) FakePendingRequest(ml.duncte123.skybot.objects.FakePendingRequest) Reminder(ml.duncte123.skybot.objects.api.Reminder) ArrayList(java.util.ArrayList) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DateTimeParseException(java.time.format.DateTimeParseException) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) OffsetDateTime(java.time.OffsetDateTime) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 PendingRequest (com.github.natanbc.reliqua.request.PendingRequest)2 FinderUtil (com.jagrosh.jdautilities.commons.utils.FinderUtil)2 LavalinkManager (fredboat.audio.player.LavalinkManager)2 TLongObjectMap (gnu.trove.map.TLongObjectMap)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 Sentry (io.sentry.Sentry)2 OffsetDateTime (java.time.OffsetDateTime)2 ZoneOffset (java.time.ZoneOffset)2 DateTimeParseException (java.time.format.DateTimeParseException)2 ChronoUnit (java.time.temporal.ChronoUnit)2 TemporalAccessor (java.time.temporal.TemporalAccessor)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 Nonnull (javax.annotation.Nonnull)2