forked from disgoorg/disgo
/
gateway_config.go
228 lines (203 loc) · 7.58 KB
/
gateway_config.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
package gateway
import (
"log/slog"
"github.com/gorilla/websocket"
)
// DefaultConfig returns a Config with sensible defaults.
func DefaultConfig() *Config {
return &Config{
Logger: slog.Default(),
Dialer: websocket.DefaultDialer,
LargeThreshold: 50,
Intents: IntentsDefault,
Compress: true,
URL: "wss://gateway.discord.gg",
ShardID: 0,
ShardCount: 1,
AutoReconnect: true,
EnableResumeURL: true,
}
}
// Config lets you configure your Gateway instance.
type Config struct {
// Logger is the Logger of the Gateway. Defaults to slog.Default().
Logger *slog.Logger
// Dialer is the websocket.Dialer of the Gateway. Defaults to websocket.DefaultDialer.
Dialer *websocket.Dialer
// LargeThreshold is the threshold for the Gateway. Defaults to 50
// See here for more information: https://discord.com/developers/docs/topics/gateway-events#identify-identify-structure.
LargeThreshold int
// Intents is the Intents for the Gateway. Defaults to IntentsNone.
Intents Intents
// Compress is whether the Gateway should compress payloads. Defaults to true.
Compress bool
// URL is the URL of the Gateway. Defaults to fetch from Discord.
URL string
// ShardID is the shardID of the Gateway. Defaults to 0.
ShardID int
// ShardCount is the shardCount of the Gateway. Defaults to 1.
ShardCount int
// SessionID is the last sessionID of the Gateway. Defaults to nil (no resume).
SessionID *string
// ResumeURL is the last resumeURL of the Gateway. Defaults to nil (no resume).
ResumeURL *string
// LastSequenceReceived is the last sequence received by the Gateway. Defaults to nil (no resume).
LastSequenceReceived *int
// AutoReconnect is whether the Gateway should automatically reconnect or call the CloseHandlerFunc. Defaults to true.
AutoReconnect bool
// EnableRawEvents is whether the Gateway should emit EventRaw. Defaults to false.
EnableRawEvents bool
// EnableResumeURL is whether the Gateway should enable the resumeURL. Defaults to true.
EnableResumeURL bool
// RateLimiter is the RateLimiter of the Gateway. Defaults to NewRateLimiter().
RateLimiter RateLimiter
// RateLimiterConfigOpts is the RateLimiterConfigOpts of the Gateway. Defaults to nil.
RateLimiterConfigOpts []RateLimiterConfigOpt
// Presence is the presence it should send on login. Defaults to nil.
Presence *MessageDataPresenceUpdate
// OS is the OS it should send on login. Defaults to runtime.GOOS.
OS string
// Browser is the Browser it should send on login. Defaults to "disgo".
Browser string
// Device is the Device it should send on login. Defaults to "disgo".
Device string
}
// ConfigOpt is a type alias for a function that takes a Config and is used to configure your Server.
type ConfigOpt func(config *Config)
// Apply applies the given ConfigOpt(s) to the Config
func (c *Config) Apply(opts []ConfigOpt) {
for _, opt := range opts {
opt(c)
}
if c.RateLimiter == nil {
c.RateLimiter = NewRateLimiter(c.RateLimiterConfigOpts...)
}
}
// WithLogger sets the Logger for the Gateway.
func WithLogger(logger *slog.Logger) ConfigOpt {
return func(config *Config) {
config.Logger = logger
}
}
// WithDialer sets the websocket.Dialer for the Gateway.
func WithDialer(dialer *websocket.Dialer) ConfigOpt {
return func(config *Config) {
config.Dialer = dialer
}
}
// WithLargeThreshold sets the threshold for the Gateway.
// See here for more information: https://discord.com/developers/docs/topics/gateway#identify-identify-structure
func WithLargeThreshold(largeThreshold int) ConfigOpt {
return func(config *Config) {
config.LargeThreshold = largeThreshold
}
}
// WithIntents sets the Intents for the Gateway.
// See here for more information: https://discord.com/developers/docs/topics/gateway#gateway-intents
func WithIntents(intents ...Intents) ConfigOpt {
return func(config *Config) {
config.Intents = config.Intents.Add(intents...)
}
}
// WithCompress sets whether this Gateway supports compression.
// See here for more information: https://discord.com/developers/docs/topics/gateway#encoding-and-compression
func WithCompress(compress bool) ConfigOpt {
return func(config *Config) {
config.Compress = compress
}
}
// WithURL sets the Gateway URL for the Gateway.
func WithURL(url string) ConfigOpt {
return func(config *Config) {
config.URL = url
}
}
// WithShardID sets the shard ID for the Gateway.
// See here for more information on sharding: https://discord.com/developers/docs/topics/gateway#sharding
func WithShardID(shardID int) ConfigOpt {
return func(config *Config) {
config.ShardID = shardID
}
}
// WithShardCount sets the shard count for the Gateway.
// See here for more information on sharding: https://discord.com/developers/docs/topics/gateway#sharding
func WithShardCount(shardCount int) ConfigOpt {
return func(config *Config) {
config.ShardCount = shardCount
}
}
// WithSessionID sets the Session ID for the Gateway.
// If sessionID and lastSequence is present while connecting, the Gateway will try to resume the session.
func WithSessionID(sessionID string) ConfigOpt {
return func(config *Config) {
config.SessionID = &sessionID
}
}
// WithSequence sets the last sequence received for the Gateway.
// If sessionID and lastSequence is present while connecting, the Gateway will try to resume the session.
func WithSequence(sequence int) ConfigOpt {
return func(config *Config) {
config.LastSequenceReceived = &sequence
}
}
// WithAutoReconnect sets whether the Gateway should automatically reconnect to Discord.
func WithAutoReconnect(autoReconnect bool) ConfigOpt {
return func(config *Config) {
config.AutoReconnect = autoReconnect
}
}
// WithEnableRawEvents enables/disables the EventTypeRaw.
func WithEnableRawEvents(enableRawEventEvents bool) ConfigOpt {
return func(config *Config) {
config.EnableRawEvents = enableRawEventEvents
}
}
// WithEnableResumeURL enables/disables usage of resume URLs sent by Discord.
func WithEnableResumeURL(enableResumeURL bool) ConfigOpt {
return func(config *Config) {
config.EnableResumeURL = enableResumeURL
}
}
// WithRateLimiter sets the grate.RateLimiter for the Gateway.
func WithRateLimiter(rateLimiter RateLimiter) ConfigOpt {
return func(config *Config) {
config.RateLimiter = rateLimiter
}
}
// WithRateLimiterConfigOpts lets you configure the default RateLimiter.
func WithRateLimiterConfigOpts(opts ...RateLimiterConfigOpt) ConfigOpt {
return func(config *Config) {
config.RateLimiterConfigOpts = append(config.RateLimiterConfigOpts, opts...)
}
}
// WithPresenceOpts allows to pass initial presence data the bot should display.
func WithPresenceOpts(opts ...PresenceOpt) ConfigOpt {
return func(config *Config) {
presenceUpdate := &MessageDataPresenceUpdate{}
for _, opt := range opts {
opt(presenceUpdate)
}
config.Presence = presenceUpdate
}
}
// WithOS sets the operating system the bot is running on.
// See here for more information: https://discord.com/developers/docs/topics/gateway#identify-identify-connection-properties
func WithOS(os string) ConfigOpt {
return func(config *Config) {
config.OS = os
}
}
// WithBrowser sets the browser the bot is running on.
// See here for more information: https://discord.com/developers/docs/topics/gateway#identify-identify-connection-properties
func WithBrowser(browser string) ConfigOpt {
return func(config *Config) {
config.Browser = browser
}
}
// WithDevice sets the device the bot is running on.
// See here for more information: https://discord.com/developers/docs/topics/gateway#identify-identify-connection-properties
func WithDevice(device string) ConfigOpt {
return func(config *Config) {
config.Device = device
}
}