Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CalendarEvent> getEvents(String calendarUrl) throws MalformedURLException {
URL url = new URL(calendarUrl);
private static List<CalendarEvent> 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<ZonedDateTime> period = new Period<>(now, nextWeek);
Period<ZonedDateTime> period = new Period<>(
now.atZone(ZoneOffset.UTC),
nextWeek.atZone(ZoneOffset.UTC)
);

List<CalendarEvent> 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<Period<ZonedDateTime>> occurrences =
event.calculateRecurrenceSet(period);


for (Period<ZonedDateTime> occurrence : occurrences) {

String summary = (event.getSummary() != null) ? event.getSummary().getValue() : null;
Expand All @@ -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";
}

Expand Down
Loading