-
Notifications
You must be signed in to change notification settings - Fork 3
/
events.go
240 lines (185 loc) · 7.06 KB
/
events.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
package internal
import (
"context"
"errors"
"runtime/debug"
"sync"
"time"
"github.com/WelcomerTeam/Discord/discord"
sandwich_structs "github.com/WelcomerTeam/Sandwich-Daemon/structs"
"github.com/savsgio/gotils/strconv"
"github.com/savsgio/gotils/strings"
)
// List of handlers for gateway events.
var gatewayHandlers = make(map[discord.GatewayOp]func(ctx context.Context, sh *Shard, msg discord.GatewayPayload, trace sandwich_structs.SandwichTrace) error)
// List of handlers for dispatch events.
var dispatchHandlers = make(map[string]func(ctx *StateCtx, msg discord.GatewayPayload, trace sandwich_structs.SandwichTrace) (result sandwich_structs.StateResult, ok bool, err error))
type StateCtx struct {
CacheUsers bool
CacheMembers bool
Stateless bool
StoreMutuals bool
context context.Context
*Shard
}
// SandwichState stores the collective state of all ShardGroups
// across all Managers.
type SandwichState struct {
guildsMu sync.RWMutex
Guilds map[discord.Snowflake]*sandwich_structs.StateGuild
guildMembersMu sync.RWMutex
GuildMembers map[discord.Snowflake]*sandwich_structs.StateGuildMembers
guildChannelsMu sync.RWMutex
GuildChannels map[discord.Snowflake]*sandwich_structs.StateGuildChannels
guildRolesMu sync.RWMutex
GuildRoles map[discord.Snowflake]*sandwich_structs.StateGuildRoles
guildEmojisMu sync.RWMutex
GuildEmojis map[discord.Snowflake]*sandwich_structs.StateGuildEmojis
usersMu sync.RWMutex
Users map[discord.Snowflake]*sandwich_structs.StateUser
dmChannelsMu sync.RWMutex
dmChannels map[discord.Snowflake]*sandwich_structs.StateDMChannel
mutualsMu sync.RWMutex
Mutuals map[discord.Snowflake]*sandwich_structs.StateMutualGuilds
guildVoiceStatesMu sync.RWMutex
GuildVoiceStates map[discord.Snowflake]*sandwich_structs.StateGuildVoiceStates
}
func NewSandwichState() *SandwichState {
state := &SandwichState{
guildsMu: sync.RWMutex{},
Guilds: make(map[discord.Snowflake]*sandwich_structs.StateGuild),
guildMembersMu: sync.RWMutex{},
GuildMembers: make(map[discord.Snowflake]*sandwich_structs.StateGuildMembers),
guildChannelsMu: sync.RWMutex{},
GuildChannels: make(map[discord.Snowflake]*sandwich_structs.StateGuildChannels),
guildRolesMu: sync.RWMutex{},
GuildRoles: make(map[discord.Snowflake]*sandwich_structs.StateGuildRoles),
guildEmojisMu: sync.RWMutex{},
GuildEmojis: make(map[discord.Snowflake]*sandwich_structs.StateGuildEmojis),
usersMu: sync.RWMutex{},
Users: make(map[discord.Snowflake]*sandwich_structs.StateUser),
dmChannelsMu: sync.RWMutex{},
dmChannels: make(map[discord.Snowflake]*sandwich_structs.StateDMChannel),
mutualsMu: sync.RWMutex{},
Mutuals: make(map[discord.Snowflake]*sandwich_structs.StateMutualGuilds),
guildVoiceStatesMu: sync.RWMutex{},
GuildVoiceStates: make(map[discord.Snowflake]*sandwich_structs.StateGuildVoiceStates),
}
return state
}
func (sh *Shard) OnEvent(ctx context.Context, msg discord.GatewayPayload, trace sandwich_structs.SandwichTrace) {
err := GatewayDispatch(ctx, sh, msg, trace)
if err != nil {
if errors.Is(err, ErrNoGatewayHandler) {
sh.Logger.Warn().
Int("op", int(msg.Op)).
Str("type", msg.Type).
Msg("Gateway sent unknown packet")
}
}
}
// OnDispatch handles routing of discord event.
func (sh *Shard) OnDispatch(ctx context.Context, msg discord.GatewayPayload, trace sandwich_structs.SandwichTrace) error {
defer func() {
if r := recover(); r != nil {
errorMessage, ok := r.(error)
if ok {
sh.Logger.Error().
Err(errorMessage).
Int("op", int(msg.Op)).
Str("type", msg.Type).
Int("seq", int(msg.Sequence)).
Bytes("data", msg.Data).
Msg("Recovered panic in OnDispatch")
} else {
sh.Logger.Error().
Str("err", "[unknown]").
Int("op", int(msg.Op)).
Str("type", msg.Type).
Int("seq", int(msg.Sequence)).
Bytes("data", msg.Data).
Msg("Recovered panic in OnDispatch")
}
println(string(debug.Stack()))
}
}()
if sh.Manager.ProducerClient == nil {
return ErrProducerMissing
}
sh.Manager.eventBlacklistMu.RLock()
contains := strings.Include(sh.Manager.eventBlacklist, msg.Type)
sh.Manager.eventBlacklistMu.RUnlock()
if contains {
return nil
}
sh.Manager.configurationMu.RLock()
cacheUsers := sh.Manager.Configuration.Caching.CacheUsers
cacheMembers := sh.Manager.Configuration.Caching.CacheMembers
storeMutuals := sh.Manager.Configuration.Caching.StoreMutuals
sh.Manager.configurationMu.RUnlock()
trace["state"] = discord.Int64(time.Now().Unix())
result, continuable, err := StateDispatch(&StateCtx{
context: ctx,
Shard: sh,
CacheUsers: cacheUsers,
CacheMembers: cacheMembers,
StoreMutuals: storeMutuals,
}, msg, trace)
if err != nil {
if !errors.Is(err, ErrNoDispatchHandler) {
sh.Logger.Error().Err(err).Str("data", strconv.B2S(msg.Data)).Msg("Encountered error whilst handling " + msg.Type)
}
return err
}
sh.ShardGroup.floodgateMu.RLock()
floodgate := sh.ShardGroup.floodgate
sh.ShardGroup.floodgateMu.RUnlock()
if !floodgate || !continuable {
return nil
}
sh.Manager.produceBlacklistMu.RLock()
contains = strings.Include(sh.Manager.produceBlacklist, msg.Type)
sh.Manager.produceBlacklistMu.RUnlock()
if contains {
return nil
}
packet, _ := sh.Sandwich.payloadPool.Get().(*sandwich_structs.SandwichPayload)
defer sh.Sandwich.payloadPool.Put(packet)
// Directly copy op, sequence and type from original message.
packet.Op = msg.Op
packet.Sequence = msg.Sequence
packet.Type = msg.Type
// Setting result.Data will override what is sent to consumers.
packet.Data = result.Data
// Extra contains any extra information such as before state and if it is a lazy guild.
packet.Extra = result.Extra
packet.Trace = trace
return sh.PublishEvent(ctx, packet)
}
func registerGatewayEvent(op discord.GatewayOp, handler func(ctx context.Context, sh *Shard, msg discord.GatewayPayload, trace sandwich_structs.SandwichTrace) error) {
gatewayHandlers[op] = handler
}
func registerDispatch(eventType string, handler func(ctx *StateCtx, msg discord.GatewayPayload, trace sandwich_structs.SandwichTrace) (result sandwich_structs.StateResult, ok bool, err error)) {
dispatchHandlers[eventType] = handler
}
// GatewayDispatch handles selecting the proper gateway handler and executing it.
func GatewayDispatch(ctx context.Context, sh *Shard,
event discord.GatewayPayload, trace sandwich_structs.SandwichTrace,
) error {
if f, ok := gatewayHandlers[event.Op]; ok {
return f(ctx, sh, event, trace)
}
sh.Logger.Warn().Int("op", int(event.Op)).Msg("No gateway handler found")
return ErrNoGatewayHandler
}
// StateDispatch handles selecting the proper state handler and executing it.
func StateDispatch(ctx *StateCtx,
event discord.GatewayPayload, trace sandwich_structs.SandwichTrace,
) (result sandwich_structs.StateResult, ok bool, err error) {
if f, ok := dispatchHandlers[event.Type]; ok {
ctx.Logger.Trace().Str("type", event.Type).Msg("State Dispatch")
return f(ctx, event, trace)
}
ctx.Logger.Warn().Str("type", event.Type).Msg("No dispatch handler found")
return result, false, ErrNoDispatchHandler
}