-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
53 lines (42 loc) · 1.13 KB
/
cron.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"time"
"github.com/robfig/cron/v3"
)
// StartEventsCron creates cron jobs for every event and starts the cron scheduler
func StartEventsCron(events []*Event, b *Bot) error {
location, err := time.LoadLocation("America/Sao_Paulo")
if err != nil {
return err
}
c := cron.New(cron.WithLocation(location))
for _, event := range events {
eventTime, err := event.GetTime()
if err != nil {
return err
}
// Subtracts 10 minutes
eventTime = eventTime.Add(-10 * time.Minute)
schedule := fmt.Sprintf("%d %d * * *", eventTime.Minute(), eventTime.Hour())
message := ""
if event.HardcoreBoss != "" {
message = fmt.Sprintf("Os seguintes World Bosses estarão ativos em 10 minutos: %s e %s", event.Boss, event.HardcoreBoss)
} else {
message = fmt.Sprintf("O seguinte World Boss estará ativo em 10 minutos: %s", event.Boss)
}
_, err = c.AddFunc(schedule, func() {
b.SendMessage(&IRCMessage{
Command: priv,
Channel: b.Config.channel,
Message: message,
})
})
if err != nil {
return err
}
}
fmt.Printf("Added %d cron entries\n", len(events))
c.Start()
return nil
}