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

fix update failure #16

Merged
merged 1 commit into from
May 16, 2024
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
25 changes: 15 additions & 10 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Bot struct {
addChan chan int64
removeChan chan int64
time time.Time
inputTime string
location *time.Location
db database.DB
}
Expand All @@ -38,14 +39,12 @@ func NewBot(token, inputTime, timezone, dbPath string) (Bot, error) {
return bot, err
}

inputTime = fmt.Sprintf("%s %s:00", time.Now().Format(time.DateOnly), inputTime)
parsedTime, err := time.ParseInLocation(time.DateTime, inputTime, loc)
t := fmt.Sprintf("%s %s:00", time.Now().Format(time.DateOnly), inputTime)
parsedTime, err := time.ParseInLocation(time.DateTime, t, loc)
if err != nil {
return bot, err
}

log.Printf("notfications is set to %s", parsedTime)

db, err := database.NewDB(dbPath)
if err != nil {
return bot, err
Expand All @@ -54,6 +53,7 @@ func NewBot(token, inputTime, timezone, dbPath string) (Bot, error) {
bot.location = loc
bot.botAPI = *botAPI
bot.time = parsedTime
bot.inputTime = inputTime
bot.addChan = make(chan int64)
bot.removeChan = make(chan int64)
bot.db = db
Expand Down Expand Up @@ -99,9 +99,11 @@ func (bot Bot) runBot() {
weekends := []time.Weekday{time.Friday, time.Saturday}

// set ticker every day at 12:00 to update time with location in case of new changes in timezone.
updateTicker := time.NewTicker(time.Until(bot.time.Truncate(24 * time.Hour).Add(24 * time.Hour)))
updateTicker := time.NewTicker(time.Until(time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour)))
reminderTicker := time.NewTicker(bot.getDuration())

log.Printf("notfications is set to %s", bot.time)

for {
select {
case chatID := <-bot.addChan:
Expand All @@ -112,13 +114,16 @@ func (bot Bot) runBot() {

case <-updateTicker.C:
// parse the time with location again to make sure the timezone is always up to date
var err error
bot.time, err = time.ParseInLocation(time.DateTime, bot.time.String(), bot.location)
t := fmt.Sprintf("%s %s:00", bot.time.Format(time.DateOnly), bot.inputTime)
parsedTime, err := time.ParseInLocation(time.DateTime, t, bot.location)
if err != nil {
log.Error().Err(err).Msg("error running the bot")
return
log.Error().Err(err).Send()
continue
}

bot.time = parsedTime
updateTicker.Reset(24 * time.Hour)
log.Printf("next notfications is set to %s", bot.time)

case <-reminderTicker.C:
chats := bot.db.List()
Expand All @@ -141,7 +146,7 @@ func (bot Bot) runBot() {
}
}

func (bot Bot) getDuration() time.Duration {
func (bot *Bot) getDuration() time.Duration {
if time.Now().After(bot.time) {
bot.time = bot.time.AddDate(0, 0, 1)
}
Expand Down
Loading