forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
586 lines (485 loc) · 17.3 KB
/
client.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package disgord
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"sync"
"time"
"golang.org/x/net/proxy"
"github.com/andersfylling/disgord/internal/gateway"
"github.com/andersfylling/disgord/internal/logger"
"github.com/andersfylling/disgord/internal/constant"
"github.com/andersfylling/disgord/internal/httd"
)
var DefaultHttpClient = &http.Client{}
// New create a Client. But panics on configuration/setup errors.
func New(conf Config) *Client {
client, err := NewClient(context.Background(), conf)
if err != nil {
panic(err)
}
return client
}
// NewClient creates a new Disgord Client and returns an error on configuration issues
// context is required since a single external request is made to verify bot details
func NewClient(ctx context.Context, conf Config) (*Client, error) {
return createClient(ctx, &conf)
}
func verifyClientProduction(ctx context.Context, client *Client) (Snowflake, error) {
usr, err := client.CurrentUser().WithContext(ctx).Get(IgnoreCache)
if err != nil {
return 0, err
}
if usr == nil {
return 0, fmt.Errorf("unable to gather bot information")
}
if usr.ID.IsZero() {
return 0, fmt.Errorf("for some reason the bot ID is unknown")
}
return usr.ID, nil
}
var verifyClient func(ctx context.Context, client *Client) (Snowflake, error) = verifyClientProduction
// NewClient creates a new Disgord Client and returns an error on configuration issues
func createClient(ctx context.Context, conf *Config) (c *Client, err error) {
if conf.Logger == nil {
conf.Logger = logger.Empty{}
}
if conf.HTTPClient == nil {
// WARNING: do not set http.Client.Timeout (!)
conf.HTTPClient = DefaultHttpClient
} else if conf.HTTPClient.Timeout > 0 {
// https://github.com/nhooyr/websocket/issues/67
return nil, errors.New("do not set timeout in the http.Client, use context.Context instead")
}
if conf.Proxy != nil {
conf.HTTPClient.Transport = &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
return conf.Proxy.Dial(network, addr)
},
}
}
if conf.HttpClient == nil {
if conf.HTTPClient != nil {
conf.HttpClient = conf.HTTPClient
} else {
return nil, errors.New("missing configured HTTP client")
}
}
if conf.WebsocketHttpClient == nil {
conf.WebsocketHttpClient = DefaultHttpClient
}
if conf.Intents > 0 {
conf.DMIntents |= conf.Intents
}
const DMIntents = IntentDirectMessageReactions | IntentDirectMessages | IntentDirectMessageTyping
if validRange := conf.DMIntents & DMIntents; (conf.DMIntents ^ validRange) > 0 {
return nil, errors.New("you have specified intents that are not for DM usage. See documentation")
}
if conf.IgnoreEvents != nil {
conf.Logger.Info("Config.IgnoreEvents has been deprecated. Use Config.RejectEvents instead")
}
conf.RejectEvents = append(conf.RejectEvents, conf.IgnoreEvents...)
// remove extra/duplicates events
uniqueEventNames := make(map[string]bool)
for _, eventName := range conf.RejectEvents {
uniqueEventNames[eventName] = false
}
// if _, ok := uniqueEventNames[EvtUserUpdate]; ok {
// return nil, errors.New("you can not reject the event USER_UPDATE")
// }
if _, ok := uniqueEventNames["PRESENCES_REPLACE"]; !ok {
// https://github.com/discord/discord-api-docs/issues/683
uniqueEventNames["PRESENCES_REPLACE"] = false
}
if _, ok := uniqueEventNames[EvtReady]; ok && conf.LoadMembersQuietly {
return nil, fmt.Errorf("you can not reject the READY event when LoadMembersQuietly is set to true")
}
conf.RejectEvents = make([]string, 0, len(uniqueEventNames))
for eventName, _ := range uniqueEventNames {
conf.RejectEvents = append(conf.RejectEvents, eventName)
}
httdClient, err := httd.NewClient(&httd.Config{
APIVersion: constant.DiscordVersion,
BotToken: conf.BotToken,
UserAgentSourceURL: constant.GitHubURL,
UserAgentVersion: constant.Version,
UserAgentExtra: conf.ProjectName,
HttpClient: conf.HTTPClient,
CancelRequestWhenRateLimited: conf.CancelRequestWhenRateLimited,
RESTBucketManager: conf.RESTBucketManager,
})
if err != nil {
return nil, err
}
if conf.ProjectName == "" {
conf.ProjectName = LibraryInfo()
}
conf.shutdownChan = make(chan interface{})
// caching
var cache Cache
if conf.DisableCache {
if _, ok := conf.Cache.(*CacheNop); !ok {
cache = &CacheNop{}
} else {
cache = conf.Cache
}
} else if conf.Cache == nil {
// don't specify any limits, this should be done by the user instead
cache = NewBasicCache()
} else {
cache = conf.Cache
}
// websocket sharding
evtChan := make(chan *gateway.Event, 2) // TODO: higher value when more shards?
// event dispatcher
dispatch := newDispatcher()
// create a disgord Client/instance/session
c = &Client{
shutdownChan: conf.shutdownChan,
config: conf,
WebsocketHttpClient: conf.WebsocketHttpClient,
botToken: conf.BotToken,
dispatcher: dispatch,
req: httdClient,
cache: cache,
log: conf.Logger,
pool: newPools(),
eventChan: evtChan,
}
c.handlers.c = c // parent reference
c.dispatcher.addSessionInstance(c)
c.clientQueryBuilder.client = c
c.voiceRepository = newVoiceRepository(c)
// this external requests ensures two things:
// - the bot token is valid (a disgord instance is locked to a bot token)
// - that the bot id is always known
if c.botID, err = verifyClient(ctx, c); err != nil {
return nil, err
}
// TODO: this is just waiting to fail
if internalCache, ok := c.cache.(*BasicCache); ok {
internalCache.currentUserID = c.botID
}
return c, nil
}
type ShardConfig = gateway.ShardConfig
type HttpClientDoer interface {
Do(req *http.Request) (*http.Response, error)
}
// Config Configuration for the Disgord Client
type Config struct {
// ################################################
// ##
// ## Basic bot configuration.
// ## This section is for everyone. And beginners
// ## should stick to this section unless they know
// ## what they are doing.
// ##
// ################################################
BotToken string
// HttpClient allows for different wrappers or alternative http logic as long as they have the same
// .Do(..).. method as the http.Client.
// Note that rate limiting is not done in the roundtripper layer at this point, so anything with re-tries, logic
// that triggers a new http request without going through Disgord interface, will not be rate limited and this
// can cause you to get banned in the long term. Be careful.
HttpClient HttpClientDoer
WebsocketHttpClient *http.Client // no way around this, sadly. At least for now.
// Deprecated: use WebsocketHttpClient and HttpClient
HTTPClient *http.Client
// Deprecated: use WebsocketHttpClient and HttpClient
Proxy proxy.Dialer
// Deprecated: use DMIntents (values here are copied to DMIntents for now)
// For direct communication with you bot you must specify intents
Intents Intent
// DMIntents specify intents related to direct message capabilities. Guild related intents are derived
// from the RejectEvents config option (I hope that one day Intents can be removed all together, and
// such optimizations can be handled in the background).
//
// You can see sent intents by enabling debug logging. Remember that derived from RejectIntents are appended.
DMIntents Intent
// your project name, name of bot, or application
ProjectName string
// AlwaysParseChannelMentions will ensure that every message populates the
// Message.ChannelsMentions, regardless of the Discord conditions.
// AlwaysParseChannelMentions bool
// TODO
CancelRequestWhenRateLimited bool
// LoadMembersQuietly will start fetching members for all Guilds in the background.
// There is currently no proper way to detect when the loading is done nor if it
// finished successfully.
LoadMembersQuietly bool
// Presence will automatically be emitted to discord on start up
Presence *UpdateStatusPayload
// for cancellation
shutdownChan chan interface{}
// Logger is a dependency that must be injected to support logging.
// disgord.DefaultLogger() can be used
Logger Logger
// ################################################
// ##
// ## WARNING! For advanced Users only.
// ## This section of options might break the bot,
// ## make it incoherent to the Discord API requirements,
// ## potentially causing your bot to be banned.
// ## You use these features on your own risk.
// ##
// ################################################
RESTBucketManager httd.RESTBucketManager
DisableCache bool
Cache Cache
ShardConfig ShardConfig
// IgnoreEvents will skip events that matches the given event names.
// WARNING! This can break your caching, so be careful about what you want to ignore.
//
// Note this also triggers discord optimizations behind the scenes, such that disgord_diagnosews might
// seem to be missing some events. But actually the lack of certain events will mean Discord aren't sending
// them at all due to how the identify command was defined. eg. guildS_subscriptions
// Deprecated: use RejectEvents instead (nothing changed, just better naming)
IgnoreEvents []string
RejectEvents []string
}
// Client is the main disgord Client to hold your state and data. You must always initiate it using the constructor
// methods (eg. New(..) or NewClient(..)).
//
// Note that this Client holds all the REST methods, and is split across files, into whatever category
// the REST methods regards.
type Client struct {
mu sync.RWMutex
// current bot id
botID Snowflake
clientQueryBuilder
shutdownChan chan interface{}
config *Config
botToken string
permissions PermissionBit
handlers internalHandlers
// reactor demultiplexer for events
dispatcher *dispatcher
// cancelRequestWhenRateLimited by default the Client waits until either the HTTPClient.timeout or
// the rate limit ends before closing a request channel. If activated, in stead, requests will
// instantly be denied, and the process ended with a rate limited error.
cancelRequestWhenRateLimited bool
// req holds the rate limiting logic and error parsing unique for Discord
req *httd.Client
WebsocketHttpClient *http.Client
shardManager gateway.ShardManager
eventChan chan *gateway.Event
connectedGuilds []Snowflake
connectedGuildsMutex sync.RWMutex
cache Cache
log Logger
// voice
*voiceRepository
// pools
pool *pools
}
//////////////////////////////////////////////////////
//
// IMPLEMENTED INTERFACES
//
//////////////////////////////////////////////////////
var _ fmt.Stringer = (*Client)(nil)
var _ Session = (*Client)(nil)
//////////////////////////////////////////////////////
//
// METHODS
//
//////////////////////////////////////////////////////
func (c *Client) Pool() *pools {
return c.pool
}
// AddPermission adds a minimum required permission to the bot. If the permission is negative, it is overwritten to 0.
// This is useful for creating the bot URL.
//
// At the moment, this holds no other effect than aesthetics.
func (c *Client) AddPermission(permission PermissionBit) (updatedPermissions PermissionBit) {
if permission < 0 {
permission = 0
}
c.permissions |= permission
return c.GetPermissions()
}
// GetPermissions returns the minimum bot requirements.
func (c *Client) GetPermissions() (permissions PermissionBit) {
return c.permissions
}
// AvgHeartbeatLatency checks the duration of waiting before receiving a response from Discord when a
// heartbeat packet was sent. Note that heartbeats are usually sent around once a minute and is not a accurate
// way to measure delay between the Client and Discord server
func (c *Client) AvgHeartbeatLatency() (duration time.Duration, err error) {
latencies, err := c.shardManager.HeartbeatLatencies()
if err != nil {
return 0, err
}
var average int64
for _, v := range latencies {
average += v.Nanoseconds()
}
average /= int64(len(latencies))
return time.Duration(average) * time.Nanosecond, nil
}
// HeartbeatLatencies returns latencies mapped to each shard, by their respective ID. shardID => latency.
func (c *Client) HeartbeatLatencies() (latencies map[uint]time.Duration, err error) {
return c.shardManager.HeartbeatLatencies()
}
// GetConnectedGuilds get a list over guild IDs that this Client is "connected to"; or have joined through the ws connection. This will always hold the different Guild IDs, while the GetGuilds or GetCurrentUserGuilds might be affected by cache configuration.
func (c *Client) GetConnectedGuilds() []Snowflake {
c.connectedGuildsMutex.RLock()
defer c.connectedGuildsMutex.RUnlock()
guildIDs := make([]Snowflake, len(c.connectedGuilds))
copy(guildIDs, c.connectedGuilds)
return guildIDs
}
// Logger returns the log instance of Disgord.
// Note that this instance is never nil. When the conf.Logger is not assigned
// an empty struct is used instead. Such that all calls are simply discarded at compile time
// removing the need for nil checks.
func (c *Client) Logger() logger.Logger {
return c.log
}
func (c *Client) String() string {
return LibraryInfo()
}
// RESTBucketGrouping shows which hashed endpoints belong to which bucket hash for the REST API.
// Note that these bucket hashes are eventual consistent.
func (c *Client) RESTRatelimitBuckets() (group map[string][]string) {
return c.req.BucketGrouping()
}
// Cache returns the cacheLink manager for the session
func (c *Client) Cache() Cache {
return c.cache
}
//////////////////////////////////////////////////////
//
// Socket connection
//
//////////////////////////////////////////////////////
func (c *Client) setupConnectEnv() {
// set the user ID upon connection
// only works with socket logic
if c.config.LoadMembersQuietly {
c.Gateway().Ready(c.handlers.loadMembers)
}
c.Gateway().GuildCreate(c.handlers.saveGuildID)
c.Gateway().GuildDelete(c.handlers.deleteGuildID)
// start demultiplexer which also trigger dispatching
go c.demultiplexer(c.dispatcher, c.eventChan)
}
type helperGatewayBotGetter struct {
c *Client
}
var _ gateway.GatewayBotGetter = (*helperGatewayBotGetter)(nil)
func (h helperGatewayBotGetter) GetGatewayBot(ctx context.Context) (gateway *gateway.GatewayBot, err error) {
return h.c.Gateway().WithContext(ctx).GetBot()
}
//////////////////////////////////////////////////////
//
// Internal event handlers
//
//////////////////////////////////////////////////////
type internalHandlers struct {
c *Client
}
// saveGuildID update internal state when joining or creating a guild
func (ih *internalHandlers) saveGuildID(_ Session, evt *GuildCreate) {
client := ih.c
client.connectedGuildsMutex.Lock()
defer client.connectedGuildsMutex.Unlock()
// don't add an entry if there already is one
for i := range client.connectedGuilds {
if client.connectedGuilds[i] == evt.Guild.ID {
return
}
}
client.connectedGuilds = append(client.connectedGuilds, evt.Guild.ID)
}
// deleteGuildID update internal state when deleting or leaving a guild
func (ih *internalHandlers) deleteGuildID(_ Session, evt *GuildDelete) {
client := ih.c
client.connectedGuildsMutex.Lock()
defer client.connectedGuildsMutex.Unlock()
guilds := client.connectedGuilds
for i := range guilds {
if guilds[i] != evt.UnavailableGuild.ID {
continue
}
guilds[i] = guilds[len(guilds)-1]
guilds = guilds[:len(guilds)-1]
break
}
client.connectedGuilds = guilds
}
func (ih *internalHandlers) loadMembers(_ Session, evt *Ready) {
client := ih.c
guildIDs := make([]Snowflake, len(evt.Guilds))
for i := range evt.Guilds {
guildIDs[i] = evt.Guilds[i].ID
}
_, _ = client.Gateway().Dispatch(RequestGuildMembers, &RequestGuildMembersPayload{
GuildIDs: guildIDs,
})
}
//////////////////////////////////////////////////////
//
// REST Methods
// customs
//
//////////////////////////////////////////////////////
func (c *Client) EditInteractionResponse(ctx context.Context, interaction *InteractionCreate, message *Message) error {
endpoint := fmt.Sprintf("/webhooks/%d/%s/messages/@original", interaction.ApplicationID, interaction.Token)
req := &httd.Request{
Endpoint: endpoint,
Method: "PATCH",
Body: message,
Ctx: ctx,
ContentType: httd.ContentTypeJSON,
}
_, _, err := c.req.Do(ctx, req)
return err
}
func (c *Client) SendInteractionResponse(ctx context.Context, interaction *InteractionCreate, data *InteractionResponse) error {
endpoint := fmt.Sprintf("/interactions/%d/%s/callback", interaction.ID, interaction.Token)
req := &httd.Request{
Endpoint: endpoint,
Method: "POST",
Body: data,
Ctx: ctx,
ContentType: httd.ContentTypeJSON,
}
_, _, err := c.req.Do(ctx, req)
return err
}
/* status updates */
// UpdateStatus updates the Client's game status
// note: for simple games, check out UpdateStatusString
func (c *Client) UpdateStatus(s *UpdateStatusPayload) error {
_, err := c.Gateway().Dispatch(UpdateStatus, s)
return err
}
// UpdateStatusString sets the Client's game activity to the provided string, status to online
// and type to Playing
func (c *Client) UpdateStatusString(s string) error {
updateData := &UpdateStatusPayload{
Since: nil,
Game: []*Activity{
{
Name: s,
Type: ActivityTypeGame,
},
},
Status: StatusOnline,
AFK: false,
}
return c.UpdateStatus(updateData)
}
func (c *Client) newRESTRequest(conf *httd.Request, flags []Flag) *rest {
r := &rest{
c: c,
conf: conf,
}
r.init()
r.flags = mergeFlags(flags)
return r
}