-
Notifications
You must be signed in to change notification settings - Fork 911
/
discordevents.go
207 lines (171 loc) · 5.42 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
package bot
import (
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/scheduledevents"
"github.com/mediocregopher/radix.v2/redis"
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(waitingGuilds) < 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")
for _, p := range common.Plugins {
starter, ok := p.(BotStartedHandler)
if ok {
starter.BotStarted()
log.Debug("Ran BotStarted for ", p.Name())
}
}
go scheduledevents.Run()
go checkConnectedGuilds()
}
}
func HandleReady(evt *eventsystem.EventData) {
waitingGuildsMU.Lock()
for i, v := range waitingReadies {
if ContextSession(evt.Context()).ShardID == v {
waitingReadies = append(waitingReadies[:i], waitingReadies[i+1:]...)
break
}
}
for _, v := range evt.Ready.Guilds {
waitingGuilds[v.ID] = true
}
waitingGuildsMU.Unlock()
ContextSession(evt.Context()).UpdateStatus(0, "v"+common.VERSION+" :)")
// 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.Ready.Version,
SessionID: evt.Ready.SessionID,
User: evt.Ready.User,
}
common.BotSession.State.Ready = ready
common.BotSession.State.Unlock()
}
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)
n, err := ContextRedis(evt.Context()).Cmd("SADD", "connected_guilds", g.ID).Int()
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{
EventDataContainer: &eventsystem.EventDataContainer{
GuildCreate: g,
},
Type: eventsystem.EventNewGuild,
}, eventsystem.EventNewGuild)
}
banned, _ := common.RedisBool(ContextRedis(evt.Context()).Cmd("SISMEMBER", "banned_servers", 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
}
log.WithFields(log.Fields{
"g_name": evt.GuildDelete.Name,
}).Info("Left guild")
client := ContextRedis(evt.Context())
err := client.Cmd("SREM", "connected_guilds", evt.GuildDelete.ID).Err
if err != nil {
log.WithError(err).Error("Redis error")
}
go EmitGuildRemoved(evt.GuildDelete.ID)
}
// Makes sure the member is always in state when coming online
func HandlePresenceUpdate(evt *eventsystem.EventData) {
p := evt.PresenceUpdate
if p.Status == discordgo.StatusOffline {
return
}
gs := State.Guild(true, p.GuildID)
if gs == nil {
return
}
m := gs.Member(true, p.User.ID)
if m != nil && m.Member != nil {
return
}
GetMember(p.GuildID, p.User.ID)
}
// 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(ContextRedis(evt.Context()), evt.GuildUpdate.Guild.ID, 0)
}
func HandleGuildRoleUpdate(evt *eventsystem.EventData) {
InvalidateCache(ContextRedis(evt.Context()), evt.GuildRoleUpdate.GuildID, 0)
}
func HandleGuildRoleCreate(evt *eventsystem.EventData) {
InvalidateCache(ContextRedis(evt.Context()), evt.GuildRoleCreate.GuildID, 0)
}
func HandleGuildRoleRemove(evt *eventsystem.EventData) {
InvalidateCache(ContextRedis(evt.Context()), evt.GuildRoleDelete.GuildID, 0)
}
func HandleChannelCreate(evt *eventsystem.EventData) {
InvalidateCache(ContextRedis(evt.Context()), evt.ChannelCreate.GuildID, 0)
}
func HandleChannelUpdate(evt *eventsystem.EventData) {
InvalidateCache(ContextRedis(evt.Context()), evt.ChannelUpdate.GuildID, 0)
}
func HandleChannelDelete(evt *eventsystem.EventData) {
InvalidateCache(ContextRedis(evt.Context()), evt.ChannelDelete.GuildID, 0)
}
func HandleGuildMemberUpdate(evt *eventsystem.EventData) {
InvalidateCache(ContextRedis(evt.Context()), 0, evt.GuildMemberUpdate.User.ID)
}
func InvalidateCache(client *redis.Client, guildID, userID int64) {
if userID != 0 {
client.Cmd("DEL", common.CacheKeyPrefix+discordgo.StrID(userID)+":guilds")
}
if guildID != 0 {
client.Cmd("DEL", common.CacheKeyPrefix+common.KeyGuild(guildID))
client.Cmd("DEL", common.CacheKeyPrefix+common.KeyGuildChannels(guildID))
}
}
func ConcurrentEventHandler(inner eventsystem.Handler) eventsystem.Handler {
return func(evt *eventsystem.EventData) {
go inner(evt)
}
}