This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guilds.go
85 lines (56 loc) · 1.74 KB
/
guilds.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
package beater
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/elastic/beats/libbeat/logp"
"time"
)
const GuildsToRequest = 100
func updateGuilds(discordUser *DiscordUser) error {
logp.Info("updateGuilds called...")
err := getAllGuilds(discordUser)
if err != nil {
return err
}
if len(discordUser.configuredGuilds) > 0 {
logp.Info(fmt.Sprintf("fetching %d configured guilds", len(discordUser.configuredGuilds)))
getConfiguredGuilds(discordUser)
}
return nil
}
func getAllGuilds(discordUser *DiscordUser) error {
logp.Info("getAllGuilds called...")
guildDiff := time.Now().Sub(discordUser.guildEpoch)
var err error
var newGuilds []*discordgo.UserGuild
if len(discordUser.allGuilds) == 0 || guildDiff.Seconds() > time.Duration(time.Minute*10).Seconds() {
logp.Info("fetching UserGuilds...")
newGuilds, err = discordUser.discordSession.UserGuilds(GuildsToRequest, "", discordUser.latestGuildID)
if err != nil {
return err
}
if len(newGuilds) < GuildsToRequest {
discordUser.guildEpoch = time.Now()
}
}
logp.Info(fmt.Sprintf("Adding %d guilds", len(newGuilds)))
discordUser.allGuilds = append(discordUser.allGuilds, newGuilds...)
return nil
}
func getConfiguredGuilds(discordUser *DiscordUser) {
logp.Info("getConfiguredGuilds called...")
refinedGuilds := []*discordgo.UserGuild{}
for _, guild := range discordUser.allGuilds {
for _, configuredGuild := range discordUser.configuredGuilds {
if configuredGuild.ID == guild.ID {
refinedGuilds = append(refinedGuilds, guild)
break
}
}
if len(refinedGuilds) == len(discordUser.configuredGuilds) {
break
}
}
logp.Info("setting a total of %d guilds on the user", len(refinedGuilds))
discordUser.allGuilds = refinedGuilds
}