/
gateway_config.go
211 lines (186 loc) · 6.45 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
package gateway
import (
"github.com/disgoorg/log"
"github.com/gorilla/websocket"
)
// DefaultConfig returns a Config with sensible defaults.
func DefaultConfig() *Config {
return &Config{
Logger: log.Default(),
Dialer: websocket.DefaultDialer,
LargeThreshold: 50,
Intents: IntentsDefault,
Compress: true,
URL: "wss://gateway.discord.gg",
ShardID: 0,
ShardCount: 1,
AutoReconnect: true,
MaxReconnectTries: 10,
EnableResumeURL: true,
}
}
// Config lets you configure your Gateway instance.
type Config struct {
Logger log.Logger
Dialer *websocket.Dialer
LargeThreshold int
Intents Intents
Compress bool
URL string
ShardID int
ShardCount int
SessionID *string
ResumeGatewayURL *string
LastSequenceReceived *int
AutoReconnect bool
MaxReconnectTries int
EnableRawEvents bool
EnableResumeURL bool
RateLimiter RateLimiter
RateRateLimiterConfigOpts []RateLimiterConfigOpt
Presence *MessageDataPresenceUpdate
OS string
Browser string
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.RateRateLimiterConfigOpts...)
}
}
// WithLogger sets the Logger for the Gateway.
func WithLogger(logger log.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
}
}
// WithMaxReconnectTries sets the maximum number of reconnect attempts before stopping.
func WithMaxReconnectTries(maxReconnectTries int) ConfigOpt {
return func(config *Config) {
config.MaxReconnectTries = maxReconnectTries
}
}
// 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
}
}
// WithRateRateLimiterConfigOpts lets you configure the default RateLimiter.
func WithRateRateLimiterConfigOpts(opts ...RateLimiterConfigOpt) ConfigOpt {
return func(config *Config) {
config.RateRateLimiterConfigOpts = append(config.RateRateLimiterConfigOpts, opts...)
}
}
// WithPresence sets the initial presence the bot should display.
func WithPresence(presence MessageDataPresenceUpdate) ConfigOpt {
return func(config *Config) {
config.Presence = &presence
}
}
// 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
}
}