-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmanager.go
265 lines (224 loc) · 6.17 KB
/
manager.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package shards
import (
"fmt"
"sync"
"time"
"github.com/bwmarrin/discordgo"
)
// Manager facilitates the management of Shards.
type Manager struct {
sync.RWMutex
// Discord gateway.
Gateway *discordgo.Session
// Discord intent.
Intent discordgo.Intent
// Shards managed by this Manager.
Shards []*Shard
// Total Shard count.
ShardCount int
// Event handlers.
handlers []interface{}
// Discord bot token.
token string
// Should state tracking be enabled.
stateEnabled bool
}
// AddHandler registers an event handler for all Shards.
func (m *Manager) AddHandler(handler interface{}) {
m.Lock()
m.handlers = append(m.handlers, handler)
m.Unlock()
m.apply(func(shard *Shard) {
shard.AddHandler(handler)
})
}
// ApplicationCommandCreate registers an application command for all Shards.
func (m *Manager) ApplicationCommandCreate(guildID string, cmd *discordgo.ApplicationCommand) (errs []error) {
m.apply(func(shard *Shard) {
err := shard.ApplicationCommandCreate(guildID, cmd)
if err != nil {
errs = append(errs, err)
}
})
return
}
// ApplicationCommandBulkOverwrite registers a series of application commands for all Shards,
// overwriting existing commands.
func (m *Manager) ApplicationCommandBulkOverwrite(guildID string, cmds []*discordgo.ApplicationCommand) (errs []error) {
m.apply(func(shard *Shard) {
err := shard.ApplicationCommandBulkOverwrite(guildID, cmds)
if err != nil {
errs = append(errs, err)
}
})
return
}
// ApplicationCommandDelete deregisters an application command for all Shards.
func (m *Manager) ApplicationCommandDelete(guildID string, cmd *discordgo.ApplicationCommand) (errs []error) {
m.apply(func(shard *Shard) {
err := shard.ApplicationCommandDelete(guildID, cmd)
if err != nil {
errs = append(errs, err)
}
})
return
}
// GuildCount returns the amount of guilds that a Manager's Shards are handling.
func (m *Manager) GuildCount() (count int) {
m.apply(func(shard *Shard) {
count += shard.GuildCount()
})
return
}
// New creates a new Manager with the recommended number of shards.
// After calling New, call Start to begin connecting the shards.
//
// Example:
// mgr := shards.New("Bot TOKEN")
func New(token string) (mgr *Manager, err error) {
return NewWithConfig(token, DefaultConfig())
}
// NewWithConfig creates a new Manager with the provided configuration.
// After calling NewWithConfig, call Start to begin connecting the shards.
func NewWithConfig(token string, config *Config) (mgr *Manager, err error) {
if config == nil {
return nil, fmt.Errorf("error: configuration cannot be nil")
}
// Initialize the Manager with provided configuration.
mgr = &Manager{
stateEnabled: config.StateEnabled,
token: token,
Intent: config.Intent,
}
// Initialize the gateway.
mgr.Gateway, err = discordgo.New(token)
if err != nil {
return
}
resp, err := mgr.Gateway.GatewayBot()
if err != nil {
return nil, fmt.Errorf("error: failed to fetch recommended shard count: %v", err)
}
if config.ShardCount > 0 && config.ShardCount > resp.Shards {
mgr.SetShardCount(config.ShardCount)
} else {
mgr.SetShardCount(resp.Shards)
}
return
}
// SetShardCount sets the shard count.
// The new shard count won't take effect until the Manager is restarted.
func (m *Manager) SetShardCount(count int) {
if count < 1 {
return
}
m.Lock()
m.ShardCount = count
m.Unlock()
}
// RegisterIntent sets the Intent for all Shards' sessions.
//
// Note: Changing the intent will not take effect until the Manager is restarted.
func (m *Manager) RegisterIntent(intent discordgo.Intent) {
m.Lock()
m.Intent = intent
m.Unlock()
}
// SessionForDM returns the proper session for sending and receiving DM's.
func (m *Manager) SessionForDM() *discordgo.Session {
m.RLock()
defer m.RUnlock()
// Per Discord documentation, Shard 0 is the only shard which can
// send and receive DM's.
//
// See https://discord.com/developers/docs/topics/gateway#sharding
return m.Shards[0].Session
}
// SessionForGuild returns the proper session for the specified guild.
func (m *Manager) SessionForGuild(guildID int64) *discordgo.Session {
m.RLock()
defer m.RUnlock()
// Formula to determine which shard handles a guild, from Discord
// docs.
//
// See https://discord.com/developers/docs/topics/gateway#sharding
return m.Shards[(guildID>>22)%int64(m.ShardCount)].Session
}
// Restart restarts the Manager, and rescales if necessary, all with zero downtime.
func (m *Manager) Restart() (nMgr *Manager, err error) {
// Lock the old Manager for reading.
m.RLock()
// Create a new Manager using our token.
mgr, err := New(m.token)
if err != nil {
m.RUnlock()
return m, err
}
// Apply the same handlers.
for _, handler := range m.handlers {
mgr.AddHandler(handler)
}
// Apply the same intent
mgr.RegisterIntent(m.Intent)
// We have no need to lock the old Manager at this point, and
// starting the new one will take some time.
m.RUnlock()
// Start the new Manager so that it can begin handling events.
err = mgr.Start()
if err != nil {
return m, err
}
// Shutdown the old Manager. The new Manager is already handling
// events.
m.Shutdown()
return mgr, nil
}
// Start starts the Manager.
func (m *Manager) Start() (err error) {
m.Lock()
defer m.Unlock()
// Ensure that we have at least one Shard.
if m.ShardCount < 1 {
m.ShardCount = 1
}
// Initialize Shards.
m.Shards = []*Shard{}
for i := 0; i < m.ShardCount; i++ {
m.Shards = append(m.Shards, &Shard{})
}
// Add event handlers to Shards and connect them.
for id, shard := range m.Shards {
// Add handlers to this shard.
for _, handler := range m.handlers {
shard.AddHandler(handler)
}
// Connect shard.
err = shard.Init(m.token, id, m.ShardCount, m.Intent, m.stateEnabled)
if err != nil {
return
}
// Ratelimit shard connections.
if id != len(m.Shards)-1 {
time.Sleep(TIMELIMIT)
}
}
return
}
// Shutdown gracefully terminates the Manager.
func (m *Manager) Shutdown() (err error) {
m.apply(func(shard *Shard) {
err = shard.Stop()
if err != nil {
return
}
})
return
}
// apply applies a function to all shards.
func (m *Manager) apply(fn func(*Shard)) {
m.RLock()
defer m.RUnlock()
for _, shard := range m.Shards {
fn(shard)
}
}