add /absent command

William Perron 1 year ago
parent b2ffbedfa5
commit 5acafefe0b
Signed by: wperron
GPG Key ID: BFDB4EF72D73C5F2

@ -17,7 +17,6 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"go.wperron.io/themis" "go.wperron.io/themis"
@ -39,9 +38,6 @@ func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT) ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT)
defer cancel() defer cancel()
// TODO(wperron) remove before merging
zerolog.SetGlobalLevel(zerolog.DebugLevel)
flag.Parse() flag.Parse()
err := touchDbFile(*dbFile) err := touchDbFile(*dbFile)
@ -166,11 +162,26 @@ func main() {
}, },
}, },
}, },
// Scheduling commands
{ {
Name: "schedule", Name: "schedule",
Description: "Get the schedule for the following weeks.", Description: "Get the schedule for the following weeks.",
Type: discordgo.ChatApplicationCommand, 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{ handlers := map[string]Handler{
"info": func(s *discordgo.Session, i *discordgo.InteractionCreate) { "info": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
@ -473,7 +484,11 @@ func main() {
sb := strings.Builder{} sb := strings.Builder{}
keys := make([]string, 0, len(sched)) 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) keys = append(keys, k)
} }
sort.Strings(keys) sort.Strings(keys)
@ -483,7 +498,7 @@ func main() {
if len(sched[d]) == 0 { if len(sched[d]) == 0 {
sb.WriteString("Everyone is available!\n") sb.WriteString("Everyone is available!\n")
} else { } 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") 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) registerHandlers(discord, handlers)

@ -51,7 +51,7 @@ func (n *Notifier) Start(ctx context.Context) {
case <-ticker.C: case <-ticker.C:
n.c <- struct{}{} 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: case <-n.c:
f() f()
} }
time.Sleep(5 * time.Minute) time.Sleep(time.Second)
} }
} }

Loading…
Cancel
Save