-
Notifications
You must be signed in to change notification settings - Fork 921
/
cplogs_legacy.go
67 lines (55 loc) · 1.79 KB
/
cplogs_legacy.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
package common
import (
"encoding/json"
"fmt"
"time"
"github.com/jonas747/discordgo/v2"
"github.com/mediocregopher/radix/v3"
)
type CPLogEntryLegacy struct {
Timestamp int64 `json:"ts"`
Action string `json:"action"`
TimestampString string `json:"-"`
}
func AddCPLogEntry(user *discordgo.User, guild int64, args ...interface{}) {
// no op, as to not cause breakage
}
func AddCPLogEntryLegacy(user *discordgo.User, guild int64, args ...interface{}) {
action := fmt.Sprintf("(UserID: %d) %s#%s: %s", user.ID, user.Username, user.Discriminator, fmt.Sprint(args...))
now := time.Now()
entry := &CPLogEntryLegacy{
Timestamp: now.Unix(),
Action: action,
}
serialized, err := json.Marshal(entry)
if err != nil {
logger.WithError(err).Error("Failed marshalling cp log entry")
return
}
key := "cp_logs:" + discordgo.StrID(guild)
err = RedisPool.Do(radix.Cmd(nil, "LPUSH", key, string(serialized)))
RedisPool.Do(radix.Cmd(nil, "LTRIM", key, "0", "100"))
if err != nil {
logger.WithError(err).WithField("guild", guild).Error("Failed updating cp logs")
}
}
func GetCPLogEntriesLegacy(guild int64) ([]*CPLogEntryLegacy, error) {
var entriesRaw [][]byte
err := RedisPool.Do(radix.Cmd(&entriesRaw, "LRANGE", "cp_logs:"+discordgo.StrID(guild), "0", "-1"))
if err != nil {
return nil, err
}
result := make([]*CPLogEntryLegacy, len(entriesRaw))
for k, entryRaw := range entriesRaw {
var decoded *CPLogEntryLegacy
err = json.Unmarshal(entryRaw, &decoded)
if err != nil {
result[k] = &CPLogEntryLegacy{Action: "Failed decoding"}
logger.WithError(err).WithField("guild", guild).WithField("cp_log_enry", k).Error("Failed decoding cp log entry")
} else {
decoded.TimestampString = time.Unix(decoded.Timestamp, 0).Format(time.Stamp)
result[k] = decoded
}
}
return result, nil
}