This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathdisgord.go
670 lines (568 loc) · 19.6 KB
/
disgord.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// Package disgord provides Go bindings for the documented Discord API, and allows for a stateful Client using the Session interface, with the option of a configurable caching system or bypass the built-in caching logic all together.
//
// # Getting started
//
// Create a Disgord client to get access to the REST API and gateway functionality. In the following example, we listen for new messages and respond with "hello".
//
// Session interface: https://pkg.go.dev/github.com/andersfylling/disgord?tab=doc#Session
//
// client := disgord.New(disgord.Config{
// BotToken: "my-secret-bot-token",
// })
// defer client.Gateway().StayConnectedUntilInterrupted()
//
// client.Gateway().MessageCreate(func(s disgord.Session, evt *disgord.MessageCreate) {
// evt.Message.Reply(context.Background(), s, "hello")
// })
//
// # Listen for events using Channels
//
// You don't have to use a callback function, channels are supported too!
//
// msgChan := make(chan *disgord.MessageCreate, 10)
// client.Gateway().MessageCreateChan(msgChan)
//
// Never close a channel without removing the handler from Disgord, as it will cause a panic. You can control the
// lifetime of a handler or injected channel by in injecting a controller: disgord.HandlerCtrl. Since you are the
// owner of the channel, disgord will never close it for you.
//
// ctrl := &disgord.Ctrl{Channel: msgCreateChan}
// client.Gateway().WithCtrl(ctrl).MessageCreateChan(msgChan)
// go func() {
// // close the channel after 20 seconds and safely remove it from the Disgord reactor
// <- time.After(20 * time.Second)
// ctrl.CloseChannel()
// }
//
// # WebSockets and Sharding
//
// Disgord handles sharding for you automatically; when starting the bot, when discord demands you to scale up your shards (during runtime), etc. It also gives you control over the shard setup in case you want to run multiple instances of Disgord (in these cases you must handle scaling yourself as Disgord can not).
//
// Sharding is done behind the scenes, so you do not need to worry about any settings. Disgord will simply ask Discord for the recommended amount of shards for your bot on startup. However, to set specific amount of shards you can use the `disgord.ShardConfig` to specify a range of valid shard IDs (starts from 0).
//
// starting a bot with exactly 5 shards
//
// client := disgord.New(disgord.Config{
// ShardConfig: disgord.ShardConfig{
// // this is a copy so u can't manipulate the config later on
// ShardIDs: []uint{0,1,2,3,4},
// },
// })
//
// Running multiple instances each with 1 shard (note each instance must use unique shard ids)
//
// client := disgord.New(disgord.Config{
// ShardConfig: disgord.ShardConfig{
// // this is a copy so u can't manipulate the config later on
// ShardIDs: []uint{0}, // this number must change for each instance. Try to automate this.
// ShardCount: 5, // total of 5 shards, but this disgord instance only has one. AutoScaling is disabled - use OnScalingRequired.
// },
// })
//
// Handle scaling options yourself
//
// client := disgord.New(disgord.Config{
// ShardConfig: disgord.ShardConfig{
// // this is a copy so u can't manipulate it later on
// DisableAutoScaling: true,
// OnScalingRequired: func(shardIDs []uint) (TotalNrOfShards uint, AdditionalShardIDs []uint) {
// // instead of asking discord for exact number of shards recommended
// // this is increased by 50% every time discord complains you don't have enough shards
// // to reduce the number of times you have to scale
// TotalNrOfShards := uint(len(shardIDs) * 1.5)
// for i := len(shardIDs) - 1; i < TotalNrOfShards; i++ {
// AdditionalShardIDs = append(AdditionalShardIDs, i)
// }
// return
// }, // end OnScalingRequired
// }, // end ShardConfig
// })
//
// # Caching
//
// You can inject your own cache implementation. By default a read only LFU implementation is used, this should be
// sufficient for the average user. But you can overwrite certain methods as well!
//
// Say you dislike the implementation for MESSAGE_CREATE events, you can embed the default cache and define your own
// logic:
//
// type MyCoolCache struct {
// disgord.BasicCache
// msgCache map[Snowflake]*Message // channelID => Message
// }
// func (c *BasicCache) MessageCreate(data []byte) (*MessageCreate, error) {
// // some smart implementation here
// }
//
// > Note: if you inject your own cache, remember that the cache is also responsible for initiating the objects.
// > See disgord.CacheNop
//
// # Bypass the built-in REST cache
//
// Whenever you call a REST method from the Session interface; the cache is always checked first. Upon a cache hit, no REST request is executed and you get the data from the cache in return. However, if this is problematic for you or there exist a bug which gives you bad/outdated data, you can bypass it by using Disgord flags.
//
// // get a user using the Session implementation (checks cache, and updates the cache on cache miss)
// user, err := client.User(userID).Get()
//
// // bypass the cache checking. Same as before, but we insert a disgord.Flag type.
// user, err := client.User(userID).Get(disgord.IgnoreCache)
//
// # Disgord Flags
//
// In addition to disgord.IgnoreCache, as shown above, you can pass in other flags such as: disgord.SortByID, disgord.OrderAscending, etc. You can find these flags in the flag.go file.
//
// # Build tags
//
// `disgord_diagnosews` will store all the incoming and outgoing JSON data as files in the directory "diagnose-report/packets". The file format is as follows: unix_clientType_direction_shardID_operationCode_sequenceNumber[_eventName].json
package disgord
//go:generate go run internal/generate/intents/main.go
//go:generate go run internal/generate/interfaces/main.go
//go:generate go run internal/generate/inter/main.go
//go:generate go run internal/generate/sorters/main.go
//go:generate go run internal/generate/querybuilders/main.go
import (
"bytes"
"errors"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/andersfylling/disgord/internal/gateway"
"github.com/andersfylling/disgord/json"
"github.com/andersfylling/disgord/internal/util"
"github.com/andersfylling/disgord/internal/constant"
)
const Name = constant.Name
const Version = constant.Version
// LibraryInfo returns name + version
func LibraryInfo() string {
return fmt.Sprintf("%s %s", constant.Name, constant.Version)
}
// DeepCopier holds the DeepCopy method which creates and returns a deep copy of
// any struct.
type DeepCopier interface {
deepCopy() interface{}
}
func DeepCopy(cp DeepCopier) interface{} {
return cp.deepCopy()
}
// Copier holds the CopyOverTo method which copies all it's content from one
// struct to another. Note that this requires a deep copy.
// useful when overwriting already existing content in the cacheLink to reduce GC.
type Copier interface {
copyOverTo(other interface{}) error
}
func DeepCopyOver(dst Copier, src Copier) error {
// TODO: make sure dst and src are of the same type!
return src.copyOverTo(dst)
}
// Reseter Reset() zero initialises or empties a struct instance
type Reseter interface {
reset()
}
func Reset(r Reseter) {
r.reset()
}
// Wrapper for snowflake
// ------------------
// Snowflake twitter snowflake identification for Discord
type Snowflake = util.Snowflake
// GetSnowflake see snowflake.GetSnowflake
func GetSnowflake(v interface{}) (Snowflake, error) {
return util.GetSnowflake(v)
}
// ParseSnowflakeString see snowflake.ParseSnowflakeString
func ParseSnowflakeString(v string) Snowflake {
return util.ParseSnowflakeString(v)
}
func newErrorMissingSnowflake(message string) *ErrorMissingSnowflake {
return &ErrorMissingSnowflake{
info: message,
}
}
// ErrorMissingSnowflake used by methods about to communicate with the Discord API. If a snowflake value is required
// this is used to identify that you must set the value before being able to interact with the Discord API
type ErrorMissingSnowflake struct {
info string
}
func (e *ErrorMissingSnowflake) Error() string {
return e.info
}
func newErrorEmptyValue(message string) *ErrorEmptyValue {
return &ErrorEmptyValue{
info: message,
}
}
// ErrorEmptyValue when a required value was set as empty
type ErrorEmptyValue struct {
info string
}
func (e *ErrorEmptyValue) Error() string {
return e.info
}
func newErrorUnsupportedType(message string) *ErrorUnsupportedType {
return &ErrorUnsupportedType{
info: message,
}
}
// ErrorUnsupportedType used when the given param type is not supported
type ErrorUnsupportedType struct {
info string
}
func (e *ErrorUnsupportedType) Error() string {
return e.info
}
// hasher creates a hash for comparing objects. This excludes the identifier and object type as those are expected
// to be the same during a comparison.
type hasher interface {
hash() string
}
type guilder interface {
getGuildIDs() []Snowflake
}
// Mentioner can be implemented by any type that is mentionable.
// https://discord.com/developers/docs/reference#message-formatting-formats
type Mentioner interface {
Mention() string
}
// zeroInitialiser zero initializes a struct by setting all the values to the default initialization values.
// Used in the flyweight pattern.
type zeroInitialiser interface {
zeroInitialize()
}
// internalUpdater is called whenever a socket event or a REST response is created.
type internalUpdater interface {
updateInternals()
}
type internalClientUpdater interface {
updateInternalsWithClient(*Client)
}
// Discord types
// helperTypes: timestamp, levels, etc.
// discordTimeFormat to be able to correctly convert timestamps back into json,
// we need the micro timestamp with an addition at the ending.
// time.RFC3331 does not yield an output similar to the discord timestamp input, the date is however correct.
const timestampFormat = "2006-01-02T15:04:05.000000+00:00"
// Time handles Discord timestamps
type Time struct {
time.Time
}
var _ json.Marshaler = (*Time)(nil)
var _ json.Unmarshaler = (*Time)(nil)
// MarshalJSON implements json.Marshaler.
// error: https://stackoverflow.com/questions/28464711/go-strange-json-hyphen-unmarshall-error
func (t Time) MarshalJSON() ([]byte, error) {
var ts string
if !t.IsZero() {
ts = t.String()
}
// wrap in double quotes for valid json parsing
return []byte(`"` + ts + `"`), nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (t *Time) UnmarshalJSON(data []byte) error {
var ts time.Time
// Don't try to unmarshal empty strings.
if bytes.Equal([]byte("\"\""), data) {
return nil
}
if err := json.Unmarshal(data, &ts); err != nil {
return err
}
t.Time = ts
return nil
}
// String returns the timestamp as a Discord formatted timestamp. Formatting
// with time.RFC3331 does not suffice.
func (t Time) String() string {
return t.Format(timestampFormat)
}
// -----------
// levels
// ExplicitContentFilterLvl ...
// https://discord.com/developers/docs/resources/guild#guild-object-explicit-content-filter-level
type ExplicitContentFilterLvl uint
// Explicit content filter levels
const (
ExplicitContentFilterLvlDisabled ExplicitContentFilterLvl = iota
ExplicitContentFilterLvlMembersWithoutRoles
ExplicitContentFilterLvlAllMembers
)
// Disabled if the content filter is disabled
func (ecfl *ExplicitContentFilterLvl) Disabled() bool {
return *ecfl == ExplicitContentFilterLvlDisabled
}
// MembersWithoutRoles if the filter only applies for members without a role
func (ecfl *ExplicitContentFilterLvl) MembersWithoutRoles() bool {
return *ecfl == ExplicitContentFilterLvlMembersWithoutRoles
}
// AllMembers if the filter applies for all members regardles of them having a role or not
func (ecfl *ExplicitContentFilterLvl) AllMembers() bool {
return *ecfl == ExplicitContentFilterLvlAllMembers
}
// MFALvl ...
// https://discord.com/developers/docs/resources/guild#guild-object-mfa-level
type MFALvl uint
// Different MFA levels
const (
MFALvlNone MFALvl = iota
MFALvlElevated
)
// None ...
func (mfal *MFALvl) None() bool {
return *mfal == MFALvlNone
}
// Elevated ...
func (mfal *MFALvl) Elevated() bool {
return *mfal == MFALvlElevated
}
// VerificationLvl ...
// https://discord.com/developers/docs/resources/guild#guild-object-verification-level
type VerificationLvl uint
// the different verification levels
const (
VerificationLvlNone VerificationLvl = iota
VerificationLvlLow
VerificationLvlMedium
VerificationLvlHigh
VerificationLvlVeryHigh
)
// None unrestricted
func (vl *VerificationLvl) None() bool {
return *vl == VerificationLvlNone
}
// Low must have verified email on account
func (vl *VerificationLvl) Low() bool {
return *vl == VerificationLvlLow
}
// Medium must be registered on Discord for longer than 5 minutes
func (vl *VerificationLvl) Medium() bool {
return *vl == VerificationLvlMedium
}
// High (╯°□°)╯︵ ┻━┻ - must be a member of the server for longer than 10 minutes
func (vl *VerificationLvl) High() bool {
return *vl == VerificationLvlHigh
}
// VeryHigh ┻━┻ミヽ(ಠ益ಠ)ノ彡┻━┻ - must have a verified phone number
func (vl *VerificationLvl) VeryHigh() bool {
return *vl == VerificationLvlVeryHigh
}
// GuildScheduledEventPrivacyLevel ...
// https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-privacy-level
type GuildScheduledEventPrivacyLevel uint
// the different scheduled event privacy level
const (
GuildScheduledEventPrivacyLevelGuildOnly GuildScheduledEventPrivacyLevel = iota + 2
)
// GuildScheduledEventEntityTypes ...
// https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-entity-types
type GuildScheduledEventEntityTypes uint
// the different scheduled event entity types
const (
GuildScheduledEventEntityTypesStageInstance GuildScheduledEventEntityTypes = iota + 1
GuildScheduledEventEntityTypesVoice
GuildScheduledEventEntityTypesExternal
)
type GuildScheduledEventStatus uint
const (
GuildScheduledEventStatusScheduled GuildScheduledEventStatus = iota + 1
GuildScheduledEventStatusActive
GuildScheduledEventStatusCompleted
GuildScheduledEventStatusCancelled
)
// PremiumTier ...
// https://discord.com/developers/docs/resources/guild#guild-object-premium-tier
type PremiumTier uint
// the different premium tier levels
const (
PremiumTierNone PremiumTier = iota
PremiumTier1
PremiumTier2
PremiumTier3
)
// DefaultMessageNotificationLvl ...
// https://discord.com/developers/docs/resources/guild#guild-object-default-message-notification-level
type DefaultMessageNotificationLvl uint
// different notification levels on new messages
const (
DefaultMessageNotificationLvlAllMessages DefaultMessageNotificationLvl = iota
DefaultMessageNotificationLvlOnlyMentions
)
// AllMessages ...
func (dmnl *DefaultMessageNotificationLvl) AllMessages() bool {
return *dmnl == DefaultMessageNotificationLvlAllMessages
}
// OnlyMentions ...
func (dmnl *DefaultMessageNotificationLvl) OnlyMentions() bool {
return *dmnl == DefaultMessageNotificationLvlOnlyMentions
}
// NewDiscriminator Discord user discriminator hashtag
func NewDiscriminator(d string) (discriminator Discriminator, err error) {
var tmp uint64
tmp, err = strconv.ParseUint(d, 10, 16)
if err == nil {
discriminator = Discriminator(tmp)
}
return
}
// Discriminator value
type Discriminator uint16
var _ json.Unmarshaler = (*Discriminator)(nil)
var _ json.Marshaler = (*Discriminator)(nil)
func (d Discriminator) String() (str string) {
if d == 0 {
str = ""
return
}
if d == 1 {
str = "0001"
return
}
str = strconv.Itoa(int(d))
if d < 1000 {
shift := 4 - len(str)
for i := 0; i < shift; i++ {
str = "0" + str
}
}
return
}
// NotSet checks if the discriminator is not set
func (d Discriminator) NotSet() bool {
return d == 0
}
// UnmarshalJSON see interface json.Unmarshaler
func (d *Discriminator) UnmarshalJSON(data []byte) error {
*d = 0
length := len(data) - 1
for i := 1; i < length; i++ {
*d = *d*10 + Discriminator(data[i]-'0')
}
return nil
}
// MarshalJSON see interface json.Marshaler
func (d Discriminator) MarshalJSON() (data []byte, err error) {
return []byte("\"" + d.String() + "\""), nil
}
// ShardID calculate the shard id for a given guild.
// https://discord.com/developers/docs/topics/gateway#sharding-sharding-formula
func ShardID(guildID Snowflake, nrOfShards uint) uint {
return gateway.GetShardForGuildID(guildID, nrOfShards)
}
//////////////////////////////////////////////////////
//
// Validators
//
//////////////////////////////////////////////////////
// https://discord.com/developers/docs/resources/user#avatar-data
func validAvatarPrefix(avatar string) (valid bool) {
if avatar == "" {
return false
}
construct := func(encoding string) string {
return "data:image/" + encoding + ";base64,"
}
if len(avatar) < len(construct("X")) {
return false // missing base64 declaration
}
encodings := []string{
"jpeg", "png", "gif",
}
for _, encoding := range encodings {
prefix := construct(encoding)
if strings.HasPrefix(avatar, prefix) {
valid = len(avatar)-len(prefix) > 0 // it has content
break
}
}
return true
}
// ValidateUsername uses Discords rule-set to verify user-names and nicknames
// https://discord.com/developers/docs/resources/user#usernames-and-nicknames
//
// Note that not all the rules are listed in the docs:
//
// There are other rules and restrictions not shared here for the sake of spam and abuse mitigation, but the
// majority of Users won't encounter them. It's important to properly handle all error messages returned by
// Discord when editing or updating names.
func ValidateUsername(name string) (err error) {
if name == "" {
return errors.New("empty")
}
// attributes
length := len(name)
// Names must be between 2 and 32 characters long.
if length < 2 {
err = fmt.Errorf("name is too short: %w", ErrIllegalValue)
} else if length > 32 {
err = fmt.Errorf("name is too long: %w", ErrIllegalValue)
}
if err != nil {
return err
}
// Names are sanitized and trimmed of leading, trailing, and excessive internal whitespace.
if name[0] == ' ' {
err = fmt.Errorf("contains whitespace prefix: %w", ErrIllegalValue)
} else if name[length-1] == ' ' {
err = fmt.Errorf("contains whitespace suffix: %w", ErrIllegalValue)
} else {
last := name[1]
for i := 2; i < length-1; i++ {
if name[i] == ' ' && last == name[i] {
err = errors.New("contains excessive internal whitespace")
break
}
last = name[i]
}
}
if err != nil {
return err
}
// Names cannot contain the following substrings: '@', '#', ':', '```'
illegalChars := []string{
"@", "#", ":", "```",
}
for _, illegalChar := range illegalChars {
if strings.Contains(name, illegalChar) {
err = errors.New("can not contain the character " + illegalChar)
return err
}
}
// Names cannot be: 'discordtag', 'everyone', 'here'
illegalNames := []string{
"discordtag", "everyone", "here",
}
for _, illegalName := range illegalNames {
if name == illegalName {
err = fmt.Errorf("the given username is illegal: %w", ErrIllegalValue)
return err
}
}
return nil
}
func validateChannelName(name string) (err error) {
if name == "" {
return ErrMissingChannelName
}
// attributes
length := len(name)
// Names must be of length of minimum 2 and maximum 100 characters long.
if length < 2 {
err = fmt.Errorf("name is too short: %w", ErrIllegalValue)
} else if length > 100 {
err = fmt.Errorf("name is too long: %w", ErrIllegalValue)
}
if err != nil {
return err
}
return nil
}
// CreateTermSigListener create a channel to listen for termination signals (graceful shutdown)
func CreateTermSigListener() <-chan os.Signal {
termSignal := make(chan os.Signal, 1)
signal.Notify(termSignal, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
return termSignal
}