From 78fb5e122ca2c25cab92d47e3697752edaef8874 Mon Sep 17 00:00:00 2001 From: William Perron Date: Sun, 19 Nov 2023 16:36:37 -0500 Subject: [PATCH] wip: add scheduled message every Saturday 5pm --- .gitignore | 3 +++ cmd/themis-server/main.go | 28 ++++++++++++++++++++ fly.toml | 1 + notify.go | 56 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 notify.go diff --git a/.gitignore b/.gitignore index ee4d410..6647a79 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,6 @@ bin/ *.db *.db-shm *.db-wal + +# local env files +env.sh diff --git a/cmd/themis-server/main.go b/cmd/themis-server/main.go index 292efae..589af62 100644 --- a/cmd/themis-server/main.go +++ b/cmd/themis-server/main.go @@ -17,6 +17,7 @@ import ( "github.com/bwmarrin/discordgo" _ "github.com/mattn/go-sqlite3" + "github.com/rs/zerolog" "github.com/rs/zerolog/log" "go.wperron.io/themis" @@ -38,6 +39,9 @@ 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) @@ -53,6 +57,10 @@ func main() { } defer store.Close() + notifChan := make(chan struct{}) + notifier := themis.NewNotifier(notifChan) + go notifier.Start(ctx) + authToken, ok := os.LookupEnv("DISCORD_TOKEN") if !ok { log.Fatal().Err(err).Msg("no auth token found at DISCORD_TOKEN env var") @@ -68,6 +76,11 @@ func main() { log.Fatal().Err(err).Msg("no guild id found at DISCORD_GUILD_ID env var") } + channelId, ok := os.LookupEnv("DISCORD_BOT_CHANNEL_ID") + if !ok { + log.Fatal().Err(err).Msg("no channel id found at DISCORD_BOT_CHANNEL_ID env var") + } + discord, err := discordgo.New(fmt.Sprintf("Bot %s", authToken)) if err != nil { log.Fatal().Err(err).Msg("failed to initialize discord session") @@ -511,6 +524,21 @@ func main() { cancel() }() + go func() { + for { + select { + case <-ctx.Done(): + return + case <-notifChan: + _, err := discord.ChannelMessageSend(channelId, "don't forget we have a session next %s!") + if err != nil { + log.Error().Err(err).Msg("failed to send scheduled notification") + } + } + time.Sleep(5 * time.Minute) + } + }() + <-ctx.Done() log.Info().Msg("context cancelled, exiting") diff --git a/fly.toml b/fly.toml index 0421d03..bf05045 100644 --- a/fly.toml +++ b/fly.toml @@ -8,6 +8,7 @@ processes = [] [env] DISCORD_APP_ID = "1014881815921705030" DISCORD_GUILD_ID = "375417755777892353" +DISCORD_BOT_CHANNEL_ID = "1018997240968265768" [experimental] allowed_public_ports = [] diff --git a/notify.go b/notify.go new file mode 100644 index 0000000..0f1398d --- /dev/null +++ b/notify.go @@ -0,0 +1,56 @@ +package themis + +import ( + "context" + "fmt" + "time" +) + +var loc *time.Location + +func init() { + loc, _ = time.LoadLocation("America/Toronto") +} + +type Notifier struct { + c chan struct{} +} + +func NewNotifier(c chan struct{}) *Notifier { + return &Notifier{ + c: c, + } +} + +func (n *Notifier) Start(ctx context.Context) { + m := NextMonday() + sat := m.AddDate(0, 0, -2) + if sat.Before(time.Now()) { + sat = sat.AddDate(0, 0, 7) + } + + t, err := time.ParseInLocation(time.DateTime, fmt.Sprintf("%s 16:35:00", sat.Format(time.DateOnly)), loc) + if err != nil { + panic("failed to parse next monday notif time. this is likely a bug.") + } + + first := time.NewTimer(time.Until(t)) + <-first.C + select { + case <-ctx.Done(): + return + default: + n.c <- struct{}{} + } + + ticker := time.NewTicker(time.Hour * 24 * 7) + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + n.c <- struct{}{} + } + time.Sleep(time.Hour) + } +}