-
Notifications
You must be signed in to change notification settings - Fork 910
/
topgames.go
104 lines (86 loc) · 2.16 KB
/
topgames.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
package topgames
import (
"fmt"
"sort"
"github.com/jonas747/dcmd/v4"
"github.com/jonas747/dstate/v4"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/commands"
)
var Command = &commands.YAGCommand{
Cooldown: 5,
CmdCategory: commands.CategoryDebug,
Name: "topgames",
Description: "Shows the top games on this server",
HideFromHelp: true,
ArgSwitches: []*dcmd.ArgDef{
{Name: "all"},
},
RunFunc: cmdFuncTopCommands,
}
func cmdFuncTopCommands(data *dcmd.Data) (interface{}, error) {
allSwitch := data.Switch("all")
var all bool
if allSwitch != nil && allSwitch.Bool() {
all = true
if admin, err := bot.IsBotAdmin(data.Author.ID); !admin || err != nil {
if err != nil {
return nil, err
}
return "Only bot admins can check top games of all servers", nil
}
}
// do it in 2 passes for speedy accumulation of data
fastResult := make(map[string]int)
if all {
processShards := bot.ReadyTracker.GetProcessShards()
for _, shard := range processShards {
guilds := bot.State.GetShardGuilds(int64(shard))
for _, g := range guilds {
checkGuild(fastResult, g)
}
}
} else {
checkGuild(fastResult, data.GuildData.GS)
}
// then we convert and sort it
fullResult := make([]*TopGameResult, 0, len(fastResult))
for k, v := range fastResult {
fullResult = append(fullResult, &TopGameResult{
Game: k,
Count: v,
})
}
sort.Slice(fullResult, func(i, j int) bool {
return fullResult[i].Count > fullResult[j].Count
})
// display it
out := "```\nTop games being played currently\n# Count - Game\n"
for k, result := range fullResult {
out += fmt.Sprintf("#%02d: %5d - %s\n", k+1, result.Count, result.Game)
if k >= 20 {
break
}
}
out += "\n```"
return out, nil
}
func checkGuild(dst map[string]int, gs *dstate.GuildSet) {
bot.State.IterateMembers(gs.ID, func(chunk []*dstate.MemberState) bool {
for _, ms := range chunk {
if ms.Presence == nil || ms.Presence.Game == nil || ms.Presence.Game.Name == "" {
continue
}
if ms.User.Bot {
continue
}
name := ms.Presence.Game.Name
dst[name]++
}
return true
})
}
type TopGameResult struct {
Game string
Count int
}