Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip weekend notifications #9

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions internal/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal

import (
"fmt"
"slices"
"time"
_ "time/tzdata"

Expand Down Expand Up @@ -86,6 +87,7 @@ func (bot Bot) Start() {

func (bot Bot) runBot() {
chatIDs := make(map[int64]bool)
weekends := []time.Weekday{time.Friday, time.Saturday}

ticker := time.NewTicker(bot.getDuration())

Expand All @@ -98,22 +100,24 @@ func (bot Bot) runBot() {
delete(chatIDs, chatID)

case <-ticker.C:
for chatID := range chatIDs {
bot.sendReminder(chatID)
// skip weekends
if !slices.Contains(weekends, bot.time.Weekday()) {
for chatID := range chatIDs {
bot.sendReminder(chatID)
}
}
bot.time = bot.time.AddDate(0, 0, 1)
ticker.Reset(24 * time.Hour)
}
}
}

func (bot Bot) getDuration() time.Duration {
targetTime := bot.time

if time.Now().After(targetTime) {
targetTime = targetTime.AddDate(0, 0, 1)
if time.Now().After(bot.time) {
bot.time = bot.time.AddDate(0, 0, 1)
}

return time.Until(targetTime)
return time.Until(bot.time)
}

func (bot Bot) sendReminder(chatID int64) {
Expand Down