-
Notifications
You must be signed in to change notification settings - Fork 939
/
serverstats.go
102 lines (80 loc) · 2.26 KB
/
serverstats.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package serverstats
//go:generate sqlboiler --no-hooks psql
import (
"context"
"database/sql"
"strconv"
"strings"
"sync"
"time"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/premium"
"github.com/jonas747/yagpdb/serverstats/models"
)
type Plugin struct {
stopStatsLoop chan *sync.WaitGroup
}
func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "Server Stats",
SysName: "server_stats",
Category: common.PluginCategoryMisc,
}
}
var logger = common.GetPluginLogger(&Plugin{})
func RegisterPlugin() {
common.InitSchemas("serverstats", dbSchemas...)
plugin := &Plugin{
stopStatsLoop: make(chan *sync.WaitGroup),
}
common.RegisterPlugin(plugin)
}
// ServerStatsConfig represents a configuration for a server
// reason we dont reference the model directly is because i need to figure out a way to
// migrate them over to the new schema, painlessly.
type ServerStatsConfig struct {
Public bool
IgnoreChannels string
ParsedChannels []int64
}
func (s *ServerStatsConfig) ParseChannels() {
split := strings.Split(s.IgnoreChannels, ",")
for _, v := range split {
parsed, err := strconv.ParseInt(v, 10, 64)
if err == nil {
s.ParsedChannels = append(s.ParsedChannels, parsed)
}
}
}
func configFromModel(model *models.ServerStatsConfig) *ServerStatsConfig {
conf := &ServerStatsConfig{
Public: model.Public.Bool,
IgnoreChannels: model.IgnoreChannels.String,
}
conf.ParseChannels()
return conf
}
func GetConfig(ctx context.Context, GuildID int64) (*ServerStatsConfig, error) {
if ctx == nil {
ctx = context.Background()
}
conf, err := models.FindServerStatsConfigG(ctx, GuildID)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
if conf == nil {
return &ServerStatsConfig{}, nil
}
return configFromModel(conf), nil
}
// RoundHour rounds a time.Time down to the hour
func RoundHour(t time.Time) time.Time {
t = t.UTC()
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
}
var _ premium.NewPremiumGuildListener = (*Plugin)(nil)
func (p *Plugin) OnNewPremiumGuild(guildID int64) error {
const q = `UPDATE server_stats_periods_compressed SET premium=true WHERE guild_id=$1 AND premium=false`
_, err := common.PQ.Exec(q, guildID)
return err
}