wip: add scheduled message every Saturday 5pm

William Perron 1 year ago
parent 9aa181b474
commit 78fb5e122c
Signed by: wperron
GPG Key ID: BFDB4EF72D73C5F2

3
.gitignore vendored

@ -19,3 +19,6 @@ bin/
*.db *.db
*.db-shm *.db-shm
*.db-wal *.db-wal
# local env files
env.sh

@ -17,6 +17,7 @@ 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"
@ -38,6 +39,9 @@ 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)
@ -53,6 +57,10 @@ func main() {
} }
defer store.Close() defer store.Close()
notifChan := make(chan struct{})
notifier := themis.NewNotifier(notifChan)
go notifier.Start(ctx)
authToken, ok := os.LookupEnv("DISCORD_TOKEN") authToken, ok := os.LookupEnv("DISCORD_TOKEN")
if !ok { if !ok {
log.Fatal().Err(err).Msg("no auth token found at DISCORD_TOKEN env var") 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") 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)) discord, err := discordgo.New(fmt.Sprintf("Bot %s", authToken))
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("failed to initialize discord session") log.Fatal().Err(err).Msg("failed to initialize discord session")
@ -511,6 +524,21 @@ func main() {
cancel() 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() <-ctx.Done()
log.Info().Msg("context cancelled, exiting") log.Info().Msg("context cancelled, exiting")

@ -8,6 +8,7 @@ processes = []
[env] [env]
DISCORD_APP_ID = "1014881815921705030" DISCORD_APP_ID = "1014881815921705030"
DISCORD_GUILD_ID = "375417755777892353" DISCORD_GUILD_ID = "375417755777892353"
DISCORD_BOT_CHANNEL_ID = "1018997240968265768"
[experimental] [experimental]
allowed_public_ports = [] allowed_public_ports = []

@ -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)
}
}
Loading…
Cancel
Save