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