-
Notifications
You must be signed in to change notification settings - Fork 907
/
buildin_events.go
77 lines (64 loc) · 2.11 KB
/
buildin_events.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
package pubsub
import (
"encoding/json"
"reflect"
"time"
"github.com/jonas747/discordgo/v2"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/cacheset"
)
// PublishRatelimit publishes a new global ratelimit hit on discord
func PublishRatelimit(rl *discordgo.RateLimit) {
logger.Printf("Got 429: %s, %s", rl.Bucket, rl.RetryAfterDur())
reset := time.Now().Add(rl.RetryAfterDur())
err := Publish("global_ratelimit", -1, &globalRatelimitTriggeredEventData{
Bucket: rl.Bucket,
Reset: reset,
})
if err != nil {
logger.WithError(err).Error("failed publishing global ratelimit")
}
}
type globalRatelimitTriggeredEventData struct {
Reset time.Time `json:"reset"`
Bucket string `json:"bucket"`
}
func handleGlobalRatelimtPusub(evt *Event) {
data := evt.Data.(*globalRatelimitTriggeredEventData)
common.BotSession.Ratelimiter.SetGlobalTriggered(data.Reset)
}
func handleEvictCoreConfigCache(evt *Event) {
common.CoreServerConfigCache.Delete(int(evt.TargetGuildInt))
}
type evictCacheSetData struct {
Name string `json:"name"`
Key json.RawMessage `json:"key"`
}
func handleEvictCacheSet(evt *Event) {
cast := evt.Data.(*evictCacheSetData)
if slot := common.CacheSet.FindSlot(cast.Name); slot != nil {
t := slot.NewKey()
err := json.Unmarshal(cast.Key, t)
if err != nil {
logger.WithError(err).Error("failed unmarshaling CacheSet key")
}
keyConv := reflect.Indirect(reflect.ValueOf(t)).Interface()
slot.Delete(keyConv)
}
}
// EvictCacheSet sends a pubsub to evict the key on slot on all nodes if guildID is set to -1, otherwise the bot worker for that guild is the only one that handles it
func EvictCacheSet(slot *cacheset.Slot, key interface{}) {
slot.Delete(key)
marshalledKey, err := json.Marshal(key)
if err != nil {
logger.WithError(err).Error("failed marshaling CacheSet key")
return
}
err = Publish("evict_cache_set", -1, &evictCacheSetData{
Name: slot.Name(),
Key: marshalledKey,
})
if err != nil {
logger.WithError(err).Error("failed publishing guild cache eviction")
}
}