Use custom struct for MeetingTimeFaculty.timeStart, move comand handlers

This commit is contained in:
2023-12-11 19:29:42 -06:00
parent 46d53fcac3
commit 5b1f325280
2 changed files with 60 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"io" "io"
"log" "log"
"math/rand" "math/rand"
@@ -78,3 +79,59 @@ func Plural(n int) string {
} }
return "s" 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)
}

View File

@@ -21,9 +21,6 @@ var (
session *discordgo.Session session *discordgo.Session
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not") RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not")
integerOptionMinValue = 0.0 integerOptionMinValue = 0.0
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"time": TimeCommandHandler,
}
) )
type MeetingTimeFaculty struct { type MeetingTimeFaculty struct {
@@ -43,8 +40,8 @@ type MeetingTimeResponse struct {
building string building string
buildingDescription string buildingDescription string
room string room string
timeStart uint64 timeStart NaiveTime
timeEnd uint64 timeEnd NaiveTime
dateStart time.Time dateStart time.Time
dateEnd time.Time dateEnd time.Time
hoursPerWeek float32 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)) registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions))
for i, v := range commandDefinitions { for i, v := range commandDefinitions {
cmd, err := session.ApplicationCommandCreate(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v) cmd, err := session.ApplicationCommandCreate(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v)