-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
157 lines (132 loc) · 3.93 KB
/
options.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
package gateway
import (
"errors"
"github.com/discordpkg/gateway/event"
"github.com/discordpkg/gateway/intent"
"github.com/discordpkg/gateway/internal/util"
)
// Option for initializing a new gateway client. An option must be deterministic regardless
// of when or how many times it is executed.
type Option func(client *Client) error
var noopOption = func(_ *Client) error {
return nil
}
func WithBotToken(token string) Option {
return func(client *Client) error {
client.botToken = token
return nil
}
}
func WithDirectMessageEvents(events ...event.Type) Option {
set := util.Set[event.Type]{}
set.Add(events...)
deduplicated := set.ToSlice()
return func(client *Client) error {
if len(deduplicated) != len(events) {
return errors.New("duplicated direct message events found")
}
client.intents |= intent.DMEventsToIntents(deduplicated)
client.allowlist.Add(deduplicated...) // apply prune optimization
return nil
}
}
func WithGuildEvents(events ...event.Type) Option {
set := util.Set[event.Type]{}
set.Add(events...)
deduplicated := set.ToSlice()
return func(client *Client) error {
if len(deduplicated) != len(events) {
return errors.New("duplicated guild events found")
}
client.intents |= intent.GuildEventsToIntents(deduplicated)
client.allowlist.Add(deduplicated...) // apply prune optimization
return nil
}
}
func WithIntents(intents intent.Type) Option {
return func(client *Client) error {
if client.allowlist != nil {
return errors.New("'Intents' can not be used along with 'DirectMessageEvents' and/or 'GuildEvents'")
}
client.intents = intents
client.allowlist.Add(intent.Events(intents)...)
return nil
}
}
func WithShardInfo(id ShardID, count int) Option {
if count < 0 {
panic("shard count must be above 0")
}
return func(client *Client) error {
client.id = id
client.totalNumberOfShards = count
return nil
}
}
func WithExistingSession(deadClient *Client) Option {
if deadClient == nil {
return noopOption
}
return func(client *Client) error {
st, ok := deadClient.ctx.state.(*ResumableClosedState)
if !ok {
// panic("the existing client did not have a valid session saved")
// TODO: is this bad form?
return nil
}
client.ctx.SessionID = st.ctx.SessionID
client.ctx.ResumeGatewayURL = st.ctx.ResumeGatewayURL
client.ctx.sequenceNumber.Store(st.ctx.sequenceNumber.Load())
client.ctx.SetState(&ResumeState{&ConnectedState{ctx: client.ctx}})
return nil
}
}
func WithIdentifyConnectionProperties(properties *IdentifyConnectionProperties) Option {
return func(client *Client) error {
client.connectionProperties = properties
return nil
}
}
func WithCommandRateLimiter(ratelimiter RateLimiter) Option {
return func(client *Client) error {
client.commandRateLimiter = ratelimiter
return nil
}
}
func WithIdentifyRateLimiter(ratelimiter RateLimiter) Option {
return func(client *Client) error {
client.identifyRateLimiter = ratelimiter
return nil
}
}
// WithHeartbeatHandler allows overwriting default heartbeat behavior.
// Basic behavior is achieved with the DefaultHeartbeatHandler:
//
// NewClient(
// WithHeartbeatHandler(&DefaultHeartbeatHandler{
// TextWriter:
// })
// )
func WithHeartbeatHandler(handler HeartbeatHandler) Option {
return func(client *Client) error {
client.heartbeatHandler = handler
return nil
}
}
// WithEventHandler provides a callback that is triggered on incoming events. Note that the allowlist will filter
// out events you have not requested.
//
// Warning: this function call is blocking. You should not run heavy logic in the handler, preferably just forward it
// to a processing component. An example usage would be to send it to a buffered worker channel.
func WithEventHandler(handler Handler) Option {
return func(client *Client) error {
client.eventHandler = handler
return nil
}
}
func WithLogger(logger Logger) Option {
return func(client *Client) error {
client.logger = logger
return nil
}
}