diff --git a/cmd/themis-server/main.go b/cmd/themis-server/main.go index 1617baa..490d310 100644 --- a/cmd/themis-server/main.go +++ b/cmd/themis-server/main.go @@ -17,7 +17,6 @@ import ( "github.com/bwmarrin/discordgo" _ "github.com/mattn/go-sqlite3" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" "go.wperron.io/themis" @@ -39,9 +38,6 @@ func main() { ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT) defer cancel() - // TODO(wperron) remove before merging - zerolog.SetGlobalLevel(zerolog.DebugLevel) - flag.Parse() err := touchDbFile(*dbFile) @@ -166,11 +162,26 @@ func main() { }, }, }, + + // Scheduling commands { Name: "schedule", Description: "Get the schedule for the following weeks.", Type: discordgo.ChatApplicationCommand, }, + { + Name: "absent", + Description: "Mark yourself as absent for a session", + Type: discordgo.ChatApplicationCommand, + Options: []*discordgo.ApplicationCommandOption{ + { + Name: "date", + Required: false, + Description: "Date of the session you can't make it to. YYYY-MM-DD format.", + Type: discordgo.ApplicationCommandOptionString, + }, + }, + }, } handlers := map[string]Handler{ "info": func(s *discordgo.Session, i *discordgo.InteractionCreate) { @@ -473,7 +484,11 @@ func main() { sb := strings.Builder{} keys := make([]string, 0, len(sched)) - for k := range sched { + for k, abs := range sched { + for i, a := range abs { + a = fmt.Sprintf("<@%s>", a) + abs[i] = a + } keys = append(keys, k) } sort.Strings(keys) @@ -483,7 +498,7 @@ func main() { if len(sched[d]) == 0 { sb.WriteString("Everyone is available!\n") } else { - sb.WriteString(strings.Join(sched[d], ", ") + " won't be able to make it") + sb.WriteString(themis.FormatStringSlice(sched[d]) + " won't be able to make it\n") } } @@ -496,6 +511,71 @@ func main() { log.Error().Err(err).Msg("failed to respond to interaction") } }, + "absent": func(s *discordgo.Session, i *discordgo.InteractionCreate) { + var rawDate string + if len(i.ApplicationCommandData().Options) == 0 { + rawDate = themis.NextMonday().Format(time.DateOnly) + } else { + rawDate = i.ApplicationCommandData().Options[0].StringValue() + } + + date, err := time.Parse(time.DateOnly, rawDate) + if err != nil { + if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "failed to parse provided date, make sure to use the YYYY-MM-DD format.", + }, + }); err != nil { + log.Error().Err(err).Msg("failed to respond to interaction") + } + } + + if date.Before(time.Now()) { + if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "The date must be some time in the future.", + }, + }); err != nil { + log.Error().Err(err).Msg("failed to respond to interaction") + } + } + + if date.Weekday() != time.Monday { + if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "The date you provided is not a Monday.", + }, + }); err != nil { + log.Error().Err(err).Msg("failed to respond to interaction") + } + // TODO(wperron) suggest Mondays before and after? + } + + userId := i.Member.User.ID + if err := store.AddAbsence(ctx, date, userId); err != nil { + if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "something went wrong recording your absence, check logs for more info.", + }, + }); err != nil { + log.Error().Err(err).Msg("failed to respond to interaction") + } + } + + err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Okey dokey.", + }, + }) + if err != nil { + log.Error().Err(err).Msg("failed to respond to interaction") + } + }, } registerHandlers(discord, handlers) diff --git a/notify.go b/notify.go index 67f6c72..2a142a7 100644 --- a/notify.go +++ b/notify.go @@ -51,7 +51,7 @@ func (n *Notifier) Start(ctx context.Context) { case <-ticker.C: n.c <- struct{}{} } - time.Sleep(time.Hour) + time.Sleep(time.Second) } } @@ -63,6 +63,6 @@ func (n *Notifier) NotifyFunc(ctx context.Context, f func()) { case <-n.c: f() } - time.Sleep(5 * time.Minute) + time.Sleep(time.Second) } }