forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discordevents.go
237 lines (192 loc) · 5.96 KB
/
discordevents.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
package bot
import (
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/master"
"github.com/mediocregopher/radix.v3"
log "github.com/sirupsen/logrus"
"sync"
"sync/atomic"
)
var (
waitingGuildsMU sync.Mutex
waitingGuilds = make(map[int64]bool)
waitingReadies []int
botStartedFired = new(int32)
)
// Once we have received a guild create for all guilds
// We fire BotStarted
func setWaitingGuildReady(g int64) {
if atomic.LoadInt32(botStartedFired) == 1 {
return
}
waitingGuildsMU.Lock()
delete(waitingGuilds, g)
shouldFireStarted := len(waitingReadies) < 1
// Some shards aren't ready yet
if len(waitingReadies) > 0 {
shouldFireStarted = false
}
if shouldFireStarted {
atomic.StoreInt32(botStartedFired, 1)
}
waitingGuildsMU.Unlock()
if shouldFireStarted {
log.Println("Bot is now fully ready")
stateLock.Lock()
currentState := state
stateLock.Unlock()
if currentState == StateSoftStarting {
SlaveClient.Send(master.EvtSoftStartComplete, nil, true)
}
BotStarted()
}
}
func HandleReady(data *eventsystem.EventData) {
evt := data.Ready()
waitingGuildsMU.Lock()
for i, v := range waitingReadies {
if ContextSession(data.Context()).ShardID == v {
waitingReadies = append(waitingReadies[:i], waitingReadies[i+1:]...)
break
}
}
for _, v := range evt.Guilds {
waitingGuilds[v.ID] = true
}
waitingGuildsMU.Unlock()
currentPresenceLock.Lock()
streamingURL := currentStreamingURL
status := currentStatus
currentPresenceLock.Unlock()
if streamingURL != "" {
ContextSession(data.Context()).UpdateStreamingStatus(0, status, streamingURL)
} else {
ContextSession(data.Context()).UpdateStatus(0, status)
}
// We pass the common.Session to the command system and that needs the user from the state
common.BotSession.State.Lock()
ready := discordgo.Ready{
Version: evt.Version,
SessionID: evt.SessionID,
User: evt.User,
}
common.BotSession.State.Ready = ready
common.BotSession.State.Unlock()
var listedServers []int64
err := common.RedisPool.Do(radix.Cmd(&listedServers, "SMEMBERS", "connected_guilds"))
if err != nil {
log.WithError(err).Error("Failed retrieving connected servers")
}
numShards := ShardManager.GetNumShards()
OUTER:
for _, v := range listedServers {
shard := (v >> 22) % int64(numShards)
if int(shard) != data.Session.ShardID {
continue
}
for _, readyGuild := range evt.Guilds {
if readyGuild.ID == v {
continue OUTER
}
}
log.Info("Left server while bot was down: ", v)
common.RedisPool.Do(radix.Cmd(nil, "SREM", "connected_guilds", discordgo.StrID(v)))
go EmitGuildRemoved(v)
if common.Statsd != nil {
common.Statsd.Incr("yagpdb.left_guilds", nil, 1)
}
}
}
func HandleGuildCreate(evt *eventsystem.EventData) {
g := evt.GuildCreate()
log.WithFields(log.Fields{
"g_name": g.Name,
"guild": g.ID,
}).Debug("Joined guild")
setWaitingGuildReady(g.ID)
var n int
err := common.RedisPool.Do(radix.Cmd(&n, "SADD", "connected_guilds", discordgo.StrID(g.ID)))
if err != nil {
log.WithError(err).Error("Redis error")
}
if n > 0 {
log.WithField("g_name", g.Name).WithField("guild", g.ID).Info("Joined new guild!")
go eventsystem.EmitEvent(&eventsystem.EventData{
EvtInterface: g,
Type: eventsystem.EventNewGuild,
}, eventsystem.EventNewGuild)
if common.Statsd != nil {
common.Statsd.Incr("yagpdb.joined_guilds", nil, 1)
}
}
var banned bool
common.RedisPool.Do(radix.Cmd(&banned, "SISMEMBER", "banned_servers", discordgo.StrID(g.ID)))
if banned {
log.WithField("guild", g.ID).Info("Banned server tried to add bot back")
common.BotSession.ChannelMessageSend(g.ID, "This server is banned from using this bot. Join the support server for more info.")
common.BotSession.GuildLeave(g.ID)
}
}
func HandleGuildDelete(evt *eventsystem.EventData) {
if evt.GuildDelete().Unavailable {
// Just a guild outage
return
}
setWaitingGuildReady(evt.GuildDelete().ID)
log.WithFields(log.Fields{
"g_name": evt.GuildDelete().Name,
}).Info("Left guild")
err := common.RedisPool.Do(radix.Cmd(nil, "SREM", "connected_guilds", discordgo.StrID(evt.GuildDelete().ID)))
if err != nil {
log.WithError(err).Error("Redis error")
}
go EmitGuildRemoved(evt.GuildDelete().ID)
if common.Statsd != nil {
common.Statsd.Incr("yagpdb.left_guilds", nil, 1)
}
}
// StateHandler updates the world state
// use AddHandlerBefore to add handler before this one, otherwise they will alwyas be after
func StateHandler(evt *eventsystem.EventData) {
State.HandleEvent(ContextSession(evt.Context()), evt.EvtInterface)
}
func HandleGuildUpdate(evt *eventsystem.EventData) {
InvalidateCache(evt.GuildUpdate().Guild.ID, 0)
}
func HandleGuildRoleUpdate(evt *eventsystem.EventData) {
InvalidateCache(evt.GuildRoleUpdate().GuildID, 0)
}
func HandleGuildRoleCreate(evt *eventsystem.EventData) {
InvalidateCache(evt.GuildRoleCreate().GuildID, 0)
}
func HandleGuildRoleRemove(evt *eventsystem.EventData) {
InvalidateCache(evt.GuildRoleDelete().GuildID, 0)
}
func HandleChannelCreate(evt *eventsystem.EventData) {
InvalidateCache(evt.ChannelCreate().GuildID, 0)
}
func HandleChannelUpdate(evt *eventsystem.EventData) {
InvalidateCache(evt.ChannelUpdate().GuildID, 0)
}
func HandleChannelDelete(evt *eventsystem.EventData) {
InvalidateCache(evt.ChannelDelete().GuildID, 0)
}
func HandleGuildMemberUpdate(evt *eventsystem.EventData) {
InvalidateCache(0, evt.GuildMemberUpdate().User.ID)
}
func InvalidateCache(guildID, userID int64) {
if userID != 0 {
common.RedisPool.Do(radix.Cmd(nil, "DEL", common.CacheKeyPrefix+discordgo.StrID(userID)+":guilds"))
}
if guildID != 0 {
common.RedisPool.Do(radix.Cmd(nil, "DEL", common.CacheKeyPrefix+common.KeyGuild(guildID)))
common.RedisPool.Do(radix.Cmd(nil, "DEL", common.CacheKeyPrefix+common.KeyGuildChannels(guildID)))
}
}
func ConcurrentEventHandler(inner eventsystem.Handler) eventsystem.Handler {
return func(evt *eventsystem.EventData) {
go inner(evt)
}
}