-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
74 lines (58 loc) · 2.06 KB
/
gateway.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
package gateway
import (
"errors"
"fmt"
"github.com/discordpkg/gateway/encoding"
"time"
"github.com/discordpkg/gateway/closecode"
"github.com/discordpkg/gateway/event/opcode"
"github.com/discordpkg/gateway/event"
"github.com/discordpkg/gateway/intent"
)
//go:generate go run internal/generate/events/main.go
//go:generate go run internal/generate/closecode/main.go
type ShardID uint
type Payload struct {
Op opcode.Type `json:"op"`
Data encoding.RawMessage `json:"d"`
Seq int64 `json:"s,omitempty"`
EventName event.Type `json:"t,omitempty"`
// CloseCode is a special case for this library.
// You can specify an io.Reader which produces relevant closecode data
// for correct handling of close frames
// TODO: improve documentation
CloseCode closecode.Type `json:"closecode,omitempty"`
}
func (p Payload) String() string {
return fmt.Sprintf("{\n\t\"op\":%d,\n\t\"data\": %s\n\t\"seq\":%d\n}", p.Op, string(p.Data), p.Seq)
}
var ErrSequenceNumberSkipped = errors.New("the sequence number increased with more than 1, events lost")
type DiscordError struct {
CloseCode closecode.Type
OpCode opcode.Type
Reason string
}
func (c *DiscordError) Error() string {
return fmt.Sprintf("[%d | %d]: %s", c.CloseCode, c.OpCode, c.Reason)
}
func (c DiscordError) CanReconnect() bool {
return closecode.CanReconnectAfter(c.CloseCode) || opcode.CanReconnectAfter(c.OpCode)
}
type Handler func(shardID ShardID, evt event.Type, data encoding.RawMessage)
type IdentifyConnectionProperties struct {
OS string `json:"os"`
Browser string `json:"browser"`
Device string `json:"device"`
}
type Identify struct {
BotToken string `json:"token"`
Properties interface{} `json:"properties"`
Compress bool `json:"compress,omitempty"`
LargeThreshold uint8 `json:"large_threshold,omitempty"`
Shard [2]int `json:"shard"`
Presence interface{} `json:"presence"`
Intents intent.Type `json:"intents"`
}
type RateLimiter interface {
Try(ShardID) (bool, time.Duration)
}