-
Notifications
You must be signed in to change notification settings - Fork 927
/
premium.go
311 lines (250 loc) · 7.68 KB
/
premium.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package premium
import (
"time"
"emperror.dev/errors"
"github.com/botlabs-gg/yagpdb/common"
"github.com/botlabs-gg/yagpdb/common/config"
"github.com/botlabs-gg/yagpdb/common/featureflags"
"github.com/botlabs-gg/yagpdb/common/scheduledevents2"
schEventsModels "github.com/botlabs-gg/yagpdb/common/scheduledevents2/models"
"github.com/botlabs-gg/yagpdb/common/templates"
"github.com/mediocregopher/radix/v3"
)
const (
// Hash
// Key: guild id's
// Value: the user id's providing the premium status
RedisKeyPremiumGuilds = "premium_activated_guilds"
RedisKeyPremiumGuildsTmp = "premium_activated_guilds_tmp"
RedisKeyPremiumGuildLastActive = "premium_guild_last_active"
)
type PremiumTier int
const (
PremiumTierNone PremiumTier = 0
PremiumTierPremium PremiumTier = 1
PremiumTierPlus PremiumTier = 2
)
func (p PremiumTier) String() string {
switch p {
case PremiumTierNone:
return "None"
case PremiumTierPlus:
return "Plus"
case PremiumTierPremium:
return "Paid"
}
return "unknown"
}
var (
confAllGuildsPremium = config.RegisterOption("yagpdb.premium.all_guilds_premium", "All servers have premium", false)
)
var logger = common.GetPluginLogger(&Plugin{})
type Plugin struct {
}
func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "Premium",
SysName: "premium",
Category: common.PluginCategoryCore,
}
}
func RegisterPlugin() {
common.InitSchemas("premium", DBSchemas...)
common.RegisterPlugin(&Plugin{})
scheduledevents2.RegisterHandler("premium_guild_added", nil, handleNewPremiumGuild)
scheduledevents2.RegisterHandler("premium_guild_removed", nil, handleRemovedPremiumGuild)
for _, v := range PremiumSources {
v.Init()
}
templates.GuildPremiumFunc = IsGuildPremium
}
type NewPremiumGuildListener interface {
OnNewPremiumGuild(guildID int64) error
}
type RemovedPremiumGuildListener interface {
OnRemovedPremiumGuild(guildID int64) error
}
var (
PremiumSources []PremiumSource
GuildPremiumSources []GuildPremiumSource
)
type PremiumSource interface {
Init()
Names() (human string, idname string)
}
type GuildPremiumSource interface {
Name() string
GuildPremiumDetails(guildID int64) (tier PremiumTier, humanDetails []string, err error)
GuildPremiumTier(guildID int64) (PremiumTier, error)
AllGuildsPremiumTiers() (map[int64]PremiumTier, error)
}
func RegisterPremiumSource(source PremiumSource) {
PremiumSources = append(PremiumSources, source)
}
func RegisterGuildPremiumSource(source GuildPremiumSource) {
GuildPremiumSources = append(GuildPremiumSources, source)
}
func GuildPremiumTier(guildID int64) (PremiumTier, error) {
flags, err := featureflags.GetGuildFlags(guildID)
if err != nil {
return PremiumTierNone, err
}
if common.ContainsStringSlice(flags, FeatureFlagPremiumFull) {
return PremiumTierPremium, nil
}
if common.ContainsStringSlice(flags, FeatureFlagPremiumPlus) {
return PremiumTierPlus, nil
}
return PremiumTierNone, nil
}
// IsGuildPremium return true if the provided guild has the premium status or not
// This is a legacy function mostly as anything equal to and above plus is counted as premium
// if you want more granular control use the other functions
func IsGuildPremium(guildID int64) (bool, error) {
if confAllGuildsPremium.GetBool() {
return true, nil
}
// for testing for example
if common.RedisPool == nil {
return false, nil
}
return featureflags.GuildHasFlag(guildID, FeatureFlagPremiumPlus)
}
// This function is deprecated and is exactly the same as calling IsguildPremium
func IsGuildPremiumCached(guildID int64) (bool, error) {
return IsGuildPremium(guildID)
}
func PremiumProvidedBy(guildID int64) (int64, error) {
if confAllGuildsPremium.GetBool() {
return int64(common.BotUser.ID), nil
}
var userID int64
err := common.RedisPool.Do(radix.FlatCmd(&userID, "HGET", RedisKeyPremiumGuilds, guildID))
return userID, errors.WithMessage(err, "PremiumProvidedBy")
}
// AllGuildsOncePremium returns all the guilds that have has premium once, and the last time that was active
func AllGuildsOncePremium() (map[int64]time.Time, error) {
if confAllGuildsPremium.GetBool() {
return allGuildsOncePremiumAllPremiumEnabled()
}
var result []int64
err := common.RedisPool.Do(radix.Cmd(&result, "ZRANGE", RedisKeyPremiumGuildLastActive, "0", "-1", "WITHSCORES"))
if err != nil {
return nil, errors.WrapIf(err, "zrange")
}
parsed := make(map[int64]time.Time)
for i := 0; i < len(result); i += 2 {
g := result[i]
score := result[i+1]
t := time.Unix(score, 0)
parsed[g] = t
}
return parsed, nil
}
func allGuildsOncePremiumAllPremiumEnabled() (map[int64]time.Time, error) {
var listedServers []int64
err := common.RedisPool.Do(radix.Cmd(&listedServers, "SMEMBERS", "connected_guilds"))
if err != nil {
return nil, err
}
results := make(map[int64]time.Time)
for _, v := range listedServers {
results[v] = time.Now()
}
return results, nil
}
func FindSource(sourceID string) PremiumSource {
for _, v := range PremiumSources {
if _, id := v.Names(); id == sourceID {
return v
}
}
return nil
}
func handleNewPremiumGuild(evt *schEventsModels.ScheduledEvent, data interface{}) (retry bool, err error) {
for _, v := range common.Plugins {
if cast, ok := v.(NewPremiumGuildListener); ok {
err := cast.OnNewPremiumGuild(evt.GuildID)
if err != nil {
return scheduledevents2.CheckDiscordErrRetry(err), err
}
}
}
return false, nil
}
func handleRemovedPremiumGuild(evt *schEventsModels.ScheduledEvent, data interface{}) (retry bool, err error) {
for _, v := range common.Plugins {
if cast, ok := v.(RemovedPremiumGuildListener); ok {
err := cast.OnRemovedPremiumGuild(evt.GuildID)
if err != nil {
return scheduledevents2.CheckDiscordErrRetry(err), err
}
}
}
return false, nil
}
var _ featureflags.PluginWithFeatureFlags = (*Plugin)(nil)
var _ featureflags.PluginWithBatchFeatureFlags = (*Plugin)(nil)
const (
FeatureFlagPremiumPlus = "premium_plus"
FeatureFlagPremiumFull = "premium_full"
)
func (p *Plugin) UpdateFeatureFlags(guildID int64) ([]string, error) {
highestTier := PremiumTierNone
for _, v := range GuildPremiumSources {
tier, err := v.GuildPremiumTier(guildID)
if err != nil {
return nil, errors.WithMessage(err, "GuildPremiumTier")
}
if tier == PremiumTierPremium {
highestTier = tier
} else if highestTier == PremiumTierNone {
highestTier = tier
}
}
return tierFlags(highestTier), nil
}
func (p *Plugin) AllFeatureFlags() []string {
return []string{
FeatureFlagPremiumFull, // set if this server has the highest premium tier
FeatureFlagPremiumPlus, // set if this server has the lower premium tier
}
}
func (p *Plugin) UpdateFeatureFlagsBatch() (map[int64][]string, error) {
// fetch all the guild premium tiers and sort the highest one in each
highestTiers := make(map[int64]PremiumTier)
for _, v := range GuildPremiumSources {
guilds, err := v.AllGuildsPremiumTiers()
if err != nil {
return nil, errors.WithMessage(err, "AllGuildsPremiumTiers")
}
for guildID, tier := range guilds {
if tier == PremiumTierNone {
continue
}
if current, ok := highestTiers[guildID]; ok {
if tier == PremiumTierPremium {
highestTiers[guildID] = tier
} else if current == PremiumTierNone {
highestTiers[guildID] = tier
}
} else {
highestTiers[guildID] = tier
}
}
}
result := make(map[int64][]string)
for guildID, tier := range highestTiers {
guildFlags := tierFlags(tier)
result[guildID] = guildFlags
}
return result, nil
}
func tierFlags(tier PremiumTier) []string {
if tier == PremiumTierPremium {
return []string{FeatureFlagPremiumFull, FeatureFlagPremiumPlus}
} else if tier == PremiumTierPlus {
return []string{FeatureFlagPremiumPlus}
}
return nil
}