diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/generic/calendar/CalendarGetSubCommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/generic/calendar/CalendarGetSubCommand.java index 5e70ed8c..41d756e8 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/commands/generic/calendar/CalendarGetSubCommand.java +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/generic/calendar/CalendarGetSubCommand.java @@ -6,6 +6,7 @@ import com.beanbeanjuice.cafebot.utility.commands.ISubCommand; import com.beanbeanjuice.cafebot.utility.handlers.calendar.CalendarHandler; import com.beanbeanjuice.cafebot.utility.helper.Helper; +import com.beanbeanjuice.cafebot.utility.logging.LogLevel; import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.commands.OptionType; @@ -42,6 +43,7 @@ public void handle(SlashCommandInteractionEvent event) { private void handleError(Throwable ex, SlashCommandInteractionEvent event) { event.getHook().sendMessageEmbeds(Helper.errorEmbed("Error Getting Calendar", "I... couldn't find the calendar... is this an error??")).queue(); + bot.getLogger().log(this.getClass(), LogLevel.WARN, ex.getMessage(), true, false, ex); } @Override @@ -58,7 +60,7 @@ public String getDescription() { public OptionData[] getOptions() { return new OptionData[] { new OptionData(OptionType.STRING, "id", "The ID of the calendar you want to view!", true, true), - new OptionData(OptionType.STRING, "timezone", "The timezone you want the calendar in", true, true) + new OptionData(OptionType.STRING, "timezone", "The timezone you want the calendar in.", true, true) }; } diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/handlers/calendar/CalendarHandler.java b/src/main/java/com/beanbeanjuice/cafebot/utility/handlers/calendar/CalendarHandler.java index 12fe464e..8ab32fbe 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/handlers/calendar/CalendarHandler.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/handlers/calendar/CalendarHandler.java @@ -4,41 +4,58 @@ import net.fortuna.ical4j.data.ParserException; import net.fortuna.ical4j.model.Calendar; import net.fortuna.ical4j.model.Component; +import net.fortuna.ical4j.model.Parameter; import net.fortuna.ical4j.model.Period; import net.fortuna.ical4j.model.component.CalendarComponent; import net.fortuna.ical4j.model.component.VEvent; +import net.fortuna.ical4j.model.parameter.Value; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; +import java.time.Instant; import java.time.ZoneId; +import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; import java.util.*; import java.util.stream.Collectors; public class CalendarHandler { - private static List getEvents(String calendarUrl) throws MalformedURLException { - URL url = new URL(calendarUrl); + private static List getEvents(String calendarUrl) throws MalformedURLException, URISyntaxException { + URL url = new URI(calendarUrl).toURL(); try (InputStream in = url.openStream()) { CalendarBuilder builder = new CalendarBuilder(); Calendar calendar = builder.build(in); - ZonedDateTime now = ZonedDateTime.now(); - ZonedDateTime nextWeek = now.plusWeeks(1); + Instant now = Instant.now(); + Instant nextWeek = now.plus(7, ChronoUnit.DAYS); - Period period = new Period<>(now, nextWeek); + Period period = new Period<>( + now.atZone(ZoneOffset.UTC), + nextWeek.atZone(ZoneOffset.UTC) + ); List events = new ArrayList<>(); for (CalendarComponent component : calendar.getComponents(Component.VEVENT)) { VEvent event = (VEvent) component; + + // Skip all-day events (DTSTART is DATE, not DATE-TIME) + boolean isAllDay = event.getDateTimeStart() + .getParameter(Parameter.VALUE) + .map(Value.DATE::equals) + .orElse(false); + if (isAllDay) continue; + Set> occurrences = event.calculateRecurrenceSet(period); - for (Period occurrence : occurrences) { String summary = (event.getSummary() != null) ? event.getSummary().getValue() : null; @@ -61,7 +78,7 @@ public static String getCalendarMessage(String calendarUrl, ZoneId zoneId) { try { events = getEvents(calendarUrl); - } catch (MalformedURLException ex) { + } catch (MalformedURLException | URISyntaxException ex) { return "Error Getting Calendar"; }