cron is now updating bot status every 6 hours

This commit is contained in:
Dawid Wysokiński 2020-07-15 12:03:43 +02:00 committed by Kichiyaki
parent 92717b6f0a
commit 9932fc67c1
4 changed files with 28 additions and 4 deletions

View File

@ -19,6 +19,7 @@ type Config struct {
Discord *discord.Session
GroupRepo group.Repository
API *sdk.SDK
Status string
}
func Attach(c *cron.Cron, cfg Config) {
@ -29,12 +30,15 @@ func Attach(c *cron.Cron, cfg Config) {
groupRepo: cfg.GroupRepo,
discord: cfg.Discord,
api: cfg.API,
status: cfg.Status,
}
c.AddFunc("@every 1m", h.checkLastEnnoblements)
c.AddFunc("@every 30m", h.checkBotMembershipOnServers)
c.AddFunc("@every 2h10m", h.deleteClosedTribalWarsServers)
c.AddFunc("@every 6h", h.updateBotStatus)
go func() {
h.checkBotMembershipOnServers()
h.deleteClosedTribalWarsServers()
h.updateBotStatus()
}()
}

View File

@ -24,6 +24,7 @@ type handler struct {
groupRepo group.Repository
discord *discord.Session
api *sdk.SDK
status string
}
func (h *handler) loadEnnoblements(servers []string) map[string]ennoblements {
@ -48,7 +49,7 @@ func (h *handler) loadEnnoblements(servers []string) map[string]ennoblements {
lastEnnoblementAt, ok := h.lastEnnoblementAt[w]
if !ok {
lastEnnoblementAt = time.Now().Add(-60 * time.Minute)
lastEnnoblementAt = time.Now().Add(-1 * time.Minute)
}
m[w] = filterEnnoblements(es, lastEnnoblementAt)
@ -241,3 +242,9 @@ func (h *handler) deleteClosedTribalWarsServers() {
}
}
}
func (h *handler) updateBotStatus() {
if err := h.discord.UpdateStatus(h.status); err != nil {
log.Print("updateBotStatus: " + err.Error())
}
}

View File

@ -50,7 +50,7 @@ func (s *Session) init() error {
return fmt.Errorf("error opening ws connection: %s", err.Error())
}
if err := s.dg.UpdateStatus(0, s.cfg.Status); err != nil {
if err := s.UpdateStatus(s.cfg.Status); err != nil {
return err
}
return nil
@ -70,6 +70,13 @@ func (s *Session) SendEmbed(channelID string, message *discordgo.MessageEmbed) e
return err
}
func (s *Session) UpdateStatus(status string) error {
if err := s.dg.UpdateStatus(0, status); err != nil {
return err
}
return nil
}
func (s *Session) handleNewMessage(_ *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.dg.State.User.ID || m.Author.Bot {
return

10
main.go
View File

@ -21,6 +21,11 @@ import (
"github.com/robfig/cron/v3"
)
const (
status = "Tribal Wars | tw!help"
commandPrefix = "tw!"
)
func init() {
os.Setenv("TZ", "UTC")
@ -59,8 +64,8 @@ func main() {
sess, err := discord.New(discord.SessionConfig{
Token: os.Getenv("BOT_TOKEN"),
CommandPrefix: "tw!",
Status: "Tribal Wars | tw!help",
CommandPrefix: commandPrefix,
Status: status,
ObservationRepository: observationRepo,
ServerRepository: serverRepo,
GroupRepository: groupRepo,
@ -80,6 +85,7 @@ func main() {
Discord: sess,
GroupRepo: groupRepo,
API: api,
Status: status,
})
c.Start()
defer c.Stop()