This repository has been archived on 2022-10-02. You can view files and clone it, but cannot push or open issues or pull requests.
dcbot-old/cron/cron.go

49 lines
1.2 KiB
Go

package cron
import (
"time"
"github.com/tribalwarshelp/golang-sdk/sdk"
"github.com/tribalwarshelp/shared/mode"
"github.com/tribalwarshelp/dcbot/discord"
"github.com/tribalwarshelp/dcbot/group"
"github.com/tribalwarshelp/dcbot/observation"
"github.com/tribalwarshelp/dcbot/server"
"github.com/robfig/cron/v3"
)
type Config struct {
ServerRepo server.Repository
ObservationRepo observation.Repository
Discord *discord.Session
GroupRepo group.Repository
API *sdk.SDK
Status string
}
func Attach(c *cron.Cron, cfg Config) {
h := &handler{
lastEnnoblementAt: make(map[string]time.Time),
serverRepo: cfg.ServerRepo,
observationRepo: cfg.ObservationRepo,
groupRepo: cfg.GroupRepo,
discord: cfg.Discord,
api: cfg.API,
status: cfg.Status,
}
c.AddFunc("@every 1m", h.checkEnnoblements)
c.AddFunc("@every 30m", h.checkBotServers)
c.AddFunc("@every 2h10m", h.deleteClosedTribalWarsServers)
c.AddFunc("@every 2h", h.updateBotStatus)
go func() {
h.checkBotServers()
h.deleteClosedTribalWarsServers()
h.updateBotStatus()
if mode.Get() == mode.DevelopmentMode {
h.checkEnnoblements()
}
}()
}