-
Notifications
You must be signed in to change notification settings - Fork 927
/
plugin_bot.go
295 lines (233 loc) · 8.07 KB
/
plugin_bot.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
package serverstats
import (
"context"
"fmt"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/pubsub"
"github.com/jonas747/yagpdb/web"
"github.com/mediocregopher/radix"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"time"
)
func MarkGuildAsToBeChecked(guildID int64) {
common.RedisPool.Do(radix.FlatCmd(nil, "SADD", "serverstats_active_guilds", guildID))
}
var (
_ bot.BotInitHandler = (*Plugin)(nil)
_ commands.CommandProvider = (*Plugin)(nil)
)
func (p *Plugin) BotInit() {
eventsystem.AddHandler(HandleMemberAdd, eventsystem.EventGuildMemberAdd)
eventsystem.AddHandler(HandleMemberRemove, eventsystem.EventGuildMemberRemove)
eventsystem.AddHandler(HandleMessageCreate, eventsystem.EventMessageCreate)
eventsystem.AddHandler(HandleGuildCreate, eventsystem.EventGuildCreate)
pubsub.AddHandler("server_stats_invalidate_cache", func(evt *pubsub.Event) {
gs := bot.State.Guild(true, evt.TargetGuildInt)
if gs != nil {
gs.UserCacheDel(true, CacheKeyConfig)
}
}, nil)
go p.runOnlineUpdater()
}
func (p *Plugin) AddCommands() {
commands.AddRootCommands(&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryTool,
Cooldown: 5,
Name: "Stats",
Description: "Shows server stats (if public stats are enabled)",
RunFunc: func(data *dcmd.Data) (interface{}, error) {
config, err := GetConfig(data.Context(), data.GS.ID)
if err != nil {
return nil, errors.WithMessage(err, "getconfig")
}
if !config.Public {
return fmt.Sprintf("Stats are set to private on this server, this can be changed in the control panel on <https://%s>", common.Conf.Host), nil
}
stats, err := RetrieveDailyStats(data.GS.ID)
if err != nil {
return nil, errors.WithMessage(err, "retrievefullstats")
}
total := int64(0)
for _, c := range stats.ChannelMessages {
total += c.Count
}
embed := &discordgo.MessageEmbed{
Title: "Server stats",
Description: fmt.Sprintf("[Click here to open in browser](%s/public/%d/stats)", web.BaseURL(), data.GS.ID),
Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField{Name: "Members joined 24h", Value: fmt.Sprint(stats.JoinedDay), Inline: true},
&discordgo.MessageEmbedField{Name: "Members Left 24h", Value: fmt.Sprint(stats.LeftDay), Inline: true},
&discordgo.MessageEmbedField{Name: "Total Messages 24h", Value: fmt.Sprint(total), Inline: true},
&discordgo.MessageEmbedField{Name: "Members Online", Value: fmt.Sprint(stats.Online), Inline: true},
&discordgo.MessageEmbedField{Name: "Total Members", Value: fmt.Sprint(stats.TotalMembers), Inline: true},
},
}
return embed, nil
},
})
}
func HandleGuildCreate(evt *eventsystem.EventData) {
g := evt.GuildCreate()
SetUpdateMemberStatsPeriod(g.ID, 0, g.MemberCount)
}
func HandleMemberAdd(evt *eventsystem.EventData) {
g := evt.GuildMemberAdd()
gs := bot.State.Guild(true, g.GuildID)
if gs == nil {
return
}
gs.RLock()
mc := gs.Guild.MemberCount
gs.RUnlock()
SetUpdateMemberStatsPeriod(g.GuildID, 1, mc)
common.LogIgnoreError(common.RedisPool.Do(radix.FlatCmd(nil, "SADD", RedisKeyGuildMembersChanged, g.GuildID)),
"[serverstats] failed marking guildmembers changed", logrus.Fields{"guild": g.GuildID})
}
func HandleMemberRemove(evt *eventsystem.EventData) {
g := evt.GuildMemberRemove()
gs := bot.State.Guild(true, g.GuildID)
if gs == nil {
return
}
gs.RLock()
mc := gs.Guild.MemberCount
gs.RUnlock()
SetUpdateMemberStatsPeriod(g.GuildID, -1, mc)
common.LogIgnoreError(common.RedisPool.Do(radix.FlatCmd(nil, "SADD", RedisKeyGuildMembersChanged, g.GuildID)),
"[serverstats] failed marking guildmembers changed", logrus.Fields{"guild": g.GuildID})
}
func SetUpdateMemberStatsPeriod(guildID int64, memberIncr int, numMembers int) {
joins := 0
leaves := 0
if memberIncr > 0 {
joins = memberIncr
} else if memberIncr < 0 {
leaves = -memberIncr
}
// round to current hour
t := RoundHour(time.Now())
_, err := common.PQ.Exec(`INSERT INTO server_stats_member_periods (guild_id, created_at, num_members, joins, leaves, max_online)
VALUES ($1, $2, $3, $4, $5, 0)
ON CONFLICT (guild_id, created_at)
DO UPDATE SET
joins = server_stats_member_periods.joins + $4,
leaves = server_stats_member_periods.leaves + $5,
num_members = server_stats_member_periods.num_members + $6;`, guildID, t, numMembers, joins, leaves, memberIncr) // update clause vars
if err != nil {
logger.WithError(err).Error("failed setting member stats period")
}
}
func HandleMessageCreate(evt *eventsystem.EventData) {
m := evt.MessageCreate()
if m.GuildID == 0 || m.Author == nil || m.Author.Bot {
return // private channel
}
channel := bot.State.Channel(true, m.ChannelID)
if channel == nil {
logger.WithField("channel", m.ChannelID).Warn("Channel not in state")
return
}
config, err := BotCachedFetchGuildConfig(evt.Context(), channel.Guild)
if err != nil {
logger.WithError(err).WithField("guild", channel.Guild.ID).Error("Failed retrieving config")
return
}
if common.ContainsInt64Slice(config.ParsedChannels, channel.ID) {
return
}
val := channel.StrID() + ":" + discordgo.StrID(m.ID) + ":" + discordgo.StrID(m.Author.ID)
err = common.RedisPool.Do(radix.FlatCmd(nil, "ZADD", "guild_stats_msg_channel_day:"+channel.Guild.StrID(), time.Now().Unix(), val))
if err != nil {
logger.WithError(err).Error("Failed adding member to stats")
}
MarkGuildAsToBeChecked(channel.Guild.ID)
}
type CacheKey int
const (
CacheKeyConfig CacheKey = iota
)
func BotCachedFetchGuildConfig(ctx context.Context, gs *dstate.GuildState) (*ServerStatsConfig, error) {
v, err := gs.UserCacheFetch(true, CacheKeyConfig, func() (interface{}, error) {
return GetConfig(ctx, gs.ID)
})
if err != nil {
return nil, err
}
return v.(*ServerStatsConfig), nil
}
func (p *Plugin) runOnlineUpdater() {
ticker := time.NewTicker(time.Second)
state := bot.State
var guildsToCheck []*dstate.GuildState
var i int
var numToCheckPerRun int
for {
select {
case <-ticker.C:
}
if len(guildsToCheck) < 0 || i >= len(guildsToCheck) {
// Copy the list of guilds so that we dont need to keep the entire state locked
i = 0
guildsToCheck = state.GuildsSlice(true)
// Hit each guild once per hour more or less
numToCheckPerRun = len(guildsToCheck) / 2500
if numToCheckPerRun < 1 {
numToCheckPerRun = 1
}
}
started := time.Now()
totalCounts := make(map[int64][2]int)
checkedThisRound := 0
for ; i < len(guildsToCheck) && checkedThisRound < numToCheckPerRun; i++ {
g := guildsToCheck[i]
online, total := p.checkGuildOnlineCount(g)
totalCounts[g.ID] = [2]int{online, total}
checkedThisRound++
}
t := RoundHour(time.Now())
tx, err := common.PQ.Begin()
if err != nil {
logger.WithError(err).Error("[serverstats] failed starting online count transaction")
continue
}
for g, counts := range totalCounts {
_, err := tx.Exec(`INSERT INTO server_stats_member_periods (guild_id, created_at, num_members, joins, leaves, max_online)
VALUES ($1, $2, $3, 0, 0, $4)
ON CONFLICT (guild_id, created_at)
DO UPDATE SET
max_online = GREATEST (server_stats_member_periods.max_online, $4)
`, g, t, counts[1], counts[0]) // update clause vars
if err != nil {
logger.WithError(err).WithField("guild", g).Error("failed checking guild online count")
tx.Rollback()
break
}
}
err = tx.Commit()
if err != nil {
logger.WithError(err).Error("failed comitting online counts")
}
if time.Since(started) > time.Millisecond*250 {
logger.Warnf("Tok %s to update online counts of %d guilds", time.Since(started).String(), checkedThisRound)
}
}
}
func (p *Plugin) checkGuildOnlineCount(guild *dstate.GuildState) (online int, total int) {
guild.RLock()
total = guild.Guild.MemberCount
for _, v := range guild.Members {
if v.PresenceSet && v.PresenceStatus != dstate.StatusOffline {
online++
}
}
guild.RUnlock()
return online, total
}