-
Notifications
You must be signed in to change notification settings - Fork 0
/
role_updater.go
80 lines (67 loc) · 1.8 KB
/
role_updater.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package bot
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/utils/json/option"
"github.com/holedaemon/bot2/internal/db/modelsx"
"github.com/zikaeroh/ctxlog"
"go.uber.org/zap"
)
const (
updateInterval = time.Hour * 24
// updateInterval = time.Minute * 2
updateOncePer = (time.Hour * 24) * 7
roleFmt = "%d HOURS IN XIV"
)
func (b *Bot) roleUpdater(ctx context.Context) {
t := time.NewTicker(updateInterval)
for {
select {
case <-ctx.Done():
t.Stop()
return
case <-t.C:
updater, err := modelsx.FetchRoleUpdater(ctx, b.db, scroteGuildID.String())
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
continue
}
ctxlog.Error(ctx, "error fetching role update settings")
continue
}
if !updater.DoUpdates {
ctxlog.Debug(ctx, "role updates are disabled, skipping...")
continue
}
if updater.LastTimestamp.Add(updateOncePer).After(time.Now()) {
ctxlog.Debug(ctx, "too early to update mettic's role, skipping...")
continue
}
games, err := b.steam.GetOwnedGames(ctx, metticSteamID)
if err != nil {
ctxlog.Error(ctx, "error fetching mettic's games")
continue
}
for _, g := range games.Games {
if g.AppID == xivAppID {
hours := g.PlaytimeForever / 60
if _, err := b.state.ModifyRole(scroteGuildID, metticRoleID, api.ModifyRoleData{
Name: option.NewNullableString(fmt.Sprintf(roleFmt, hours)),
}); err != nil {
ctxlog.Error(ctx, "error updating mettic's role", zap.Error(err))
continue
}
}
}
if err := modelsx.UpsertRoleUpdater(ctx, b.db, updater); err != nil {
ctxlog.Error(ctx, "error upsert role updater", zap.Error(err))
continue
}
ctxlog.Debug(ctx, "updated mettic's role")
}
}
}