diff --git a/cmd/jia.go b/cmd/jia.go index 912906a..fbcf1ec 100644 --- a/cmd/jia.go +++ b/cmd/jia.go @@ -15,6 +15,8 @@ func main() { fmt.Println("Starting Jia…") config := jia.NewConfig() + jia.InitCron(config) + // Start receiving messages fmt.Println(fmt.Sprintf("Listening on port %d", config.Port)) jia.StartServer(config) diff --git a/go.mod b/go.mod index b03288d..5fc1f2f 100644 --- a/go.mod +++ b/go.mod @@ -6,5 +6,6 @@ require ( github.com/go-redis/redis v6.15.8+incompatible github.com/joho/godotenv v1.3.0 github.com/pkg/errors v0.9.1 // indirect + github.com/robfig/cron v1.2.0 github.com/slack-go/slack v0.6.5 ) diff --git a/go.sum b/go.sum index 451eb3b..7039e19 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= +github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= github.com/slack-go/slack v0.6.4 h1:cxOqFgM5RW6mdEyDqAJutFk3qiORK9oHRKi5bPqkY9o= github.com/slack-go/slack v0.6.4/go.mod h1:sGRjv3w+ERAUMMMbldHObQPBcNSyVB7KLKYfnwUFBfw= github.com/slack-go/slack v0.6.5 h1:IkDKtJ2IROJNoe3d6mW870/NRKvq2fhLB/Q5XmzWk00= diff --git a/pkg/jia/cron.go b/pkg/jia/cron.go new file mode 100644 index 0000000..ca62c05 --- /dev/null +++ b/pkg/jia/cron.go @@ -0,0 +1,35 @@ +package jia + +import ( + "fmt" + "log" + "time" + + "github.com/robfig/cron" + "github.com/slack-go/slack" +) + +func InitCron(config *Config) { + c := cron.New() + + c.AddFunc("0 * * * * *", func() { + slackClient := slack.New(config.BotToken) + + now := time.Now() + entries, _ := getLeaderboardEntries(now.Month(), now.Year()) + + _, _, err := slackClient.PostMessage( + config.ChannelID, + slack.MsgOptionText( + fmt.Sprintf("It is the start of a new minute! %v", entries[0].User), + false, + ), + ) + + if err != nil { + log.Println(err) + } + }) + + c.Start() +} diff --git a/pkg/jia/db.go b/pkg/jia/db.go new file mode 100644 index 0000000..fbc2b59 --- /dev/null +++ b/pkg/jia/db.go @@ -0,0 +1,46 @@ +package jia + +import ( + "errors" + "fmt" + "sort" + "time" +) + +type Entry struct { + Number int + User string +} + +func getLeaderboardEntries(month time.Month, year int) ([]Entry, error) { + scan := redisClient.Scan(0, fmt.Sprintf("leaderboard:%d-%d:*", year, month), 10) + if scan.Err() != nil { + return nil, errors.New("Something went wrong while loading the leaderboard :cry: Please try again later!") + } + + scanIterator := scan.Iterator() + + entries := []Entry{} + + for scanIterator.Next() { + entry := redisClient.Get(scanIterator.Val()) + entryInt, err := entry.Int() + if err != nil { + return nil, err + } + + if user, ok := parseLeaderboardEntry(scanIterator.Val()); ok { + entries = append(entries, Entry{ + Number: entryInt, + User: user, + }) + } + } + + // Sort entries + sort.Slice(entries, func(i, j int) bool { + return entries[i].Number > entries[j].Number + }) + + return entries, nil +} diff --git a/pkg/jia/handlers.go b/pkg/jia/handlers.go index 28f4fd8..8f8abc4 100644 --- a/pkg/jia/handlers.go +++ b/pkg/jia/handlers.go @@ -6,7 +6,6 @@ import ( "log" "net/http" "regexp" - "sort" "strconv" "time" @@ -100,41 +99,12 @@ func HandleLeaderboardSlashCommand(w http.ResponseWriter, r *http.Request) { year := now.Year() month := now.Month() - scan := redisClient.Scan(0, fmt.Sprintf("leaderboard:%d-%d:*", year, month), 10) - if scan.Err() != nil { - w.Write([]byte("Something went wrong while loading the leaderboard :cry: Please try again later!")) + entries, err := getLeaderboardEntries(month, year) + if err != nil { + w.Write(nil) return } - scanIterator := scan.Iterator() - - type Entry struct { - Number int - User string - } - - entries := []Entry{} - - for scanIterator.Next() { - entry := redisClient.Get(scanIterator.Val()) - entryInt, err := entry.Int() - if err != nil { - return - } - - if user, ok := parseLeaderboardEntry(scanIterator.Val()); ok { - entries = append(entries, Entry{ - Number: entryInt, - User: user, - }) - } - } - - // Sort entries - sort.Slice(entries, func(i, j int) bool { - return entries[i].Number > entries[j].Number - }) - blocks := []slack.Block{ slack.NewSectionBlock( slack.NewTextBlockObject("mrkdwn", fmt.Sprintf(":chart_with_upwards_trend: Counting stats for *%s %d*:", month.String(), year), false, false),