diff --git a/cron/cron.go b/cron/cron.go index 51b3fd1..d2347ca 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -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() }() } diff --git a/cron/handler.go b/cron/handler.go index 912d9b0..5842d47 100644 --- a/cron/handler.go +++ b/cron/handler.go @@ -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()) + } +} diff --git a/discord/discord.go b/discord/discord.go index 5168cf4..6729384 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -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 diff --git a/main.go b/main.go index ae4ca9b..218955c 100644 --- a/main.go +++ b/main.go @@ -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()