From 916d9972de1c2bce4cbecf429383fd3ac0c85282 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 28 Jan 2024 06:00:17 -0600 Subject: [PATCH] ICS command continued work --- commands.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 370bf45..f6e9dc0 100644 --- a/commands.go +++ b/commands.go @@ -9,11 +9,12 @@ import ( ) var ( - commandDefinitions = []*discordgo.ApplicationCommand{TermCommandDefinition, TimeCommandDefinition, SearchCommandDefinition} + commandDefinitions = []*discordgo.ApplicationCommand{TermCommandDefinition, TimeCommandDefinition, SearchCommandDefinition, IcsCommandDefinition} commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) error{ TimeCommandDefinition.Name: TimeCommandHandler, TermCommandDefinition.Name: TermCommandHandler, SearchCommandDefinition.Name: SearchCommandHandler, + IcsCommandDefinition.Name: IcsCommandHandler, } ) @@ -254,3 +255,65 @@ func TimeCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) er }) return nil } + +var IcsCommandDefinition = &discordgo.ApplicationCommand{ + Name: "ics", + Description: "Generate an ICS file for a course", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionInteger, + Name: "crn", + Description: "Course Reference Number", + Required: true, + }, + }, +} + +func IcsCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) error { + crn := i.ApplicationCommandData().Options[0].IntValue() + + meetingTimes, err := GetCourseMeetingTime(202420, int(crn)) + if err != nil { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Error getting meeting time", + }, + }) + return err + } + + cal := NewCalendar() + cal.SetTimezoneId("America/Chicago") + + for _, meeting := range meetingTimes { + event := cal.AddEvent(fmt.Sprintf("%s@UTSA.EDU", meeting.CourseReferenceNumber)) + event.SetSummary(meeting.CourseReferenceNumber) + event.SetDtStampTime(time.Now()) + + startDay := meeting.StartDay() + startTime := meeting.StartTime() + endTime := meeting.EndTime() + + event.SetStartAt(time.Date(startDay.Year(), startDay.Month(), startDay.Day(), int(startTime.Hours), int(startTime.Minutes), 0, 0, time.UTC)) + event.SetEndAt(time.Date(startDay.Year(), startDay.Month(), startDay.Day(), int(endTime.Hours), int(endTime.Minutes), 0, 0, time.UTC)) + + event.AddRrule(meeting.RRule()) + event.SetLocation(meeting.PlaceString()) + } + + session.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Files: []*discordgo.File{ + { + Name: fmt.Sprintf("%d.ics", crn), + ContentType: "text/calendar", + Reader: strings.NewReader(cal.Serialize()), + }, + }, + AllowedMentions: &discordgo.MessageAllowedMentions{}, + }, + }) + return nil +}