From 5b1f325280550b5cadca04bfc8bbff81871ee485 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 11 Dec 2023 19:29:42 -0600 Subject: [PATCH] Use custom struct for MeetingTimeFaculty.timeStart, move comand handlers --- helpers.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 9 +++------ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/helpers.go b/helpers.go index d30cd79..0317419 100644 --- a/helpers.go +++ b/helpers.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" "log" "math/rand" @@ -78,3 +79,59 @@ func Plural(n int) string { } return "s" } + +func WeekdaysToString(days map[time.Weekday]bool) string { + str := "" + + if days[time.Monday] { + str += "M" + } + + if days[time.Tuesday] { + str += "Tu" + } + + if days[time.Wednesday] { + str += "W" + } + + if days[time.Thursday] { + str += "Th" + } + + if days[time.Friday] { + str += "F" + } + + if days[time.Saturday] { + str += "Sa" + } + + if days[time.Sunday] { + str += "Su" + } + + return str +} + +type NaiveTime struct { + Hours uint + Minutes uint +} + +func ParseNaiveTime(integer uint64) NaiveTime { + minutes := uint(integer % 100) + hours := uint(integer / 100) + + return NaiveTime{Hours: hours, Minutes: minutes} +} + +func (nt NaiveTime) String() string { + meridiem := "AM" + hour := nt.Hours + if nt.Hours >= 12 { + meridiem = "PM" + hour -= 12 + } + return fmt.Sprintf("%d:%02d%s", hour, nt.Minutes, meridiem) +} diff --git a/main.go b/main.go index 29fe0b4..03fd798 100644 --- a/main.go +++ b/main.go @@ -21,9 +21,6 @@ var ( session *discordgo.Session RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not") integerOptionMinValue = 0.0 - commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ - "time": TimeCommandHandler, - } ) type MeetingTimeFaculty struct { @@ -43,8 +40,8 @@ type MeetingTimeResponse struct { building string buildingDescription string room string - timeStart uint64 - timeEnd uint64 + timeStart NaiveTime + timeEnd NaiveTime dateStart time.Time dateEnd time.Time hoursPerWeek float32 @@ -87,7 +84,7 @@ func main() { } }) - log.Println("Adding commands...") + log.Printf("Adding %d command%s...", len(commandDefinitions), Plural(len(commandDefinitions))) registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions)) for i, v := range commandDefinitions { cmd, err := session.ApplicationCommandCreate(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v)